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

89 lines
2.2 KiB
Go
Raw Normal View History

// 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"
"github.com/goreleaser/goreleaser/internal/semerrgroup"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/context"
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"
}
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) {
artifactList := ctx.Artifacts.Filter(
artifact.Or(
artifact.ByType(artifact.UploadableArchive),
artifact.ByType(artifact.UploadableBinary),
artifact.ByType(artifact.LinuxPackage),
),
).List()
if len(artifactList) == 0 {
return nil
}
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,
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
var g = semerrgroup.New(ctx.Parallelism)
for _, artifact := range artifactList {
2017-04-14 18:50:25 +02:00
artifact := artifact
g.Go(func() error {
return checksums(ctx.Config.Checksum.Algorithm, file, artifact)
2017-04-14 18:50:25 +02:00
})
}
ctx.Artifacts.Add(&artifact.Artifact{
2017-12-18 03:04:29 +02:00
Type: artifact.Checksum,
Path: file.Name(),
Name: filename,
})
2017-04-14 18:50:25 +02:00
return g.Wait()
}
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")
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
}