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

126 lines
2.9 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"
2017-04-17 15:47:03 +02:00
"os"
2017-04-14 18:31:47 +02:00
"path/filepath"
"sort"
"strings"
2017-04-14 18:31:47 +02:00
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/extrafiles"
"github.com/goreleaser/goreleaser/internal/pipe"
"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.
2017-04-14 18:31:47 +02:00
type Pipe struct{}
func (Pipe) String() string {
return "calculating checksums"
2017-04-14 18:31:47 +02:00
}
// Default sets the pipe defaults.
2017-12-17 20:37:19 +02:00
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
}
// Run the pipe.
2017-04-14 18:31:47 +02:00
func (Pipe) Run(ctx *context.Context) (err error) {
if ctx.Config.Checksum.Disable {
return pipe.ErrSkipDisabledPipe
}
filter := artifact.Or(
artifact.ByType(artifact.UploadableArchive),
artifact.ByType(artifact.UploadableBinary),
artifact.ByType(artifact.UploadableSourceArchive),
artifact.ByType(artifact.LinuxPackage),
)
if len(ctx.Config.Checksum.IDs) > 0 {
filter = artifact.And(filter, artifact.ByIDs(ctx.Config.Checksum.IDs...))
}
artifactList := ctx.Artifacts.Filter(filter).List()
if len(artifactList) == 0 {
return nil
}
extraFiles, err := extrafiles.Find(ctx.Config.Checksum.ExtraFiles)
if err != nil {
return err
}
for name, path := range extraFiles {
artifactList = append(artifactList, &artifact.Artifact{
Name: name,
Path: path,
Type: artifact.UploadableFile,
})
}
g := semerrgroup.New(ctx.Parallelism)
sumLines := make([]string, len(artifactList))
for i, artifact := range artifactList {
i := i
artifact := artifact
g.Go(func() error {
sumLine, err := checksums(ctx.Config.Checksum.Algorithm, artifact)
if err != nil {
return err
}
sumLines[i] = sumLine
return nil
})
}
err = g.Wait()
if err != nil {
return err
}
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,
0o644,
2017-04-17 15:47:03 +02:00
)
if err != nil {
return err
}
defer file.Close()
2017-12-18 03:04:29 +02:00
ctx.Artifacts.Add(&artifact.Artifact{
2017-12-18 03:04:29 +02:00
Type: artifact.Checksum,
Path: file.Name(),
Name: filename,
})
// sort to ensure the signature is deterministic downstream
sort.Strings(sumLines)
_, err = file.WriteString(strings.Join(sumLines, ""))
return err
2017-04-14 18:50:25 +02:00
}
func checksums(algorithm string, artifact *artifact.Artifact) (string, 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
2017-04-14 18:50:25 +02:00
}
return fmt.Sprintf("%v %v\n", sha, artifact.Name), nil
2017-04-14 18:31:47 +02:00
}