From 7c0e2628a4ab618d14cd15df283555d78771d284 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Fri, 14 Apr 2017 12:07:40 -0300 Subject: [PATCH] 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) }