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

81 lines
1.8 KiB
Go
Raw Normal View History

2017-04-14 18:31:47 +02:00
package checksums
import (
2017-04-14 18:50:25 +02:00
"fmt"
2017-04-17 15:47:03 +02:00
"os"
2017-04-14 18:31:47 +02:00
"path/filepath"
2017-06-22 05:09:14 +02:00
"github.com/apex/log"
2017-12-17 20:37:19 +02:00
"golang.org/x/sync/errgroup"
2017-04-14 18:50:25 +02:00
"github.com/goreleaser/goreleaser/checksum"
2017-04-14 18:31:47 +02:00
"github.com/goreleaser/goreleaser/context"
2017-12-17 20:37:19 +02:00
"github.com/goreleaser/goreleaser/internal/artifact"
2017-04-14 18:31:47 +02:00
)
// Pipe for checksums
type Pipe struct{}
func (Pipe) String() string {
return "calculating checksums"
2017-04-14 18:31:47 +02:00
}
2017-12-17 20:37:19 +02:00
// 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
}
2017-04-14 18:31:47 +02:00
// Run the pipe
func (Pipe) Run(ctx *context.Context) (err error) {
filename, err := filenameFor(ctx)
2017-08-28 01:45:33 +02:00
if err != nil {
return err
}
2017-04-17 15:47:03 +02:00
file, err := os.OpenFile(
2017-08-28 01:45:33 +02:00
filepath.Join(ctx.Config.Dist, filename),
2017-04-17 15:47:03 +02:00
os.O_APPEND|os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
0444,
2017-04-17 15:47:03 +02:00
)
if err != nil {
return err
}
defer func() {
if err := file.Close(); err != nil {
log.WithError(err).Errorf("failed to close %s", file.Name())
}
2017-12-17 20:37:19 +02:00
ctx.Artifacts.Add(artifact.Artifact{
Type: artifact.Checksum,
Path: file.Name(),
Name: filename,
})
2017-04-17 15:47:03 +02:00
}()
2017-12-17 20:37:19 +02:00
// TODO: parallelism should be considered here as well.
2017-04-14 18:50:25 +02:00
var g errgroup.Group
2017-12-17 20:59:54 +02:00
for _, artifact := range ctx.Artifacts.Filter(
artifact.Or(
artifact.ByType(artifact.UploadableArchive),
artifact.ByType(artifact.UploadableBinary),
2017-12-18 00:33:28 +02:00
artifact.ByType(artifact.LinuxPackage),
2017-12-17 20:59:54 +02:00
),
).List() {
2017-04-14 18:50:25 +02:00
artifact := artifact
g.Go(func() error {
2017-04-17 15:47:03 +02:00
return checksums(ctx, file, artifact)
2017-04-14 18:50:25 +02:00
})
}
return g.Wait()
}
2017-12-17 20:37:19 +02:00
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)
2017-04-14 18:50:25 +02:00
if err != nil {
return err
}
2017-12-17 20:37:19 +02:00
_, err = file.WriteString(fmt.Sprintf("%v %v\n", sha, artifact.Name))
2017-04-17 15:47:03 +02:00
return err
2017-04-14 18:31:47 +02:00
}