1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-11 14:39:28 +02:00

refactory: make easier to manage merges with pro (#3178)

* refactory: improve cmd code

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* fix: improve time reporting

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* fix: improve artifacts json

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* fix: improve artifacts json

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* fix: diffs

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* docs: improve root cmd help

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
This commit is contained in:
Carlos Alexandro Becker 2022-06-22 01:48:11 -03:00 committed by GitHub
parent cb70fa5181
commit 9efea360b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 58 additions and 86 deletions

View File

@ -57,23 +57,14 @@ When using ` + "`--single-target`" + `, the ` + "`GOOS`" + ` and ` + "`GOARCH`"
SilenceUsage: true,
SilenceErrors: true,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
start := time.Now()
log.Infof(boldStyle.Render("building..."))
RunE: timedRunE("build", func(cmd *cobra.Command, args []string) error {
ctx, err := buildProject(root.opts)
if err != nil {
return wrapError(err, boldStyle.Render(fmt.Sprintf("build failed after %0.2fs", time.Since(start).Seconds())))
return err
}
if ctx.Deprecated {
log.Warn(boldStyle.Render("your config is using deprecated properties, check logs above for details"))
}
log.Infof(boldStyle.Render(fmt.Sprintf("build succeeded after %0.2fs", time.Since(start).Seconds())))
deprecateWarn(ctx)
return nil
},
}),
}
cmd.Flags().StringVarP(&root.opts.config, "config", "f", "", "Load configuration from file")

View File

@ -1,7 +1,6 @@
package cmd
import (
"fmt"
"runtime"
"time"
@ -52,23 +51,14 @@ func newReleaseCmd() *releaseCmd {
SilenceUsage: true,
SilenceErrors: true,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
start := time.Now()
log.Infof(boldStyle.Render("releasing..."))
RunE: timedRunE("release", func(cmd *cobra.Command, args []string) error {
ctx, err := releaseProject(root.opts)
if err != nil {
return wrapError(err, boldStyle.Render(fmt.Sprintf("release failed after %0.2fs", time.Since(start).Seconds())))
return err
}
if ctx.Deprecated {
log.Warn(boldStyle.Render("your config is using deprecated properties, check logs above for details"))
}
log.Infof(boldStyle.Render(fmt.Sprintf("release succeeded after %0.2fs", time.Since(start).Seconds())))
deprecateWarn(ctx)
return nil
},
}),
}
cmd.Flags().StringVarP(&root.opts.config, "config", "f", "", "Load configuration from file")
@ -80,7 +70,7 @@ func newReleaseCmd() *releaseCmd {
cmd.Flags().StringVar(&root.opts.releaseFooterTmpl, "release-footer-tmpl", "", "Load custom release notes footer from a templated markdown file (overrides --release-footer)")
cmd.Flags().BoolVar(&root.opts.autoSnapshot, "auto-snapshot", false, "Automatically sets --snapshot if the repo is dirty")
cmd.Flags().BoolVar(&root.opts.snapshot, "snapshot", false, "Generate an unversioned snapshot release, skipping all validations and without publishing any artifacts (implies --skip-publish, --skip-announce and --skip-validate)")
cmd.Flags().BoolVar(&root.opts.skipPublish, "skip-publish", false, "Skips publishing artifacts")
cmd.Flags().BoolVar(&root.opts.skipPublish, "skip-publish", false, "Skips publishing artifacts (implies --skip-announce)")
cmd.Flags().BoolVar(&root.opts.skipAnnounce, "skip-announce", false, "Skips announcing releases (implies --skip-validate)")
cmd.Flags().BoolVar(&root.opts.skipSign, "skip-sign", false, "Skips signing artifacts")
cmd.Flags().BoolVar(&root.opts.skipSBOMCataloging, "skip-sbom", false, "Skips cataloging artifacts")

View File

@ -2,10 +2,13 @@ package cmd
import (
"errors"
"fmt"
"os"
"time"
"github.com/caarlos0/log"
"github.com/charmbracelet/lipgloss"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/muesli/termenv"
"github.com/spf13/cobra"
)
@ -60,7 +63,9 @@ Its goal is to simplify the build, release and publish steps while providing var
GoReleaser is built for CI tools, you only need to download and execute it in your build script. Of course, you can also install it locally if you wish.
You can also customize your entire release process through a single .goreleaser.yaml file.
You can customize your entire release process through a single .goreleaser.yaml file.
Check out our website for more information, examples and documentation: https://goreleaser.com
`,
Version: version,
SilenceUsage: true,
@ -120,3 +125,24 @@ func shouldPrependRelease(cmd *cobra.Command, args []string) bool {
// otherwise, we should probably prepend release
return true
}
func deprecateWarn(ctx *context.Context) {
if ctx.Deprecated {
log.Warn(boldStyle.Render("your config is using deprecated properties, check logs above for details"))
}
}
func timedRunE(verb string, rune func(cmd *cobra.Command, args []string) error) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
start := time.Now()
log.Infof(boldStyle.Render(fmt.Sprintf("starting %s...", verb)))
if err := rune(cmd, args); err != nil {
return wrapError(err, boldStyle.Render(fmt.Sprintf("%s failed after %s", verb, time.Since(start).Truncate(time.Second))))
}
log.Infof(boldStyle.Render(fmt.Sprintf("%s succeeded after %s", verb, time.Since(start).Truncate(time.Second))))
return nil
}
}

View File

@ -112,10 +112,6 @@ func (t Type) String() string {
}
}
func (t Type) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%q", t)), nil
}
const (
ExtraID = "ID"
ExtraBinary = "Binary"
@ -136,8 +132,7 @@ func (e Extras) MarshalJSON() ([]byte, error) {
for k, v := range e {
if k == ExtraRefresh {
// refresh is a func, so we can't serialize it.
// set v to a string representation of the function signature instead.
v = "func() error"
continue
}
m[k] = v
}
@ -153,7 +148,8 @@ type Artifact struct {
Goarm string `json:"goarm,omitempty"`
Gomips string `json:"gomips,omitempty"`
Goamd64 string `json:"goamd64,omitempty"`
Type Type `json:"type,omitempty"`
Type Type `json:"internal_type,omitempty"`
TypeS string `json:"type,omitempty"`
Extra Extras `json:"extra,omitempty"`
}
@ -489,7 +485,7 @@ type VisitFn func(a *Artifact) error
// Visit executes the given function for each artifact in the list.
func (artifacts Artifacts) Visit(fn VisitFn) error {
for _, artifact := range artifacts.List() {
for _, artifact := range artifacts.items {
if err := fn(artifact); err != nil {
return err
}

View File

@ -483,46 +483,6 @@ func TestByFormats(t *testing.T) {
require.Len(t, artifacts.Filter(ByFormats("zip", "tar.gz")).items, 3)
}
func TestTypeToString(t *testing.T) {
for _, a := range []Type{
UploadableArchive,
UploadableBinary,
UploadableFile,
Binary,
UniversalBinary,
LinuxPackage,
PublishableSnapcraft,
Snapcraft,
PublishableDockerImage,
DockerImage,
DockerManifest,
Checksum,
Signature,
Certificate,
UploadableSourceArchive,
BrewTap,
GoFishRig,
KrewPluginManifest,
ScoopManifest,
SBOM,
PkgBuild,
SrcInfo,
} {
t.Run(a.String(), func(t *testing.T) {
require.NotEqual(t, "unknown", a.String())
bts, err := a.MarshalJSON()
require.NoError(t, err)
require.Equal(t, []byte(`"`+a.String()+`"`), bts)
})
}
t.Run("unknown", func(t *testing.T) {
require.Equal(t, "unknown", Type(9999).String())
bts, err := Type(9999).MarshalJSON()
require.NoError(t, err)
require.Equal(t, []byte(`"unknown"`), bts)
})
}
func TestPaths(t *testing.T) {
paths := []string{"a/b", "b/c", "d/e", "f/g"}
artifacts := New()
@ -900,7 +860,11 @@ func Test_ByBinaryLikeArtifacts(t *testing.T) {
arts.Add(a)
}
actual := arts.Filter(ByBinaryLikeArtifacts(arts)).List()
assert.Equal(t, tt.expected, actual)
expected := New()
for _, a := range tt.expected {
expected.Add(a)
}
assert.Equal(t, expected.List(), actual)
if t.Failed() {
t.Log("expected:")

View File

@ -1 +1 @@
[{"name":"foo","type":"Binary","extra":{"ID":"adsad"}},{"name":"foo","type":"Archive","extra":{"ID":"adsad"}},{"name":"foo","type":"Checksum","extra":{"Refresh":"func() error"}}]
[{"name":"foo","internal_type":4,"extra":{"ID":"adsad"}},{"name":"foo","internal_type":1,"extra":{"ID":"adsad"}},{"name":"foo","internal_type":12,"extra":{}}]

View File

@ -8,6 +8,7 @@ import (
"time"
"github.com/caarlos0/log"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/pkg/context"
)
@ -41,6 +42,10 @@ func writeMetadata(ctx *context.Context) error {
}
func writeArtifacts(ctx *context.Context) error {
_ = ctx.Artifacts.Visit(func(a *artifact.Artifact) error {
a.TypeS = a.Type.String()
return nil
})
return writeJSON(ctx, ctx.Artifacts.List(), "artifacts.json")
}

View File

@ -1 +1 @@
[{"name":"foo","path":"foo.txt","goos":"darwin","goarch":"amd64","goarm":"7","type":"Binary","extra":{"foo":"bar"}}]
[{"name":"foo","path":"foo.txt","goos":"darwin","goarch":"amd64","goarm":"7","internal_type":4,"type":"Binary","extra":{"foo":"bar"}}]

View File

@ -7,7 +7,7 @@
package context
import (
ctx "context"
stdctx "context"
"os"
"runtime"
"strings"
@ -69,7 +69,7 @@ const (
// Context carries along some data through the pipes.
type Context struct {
ctx.Context
stdctx.Context
Config config.Project
Env Env
SkipTokenCheck bool
@ -119,17 +119,17 @@ type Semver struct {
// New context.
func New(config config.Project) *Context {
return Wrap(ctx.Background(), config)
return Wrap(stdctx.Background(), config)
}
// NewWithTimeout new context with the given timeout.
func NewWithTimeout(config config.Project, timeout time.Duration) (*Context, ctx.CancelFunc) {
ctx, cancel := ctx.WithTimeout(ctx.Background(), timeout)
func NewWithTimeout(config config.Project, timeout time.Duration) (*Context, stdctx.CancelFunc) {
ctx, cancel := stdctx.WithTimeout(stdctx.Background(), timeout)
return Wrap(ctx, config), cancel
}
// Wrap wraps an existing context.
func Wrap(ctx ctx.Context, config config.Project) *Context {
func Wrap(ctx stdctx.Context, config config.Project) *Context {
return &Context{
Context: ctx,
Config: config,