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

73 lines
1.6 KiB
Go
Raw Normal View History

2017-04-14 20:39:32 +02:00
// Package checksums provides a Pipe that creates .checksums files for
// each artifact.
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-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-08-28 01:45:33 +02:00
"github.com/goreleaser/goreleaser/internal/name"
2017-04-14 19:07:46 +02:00
"golang.org/x/sync/errgroup"
2017-04-14 18:31:47 +02:00
)
// Pipe for checksums
type Pipe struct{}
// Description of the pipe
func (Pipe) String() string {
return "calculating checksums"
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-04-17 15:47:03 +02:00
ctx.AddArtifact(file.Name())
}()
2017-04-14 18:50:25 +02:00
var g errgroup.Group
for _, artifact := range ctx.Artifacts {
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()
}
// 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-17 15:47:03 +02:00
func checksums(ctx *context.Context, file *os.File, name string) error {
2017-06-22 15:47:34 +02:00
log.WithField("file", name).Info("checksumming")
2017-04-14 18:50:25 +02:00
var artifact = filepath.Join(ctx.Config.Dist, name)
sha, err := checksum.SHA256(artifact)
if err != nil {
return err
}
_, err = file.WriteString(fmt.Sprintf("%v %v\n", sha, name))
2017-04-17 15:47:03 +02:00
return err
2017-04-14 18:31:47 +02:00
}