1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-10 03:47:03 +02:00

refactor: fixed checksums pipe

This commit is contained in:
Carlos Alexandro Becker 2017-12-17 16:37:19 -02:00
parent 248810535e
commit bdacb33cea
No known key found for this signature in database
GPG Key ID: E61E2F7DC14AB940

View File

@ -8,9 +8,11 @@ import (
"path/filepath" "path/filepath"
"github.com/apex/log" "github.com/apex/log"
"golang.org/x/sync/errgroup"
"github.com/goreleaser/goreleaser/checksum" "github.com/goreleaser/goreleaser/checksum"
"github.com/goreleaser/goreleaser/context" "github.com/goreleaser/goreleaser/context"
"golang.org/x/sync/errgroup" "github.com/goreleaser/goreleaser/internal/artifact"
) )
// Pipe for checksums // Pipe for checksums
@ -20,6 +22,14 @@ func (Pipe) String() string {
return "calculating checksums" return "calculating checksums"
} }
// Default sets the pipe defaults
func (Pipe) Default(ctx *context.Context) error {
if ctx.Config.Checksum.NameTemplate == "" {
ctx.Config.Checksum.NameTemplate = "{{ .ProjectName }}_{{ .Version }}_checksums.txt"
}
return nil
}
// Run the pipe // Run the pipe
func (Pipe) Run(ctx *context.Context) (err error) { func (Pipe) Run(ctx *context.Context) (err error) {
filename, err := filenameFor(ctx) filename, err := filenameFor(ctx)
@ -38,11 +48,22 @@ func (Pipe) Run(ctx *context.Context) (err error) {
if err := file.Close(); err != nil { if err := file.Close(); err != nil {
log.WithError(err).Errorf("failed to close %s", file.Name()) log.WithError(err).Errorf("failed to close %s", file.Name())
} }
ctx.AddArtifact(file.Name()) ctx.Artifacts.Add(artifact.Artifact{
ctx.AddChecksum(file.Name()) Type: artifact.Checksum,
Path: file.Name(),
Name: filename,
})
}() }()
// TODO: parallelism should be considered here as well.
var g errgroup.Group var g errgroup.Group
for _, artifact := range ctx.Artifacts { 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 {
artifact := artifact artifact := artifact
g.Go(func() error { g.Go(func() error {
return checksums(ctx, file, artifact) return checksums(ctx, file, artifact)
@ -51,21 +72,12 @@ func (Pipe) Run(ctx *context.Context) (err error) {
return g.Wait() return g.Wait()
} }
// Default sets the pipe defaults func checksums(ctx *context.Context, file *os.File, artifact artifact.Artifact) error {
func (Pipe) Default(ctx *context.Context) error { log.WithField("file", artifact.Name).Info("checksumming")
if ctx.Config.Checksum.NameTemplate == "" { sha, err := checksum.SHA256(artifact.Path)
ctx.Config.Checksum.NameTemplate = "{{ .ProjectName }}_{{ .Version }}_checksums.txt"
}
return nil
}
func checksums(ctx *context.Context, file *os.File, name string) error {
log.WithField("file", name).Info("checksumming")
var artifact = filepath.Join(ctx.Config.Dist, name)
sha, err := checksum.SHA256(artifact)
if err != nil { if err != nil {
return err return err
} }
_, err = file.WriteString(fmt.Sprintf("%v %v\n", sha, name)) _, err = file.WriteString(fmt.Sprintf("%v %v\n", sha, artifact.Name))
return err return err
} }