mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-01-08 03:31:59 +02:00
refactor: fixed checksums pipe
This commit is contained in:
parent
248810535e
commit
bdacb33cea
@ -8,9 +8,11 @@ import (
|
||||
"path/filepath"
|
||||
|
||||
"github.com/apex/log"
|
||||
"golang.org/x/sync/errgroup"
|
||||
|
||||
"github.com/goreleaser/goreleaser/checksum"
|
||||
"github.com/goreleaser/goreleaser/context"
|
||||
"golang.org/x/sync/errgroup"
|
||||
"github.com/goreleaser/goreleaser/internal/artifact"
|
||||
)
|
||||
|
||||
// Pipe for checksums
|
||||
@ -20,6 +22,14 @@ func (Pipe) String() string {
|
||||
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
|
||||
func (Pipe) Run(ctx *context.Context) (err error) {
|
||||
filename, err := filenameFor(ctx)
|
||||
@ -38,11 +48,22 @@ func (Pipe) Run(ctx *context.Context) (err error) {
|
||||
if err := file.Close(); err != nil {
|
||||
log.WithError(err).Errorf("failed to close %s", file.Name())
|
||||
}
|
||||
ctx.AddArtifact(file.Name())
|
||||
ctx.AddChecksum(file.Name())
|
||||
ctx.Artifacts.Add(artifact.Artifact{
|
||||
Type: artifact.Checksum,
|
||||
Path: file.Name(),
|
||||
Name: filename,
|
||||
})
|
||||
}()
|
||||
// TODO: parallelism should be considered here as well.
|
||||
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
|
||||
g.Go(func() error {
|
||||
return checksums(ctx, file, artifact)
|
||||
@ -51,21 +72,12 @@ func (Pipe) Run(ctx *context.Context) (err error) {
|
||||
return g.Wait()
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
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)
|
||||
func checksums(ctx *context.Context, file *os.File, artifact artifact.Artifact) error {
|
||||
log.WithField("file", artifact.Name).Info("checksumming")
|
||||
sha, err := checksum.SHA256(artifact.Path)
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user