From 1982259c29b8c214352862b6bb8d668062c9454d Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sun, 17 Dec 2017 16:59:54 -0200 Subject: [PATCH] refactor: improved artifact filtering --- internal/artifact/artifact.go | 43 +++++++++++++++++++++--------- internal/artifact/artifact_test.go | 26 ++++++++++++++---- pipeline/brew/brew.go | 10 ++++--- pipeline/checksums/checksums.go | 14 +++++----- pipeline/docker/docker.go | 14 +++++----- pipeline/release/release.go | 39 +++++++++++++++------------ 6 files changed, 93 insertions(+), 53 deletions(-) diff --git a/internal/artifact/artifact.go b/internal/artifact/artifact.go index 1fd1d7984..c9eadf0df 100644 --- a/internal/artifact/artifact.go +++ b/internal/artifact/artifact.go @@ -2,8 +2,9 @@ package artifact import ( - "fmt" "sync" + + "github.com/apex/log" ) // Type defines the type of an artifact @@ -104,25 +105,41 @@ func ByType(t Type) Filter { } } +// Or performs an OR between all given filters +func Or(filters ...Filter) Filter { + return func(a Artifact) bool { + for _, f := range filters { + if f(a) { + return true + } + } + return false + } +} + +// And performs an AND between all given filters +func And(filters ...Filter) Filter { + return func(a Artifact) bool { + for _, f := range filters { + if !f(a) { + return false + } + } + return true + } +} + // Filter filters the artifact list, returning a new instance. // There are some pre-defined filters but anything of the Type Filter // is accepted. -func (artifacts *Artifacts) Filter(filters ...Filter) Artifacts { +// You can compose filters by using the And and Or filters. +func (artifacts *Artifacts) Filter(filter Filter) Artifacts { var result = New() for _, a := range artifacts.items { - if apply(a, filters) { - fmt.Println("add", a) + if filter(a) { + log.Infof("adding %v", a) result.Add(a) } } return result } - -func apply(a Artifact, filters []Filter) bool { - for _, filter := range filters { - if !filter(a) { - return false - } - } - return true -} diff --git a/internal/artifact/artifact_test.go b/internal/artifact/artifact_test.go index 6a1a35b34..f5b17c45f 100644 --- a/internal/artifact/artifact_test.go +++ b/internal/artifact/artifact_test.go @@ -41,8 +41,9 @@ func TestAdd(t *testing.T) { func TestFilter(t *testing.T) { var data = []Artifact{ { - Name: "foo", - Goos: "linux", + Name: "foo", + Goos: "linux", + Goarch: "arm", }, { Name: "bar", @@ -78,9 +79,24 @@ func TestFilter(t *testing.T) { assert.Len(t, artifacts.Filter(ByType(Checksum)).items, 2) assert.Len(t, artifacts.Filter(ByType(Binary)).items, 0) - assert.Len(t, artifacts.Filter(ByType(Checksum), func(a Artifact) bool { - return a.Name == "checkzumm" - }).List(), 1) + assert.Len(t, artifacts.Filter( + And( + ByType(Checksum), + func(a Artifact) bool { + return a.Name == "checkzumm" + }, + ), + ).List(), 1) + + assert.Len(t, artifacts.Filter( + Or( + ByType(Checksum), + And( + ByGoos("linux"), + ByGoarm("arm"), + ), + ), + ).List(), 2) } func TestGroupByPlatform(t *testing.T) { diff --git a/pipeline/brew/brew.go b/pipeline/brew/brew.go index e9d3bb03c..ec27182e9 100644 --- a/pipeline/brew/brew.go +++ b/pipeline/brew/brew.go @@ -103,10 +103,12 @@ func doRun(ctx *context.Context, client client.Client) error { } var archives = ctx.Artifacts.Filter( - artifact.ByGoos("darwin"), - artifact.ByGoarch("amd64"), - artifact.ByGoarch(""), - artifact.ByType(artifact.UploadableArchive), + artifact.And( + artifact.ByGoos("darwin"), + artifact.ByGoarch("amd64"), + artifact.ByGoarch(""), + artifact.ByType(artifact.UploadableArchive), + ), ).List() if len(archives) == 0 { return ErrNoDarwin64Build diff --git a/pipeline/checksums/checksums.go b/pipeline/checksums/checksums.go index 85fba3f97..df2adcc86 100644 --- a/pipeline/checksums/checksums.go +++ b/pipeline/checksums/checksums.go @@ -56,14 +56,12 @@ func (Pipe) Run(ctx *context.Context) (err error) { }() // TODO: parallelism should be considered here as well. var g errgroup.Group - var artifacts []artifact.Artifact - for _, t := range []artifact.Type{ - artifact.UploadableArchive, - artifact.UploadableBinary, - } { - artifacts = append(artifacts, ctx.Artifacts.Filter(artifact.ByType(t)).List()...) - } - for _, artifact := range artifacts { + for _, artifact := range ctx.Artifacts.Filter( + artifact.Or( + artifact.ByType(artifact.UploadableArchive), + artifact.ByType(artifact.UploadableBinary), + ), + ).List() { artifact := artifact g.Go(func() error { return checksums(ctx, file, artifact) diff --git a/pipeline/docker/docker.go b/pipeline/docker/docker.go index 743f404c3..0bdb0dfb1 100644 --- a/pipeline/docker/docker.go +++ b/pipeline/docker/docker.go @@ -70,12 +70,14 @@ func (Pipe) Run(ctx *context.Context) error { func doRun(ctx *context.Context) error { for _, docker := range ctx.Config.Dockers { var binaries = ctx.Artifacts.Filter( - artifact.ByGoos(docker.Goos), - artifact.ByGoarch(docker.Goarch), - artifact.ByGoarm(docker.Goarm), - func(a artifact.Artifact) bool { - return a.Name == docker.Binary - }, + artifact.And( + artifact.ByGoos(docker.Goos), + artifact.ByGoarch(docker.Goarch), + artifact.ByGoarm(docker.Goarm), + func(a artifact.Artifact) bool { + return a.Name == docker.Binary + }, + ), ).List() for _, binary := range binaries { var err = process(ctx, docker, binary) diff --git a/pipeline/release/release.go b/pipeline/release/release.go index 6644a3bab..6044b40cc 100644 --- a/pipeline/release/release.go +++ b/pipeline/release/release.go @@ -4,7 +4,8 @@ package release import ( "os" - "path/filepath" + + "github.com/goreleaser/goreleaser/internal/artifact" "github.com/apex/log" "github.com/goreleaser/goreleaser/context" @@ -20,15 +21,6 @@ func (Pipe) String() string { return "releasing to GitHub" } -// Run the pipe -func (Pipe) Run(ctx *context.Context) error { - c, err := client.NewGitHub(ctx) - if err != nil { - return err - } - return doRun(ctx, c) -} - // Default sets the pipe defaults func (Pipe) Default(ctx *context.Context) error { if ctx.Config.Release.NameTemplate == "" { @@ -45,6 +37,15 @@ func (Pipe) Default(ctx *context.Context) error { return nil } +// Run the pipe +func (Pipe) Run(ctx *context.Context) error { + c, err := client.NewGitHub(ctx) + if err != nil { + return err + } + return doRun(ctx, c) +} + func doRun(ctx *context.Context, c client.Client) error { if !ctx.Publish { return pipeline.Skip("--skip-publish is set") @@ -62,7 +63,13 @@ func doRun(ctx *context.Context, c client.Client) error { } var g errgroup.Group sem := make(chan bool, ctx.Parallelism) - for _, artifact := range ctx.Artifacts { + for _, artifact := range ctx.Artifacts.Filter( + artifact.Or( + artifact.ByType(artifact.UploadableArchive), + artifact.ByType(artifact.UploadableBinary), + artifact.ByType(artifact.Checksum), + ), + ).List() { sem <- true artifact := artifact g.Go(func() error { @@ -75,14 +82,12 @@ func doRun(ctx *context.Context, c client.Client) error { return g.Wait() } -func upload(ctx *context.Context, c client.Client, releaseID int, artifact string) error { - var path = filepath.Join(ctx.Config.Dist, artifact) - file, err := os.Open(path) +func upload(ctx *context.Context, c client.Client, releaseID int, artifact artifact.Artifact) error { + file, err := os.Open(artifact.Path) if err != nil { return err } defer func() { _ = file.Close() }() - _, name := filepath.Split(path) - log.WithField("file", file.Name()).WithField("name", name).Info("uploading to release") - return c.Upload(ctx, releaseID, name, file) + log.WithField("file", file.Name()).WithField("name", artifact.Name).Info("uploading to release") + return c.Upload(ctx, releaseID, artifact.Name, file) }