1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-26 04:22:05 +02:00

71 lines
1.6 KiB
Go
Raw Normal View History

2017-04-14 15:39:32 -03:00
// Package checksums provides a Pipe that creates .checksums files for
// each artifact.
2017-04-14 13:31:47 -03:00
package checksums
import (
2017-04-14 13:50:25 -03:00
"fmt"
2017-04-17 10:47:03 -03:00
"os"
2017-04-14 13:31:47 -03:00
"path/filepath"
2017-06-22 00:09:14 -03:00
"github.com/apex/log"
2017-04-14 13:50:25 -03:00
"github.com/goreleaser/goreleaser/checksum"
2017-04-14 13:31:47 -03:00
"github.com/goreleaser/goreleaser/context"
2017-04-14 14:07:46 -03:00
"golang.org/x/sync/errgroup"
2017-04-14 13:31:47 -03:00
)
// Pipe for checksums
type Pipe struct{}
func (Pipe) String() string {
return "calculating checksums"
2017-04-14 13:31:47 -03:00
}
// Run the pipe
func (Pipe) Run(ctx *context.Context) (err error) {
filename, err := filenameFor(ctx)
2017-08-27 20:45:33 -03:00
if err != nil {
return err
}
2017-04-17 10:47:03 -03:00
file, err := os.OpenFile(
2017-08-27 20:45:33 -03:00
filepath.Join(ctx.Config.Dist, filename),
2017-04-17 10:47:03 -03:00
os.O_APPEND|os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
0444,
2017-04-17 10:47:03 -03: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 10:47:03 -03:00
ctx.AddArtifact(file.Name())
}()
2017-04-14 13:50:25 -03:00
var g errgroup.Group
for _, artifact := range ctx.Artifacts {
artifact := artifact
g.Go(func() error {
2017-04-17 10:47:03 -03:00
return checksums(ctx, file, artifact)
2017-04-14 13:50:25 -03: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 10:47:03 -03:00
func checksums(ctx *context.Context, file *os.File, name string) error {
2017-06-22 10:47:34 -03:00
log.WithField("file", name).Info("checksumming")
2017-04-14 13:50:25 -03: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 10:47:03 -03:00
return err
2017-04-14 13:31:47 -03:00
}