1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-24 04:16:27 +02:00

fixed old TODO: artifact list on context

This commit is contained in:
Carlos Alexandro Becker 2017-04-14 12:07:40 -03:00
parent 21dc17750e
commit 7c0e2628a4
No known key found for this signature in database
GPG Key ID: E61E2F7DC14AB940
4 changed files with 31 additions and 21 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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
}

View File

@ -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)
}