From 7c0e2628a4ab618d14cd15df283555d78771d284 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Fri, 14 Apr 2017 12:07:40 -0300 Subject: [PATCH 1/4] fixed old TODO: artifact list on context --- context/context.go | 26 +++++++++++++++++++++----- pipeline/archive/archive.go | 5 +++-- pipeline/fpm/fpm.go | 1 + pipeline/release/release.go | 20 ++++++-------------- 4 files changed, 31 insertions(+), 21 deletions(-) diff --git a/context/context.go b/context/context.go index 1160cb3ff..c232a9f0b 100644 --- a/context/context.go +++ b/context/context.go @@ -2,6 +2,9 @@ package context import ( ctx "context" + "log" + "strings" + "sync" "github.com/goreleaser/goreleaser/config" ) @@ -17,11 +20,24 @@ type GitInfo struct { // Context carries along some data through the pipes type Context struct { ctx.Context - Config config.Project - Token string - Git GitInfo - Archives map[string]string - Version string + Config config.Project + Token string + Git GitInfo + Archives map[string]string + Artifacts []string + Version string +} + +var lock sync.Mutex + +// AddArtifact adds a file to upload list +func (ctx *Context) AddArtifact(file string) { + lock.Lock() + defer lock.Unlock() + file = strings.TrimPrefix(file, ctx.Config.Dist) + file = strings.Replace(file, "/", "", -1) + ctx.Artifacts = append(ctx.Artifacts, file) + log.Println("Registered artifact", file) } // New context diff --git a/pipeline/archive/archive.go b/pipeline/archive/archive.go index 2ecf5f580..4dd5ccbdb 100644 --- a/pipeline/archive/archive.go +++ b/pipeline/archive/archive.go @@ -26,7 +26,7 @@ func (Pipe) Run(ctx *context.Context) error { for _, archive := range ctx.Archives { archive := archive g.Go(func() error { - return create(archive, ctx) + return create(ctx, archive) }) } return g.Wait() @@ -38,7 +38,7 @@ type Archive interface { Add(name, path string) error } -func create(name string, ctx *context.Context) error { +func create(ctx *context.Context, name string) error { folder := filepath.Join(ctx.Config.Dist, name) file, err := os.Create(folder + "." + ctx.Config.Archive.Format) log.Println("Creating", file.Name()) @@ -62,6 +62,7 @@ func create(name string, ctx *context.Context) error { return err } } + ctx.AddArtifact(file.Name()) return nil } diff --git a/pipeline/fpm/fpm.go b/pipeline/fpm/fpm.go index e6abae23d..4df33049f 100644 --- a/pipeline/fpm/fpm.go +++ b/pipeline/fpm/fpm.go @@ -83,5 +83,6 @@ func create(ctx *context.Context, format, archive, arch string) error { if out, err := exec.Command("fpm", options...).CombinedOutput(); err != nil { return errors.New(string(out)) } + ctx.AddArtifact(file) return nil } diff --git a/pipeline/release/release.go b/pipeline/release/release.go index 595709ebd..7e8521343 100644 --- a/pipeline/release/release.go +++ b/pipeline/release/release.go @@ -31,31 +31,23 @@ func doRun(ctx *context.Context, client clients.Client) error { return err } var g errgroup.Group - for _, archive := range ctx.Archives { - archive := archive + for _, artifact := range ctx.Artifacts { + artifact := artifact g.Go(func() error { - return upload(ctx, client, releaseID, archive, ctx.Config.Archive.Format) + return upload(ctx, client, releaseID, artifact) }) - for _, format := range ctx.Config.FPM.Formats { - format := format - g.Go(func() error { - return upload(ctx, client, releaseID, archive, format) - }) - } } return g.Wait() } -func upload(ctx *context.Context, client clients.Client, releaseID int, archive, format string) error { - archive = archive + "." + format - var path = filepath.Join(ctx.Config.Dist, archive) +func upload(ctx *context.Context, client clients.Client, releaseID int, artifact string) error { + var path = filepath.Join(ctx.Config.Dist, artifact) // In case the file doesn't exist, we just ignore it. // We do this because we can get invalid combinations of archive+format here, // like darwinamd64 + deb or something like that. // It's assumed that the archive pipe would fail the entire thing in case it fails to // generate some archive, as well fpm pipe is expected to fail if something wrong happens. // So, here, we just assume IsNotExist as an expected error. - // TODO: maybe add a list of files to upload in the context so we don't have to do this. if _, err := os.Stat(path); os.IsNotExist(err) { return nil } @@ -65,5 +57,5 @@ func upload(ctx *context.Context, client clients.Client, releaseID int, archive, } defer func() { _ = file.Close() }() log.Println("Uploading", file.Name()) - return client.Upload(ctx, releaseID, archive, file) + return client.Upload(ctx, releaseID, artifact, file) } From 3a97034a34d8d84e4e468225d15fa19e08841f17 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Fri, 14 Apr 2017 12:15:51 -0300 Subject: [PATCH 2/4] added tests --- context/context_test.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 context/context_test.go diff --git a/context/context_test.go b/context/context_test.go new file mode 100644 index 000000000..18b3163fd --- /dev/null +++ b/context/context_test.go @@ -0,0 +1,34 @@ +package context + +import ( + "testing" + + "golang.org/x/sync/errgroup" + + "github.com/goreleaser/goreleaser/config" + "github.com/stretchr/testify/assert" +) + +func TestMultipleArtifactAdds(t *testing.T) { + var assert = assert.New(t) + var list = []string{ + "dist/a", + "dist/b", + "dist/c", + "dist/d", + } + var ctx = New(config.Project{ + Dist: "dist", + }) + var g errgroup.Group + for _, f := range list { + f := f + g.Go(func() error { + ctx.AddArtifact(f) + return nil + }) + } + assert.NoError(g.Wait()) + assert.Len(ctx.Artifacts, len(list)) + assert.Contains(ctx.Artifacts, "a", "b", "c", "d") +} From 6bc816b1ca422e9eefdf5c3c8286ac914ce66ec9 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Fri, 14 Apr 2017 12:18:59 -0300 Subject: [PATCH 3/4] fixed broken test --- pipeline/release/release_test.go | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/pipeline/release/release_test.go b/pipeline/release/release_test.go index 24548f98c..434fa3eeb 100644 --- a/pipeline/release/release_test.go +++ b/pipeline/release/release_test.go @@ -21,9 +21,9 @@ func TestRunPipe(t *testing.T) { assert := assert.New(t) folder, err := ioutil.TempDir("", "gorelasertest") assert.NoError(err) - _, err = os.Create(filepath.Join(folder, "bin.tar.gz")) + tarfile, err := os.Create(filepath.Join(folder, "bin.tar.gz")) assert.NoError(err) - _, err = os.Create(filepath.Join(folder, "bin.deb")) + debfile, err := os.Create(filepath.Join(folder, "bin.deb")) assert.NoError(err) var ctx = &context.Context{ Git: context.GitInfo{ @@ -31,25 +31,16 @@ func TestRunPipe(t *testing.T) { }, Config: config.Project{ Dist: folder, - Archive: config.Archive{ - Format: "tar.gz", - }, Release: config.Release{ GitHub: config.Repo{ Owner: "test", Name: "test", }, }, - FPM: config.FPM{ - Formats: []string{ - "deb", - }, - }, - }, - Archives: map[string]string{ - "darwinamd64": "bin", }, } + ctx.AddArtifact(tarfile.Name()) + ctx.AddArtifact(debfile.Name()) client := &DummyClient{} assert.NoError(doRun(ctx, client)) assert.True(client.CreatedRelease) From 054a34c3097534fc15c96dfa8010b8b8737d490e Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Fri, 14 Apr 2017 12:22:18 -0300 Subject: [PATCH 4/4] removed code that is no longer necessary --- pipeline/release/release.go | 9 --------- 1 file changed, 9 deletions(-) diff --git a/pipeline/release/release.go b/pipeline/release/release.go index 7e8521343..3140eeb7e 100644 --- a/pipeline/release/release.go +++ b/pipeline/release/release.go @@ -42,15 +42,6 @@ func doRun(ctx *context.Context, client clients.Client) error { func upload(ctx *context.Context, client clients.Client, releaseID int, artifact string) error { var path = filepath.Join(ctx.Config.Dist, artifact) - // In case the file doesn't exist, we just ignore it. - // We do this because we can get invalid combinations of archive+format here, - // like darwinamd64 + deb or something like that. - // It's assumed that the archive pipe would fail the entire thing in case it fails to - // generate some archive, as well fpm pipe is expected to fail if something wrong happens. - // So, here, we just assume IsNotExist as an expected error. - if _, err := os.Stat(path); os.IsNotExist(err) { - return nil - } file, err := os.Open(path) if err != nil { return err