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
|
2017-12-02 23:53:19 +02:00
|
|
|
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) {
|
2017-12-03 01:58:55 +02:00
|
|
|
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,
|
2017-11-26 16:09:12 +02:00
|
|
|
0444,
|
2017-04-17 15:47:03 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
2017-11-26 16:09:12 +02:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2017-12-02 23:53:19 +02:00
|
|
|
// Default sets the pipe defaults
|
|
|
|
func (Pipe) Default(ctx *context.Context) error {
|
2017-12-03 00:18:30 +02:00
|
|
|
if ctx.Config.Checksum.NameTemplate == "" {
|
2017-12-02 23:53:19 +02:00
|
|
|
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
|
|
|
|
}
|
2017-05-11 18:34:29 +02:00
|
|
|
_, 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
|
|
|
}
|