2018-02-24 22:59:08 +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"
|
2019-02-26 22:57:04 +02:00
|
|
|
"io"
|
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
|
|
|
"github.com/goreleaser/goreleaser/internal/artifact"
|
2018-07-10 06:38:00 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/semerrgroup"
|
2018-07-09 06:05:38 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/tmpl"
|
2018-08-15 04:50:20 +02:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
2017-04-14 18:31:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Pipe for checksums
|
|
|
|
type Pipe struct{}
|
|
|
|
|
2017-12-02 23:53:19 +02:00
|
|
|
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"
|
|
|
|
}
|
2019-02-04 21:27:51 +02:00
|
|
|
if ctx.Config.Checksum.Algorithm == "" {
|
|
|
|
ctx.Config.Checksum.Algorithm = "sha256"
|
|
|
|
}
|
2017-12-17 20:37:19 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-14 18:31:47 +02:00
|
|
|
// Run the pipe
|
|
|
|
func (Pipe) Run(ctx *context.Context) (err error) {
|
2018-07-09 06:05:38 +02:00
|
|
|
filename, err := tmpl.New(ctx).Apply(ctx.Config.Checksum.NameTemplate)
|
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
|
|
|
|
}
|
2017-12-18 03:04:29 +02:00
|
|
|
defer file.Close() // nolint: errcheck
|
|
|
|
|
2018-07-10 06:38:00 +02:00
|
|
|
var g = semerrgroup.New(ctx.Parallelism)
|
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 {
|
2019-02-04 21:27:51 +02:00
|
|
|
return checksums(ctx.Config.Checksum.Algorithm, file, artifact)
|
2017-04-14 18:50:25 +02:00
|
|
|
})
|
|
|
|
}
|
2017-12-18 03:04:29 +02:00
|
|
|
ctx.Artifacts.Add(artifact.Artifact{
|
|
|
|
Type: artifact.Checksum,
|
|
|
|
Path: file.Name(),
|
|
|
|
Name: filename,
|
|
|
|
})
|
2017-04-14 18:50:25 +02:00
|
|
|
return g.Wait()
|
|
|
|
}
|
|
|
|
|
2019-02-26 22:57:04 +02:00
|
|
|
func checksums(algorithm string, w io.Writer, artifact artifact.Artifact) error {
|
2017-12-17 20:37:19 +02:00
|
|
|
log.WithField("file", artifact.Name).Info("checksumming")
|
2019-02-04 21:27:51 +02:00
|
|
|
sha, err := artifact.Checksum(algorithm)
|
2017-04-14 18:50:25 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-26 23:04:41 +02:00
|
|
|
// TODO: could change the signature to io.StringWriter, but will break
|
|
|
|
// compatibility with go versions bellow 1.12
|
2019-02-26 22:57:04 +02:00
|
|
|
_, err = io.WriteString(w, 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
|
|
|
}
|