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 (
|
2021-12-06 03:42:13 +02:00
|
|
|
"errors"
|
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"
|
2020-07-06 22:09:22 +02:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2021-12-22 19:42:18 +02:00
|
|
|
"sync"
|
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"
|
2021-09-02 04:07:43 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/extrafiles"
|
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
|
|
|
)
|
|
|
|
|
2021-12-22 19:42:18 +02:00
|
|
|
var (
|
|
|
|
errNoArtifacts = errors.New("there are no artifacts to sign")
|
|
|
|
lock sync.Mutex
|
|
|
|
)
|
2021-12-06 03:42:13 +02:00
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// Pipe for checksums.
|
2017-04-14 18:31:47 +02:00
|
|
|
type Pipe struct{}
|
|
|
|
|
2021-09-18 15:21:29 +02:00
|
|
|
func (Pipe) String() string { return "calculating checksums" }
|
|
|
|
func (Pipe) Skip(ctx *context.Context) bool { return ctx.Config.Checksum.Disable }
|
2017-04-14 18:31:47 +02:00
|
|
|
|
2020-05-26 05:48:10 +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"
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// Run the pipe.
|
2021-12-06 03:42:13 +02:00
|
|
|
func (Pipe) Run(ctx *context.Context) error {
|
|
|
|
filename, err := tmpl.New(ctx).Apply(ctx.Config.Checksum.NameTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
filepath := filepath.Join(ctx.Config.Dist, filename)
|
|
|
|
if err := refresh(ctx, filepath); err != nil {
|
|
|
|
if errors.Is(err, errNoArtifacts) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ctx.Artifacts.Add(&artifact.Artifact{
|
|
|
|
Type: artifact.Checksum,
|
|
|
|
Path: filepath,
|
|
|
|
Name: filename,
|
|
|
|
Extra: map[string]interface{}{
|
|
|
|
artifact.ExtraRefresh: func() error {
|
|
|
|
log.WithField("file", filename).Info("refreshing checksums")
|
|
|
|
return refresh(ctx, filepath)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func refresh(ctx *context.Context, filepath string) error {
|
2021-12-22 19:42:18 +02:00
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
2020-12-09 13:38:56 +02:00
|
|
|
filter := artifact.Or(
|
|
|
|
artifact.ByType(artifact.UploadableArchive),
|
|
|
|
artifact.ByType(artifact.UploadableBinary),
|
|
|
|
artifact.ByType(artifact.UploadableSourceArchive),
|
|
|
|
artifact.ByType(artifact.LinuxPackage),
|
2021-12-12 05:21:51 +02:00
|
|
|
artifact.ByType(artifact.SBOM),
|
2020-12-09 13:38:56 +02:00
|
|
|
)
|
|
|
|
if len(ctx.Config.Checksum.IDs) > 0 {
|
|
|
|
filter = artifact.And(filter, artifact.ByIDs(ctx.Config.Checksum.IDs...))
|
|
|
|
}
|
|
|
|
|
|
|
|
artifactList := ctx.Artifacts.Filter(filter).List()
|
2020-04-02 15:18:05 +02:00
|
|
|
|
2021-11-22 19:53:08 +02:00
|
|
|
extraFiles, err := extrafiles.Find(ctx, ctx.Config.Checksum.ExtraFiles)
|
2021-09-02 04:07:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, path := range extraFiles {
|
|
|
|
artifactList = append(artifactList, &artifact.Artifact{
|
|
|
|
Name: name,
|
|
|
|
Path: path,
|
|
|
|
Type: artifact.UploadableFile,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-23 14:40:47 +02:00
|
|
|
if len(artifactList) == 0 {
|
2021-12-06 03:42:13 +02:00
|
|
|
return errNoArtifacts
|
2021-11-23 14:40:47 +02:00
|
|
|
}
|
|
|
|
|
2021-04-19 14:31:57 +02:00
|
|
|
g := semerrgroup.New(ctx.Parallelism)
|
2020-07-06 22:09:22 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-04-17 15:47:03 +02:00
|
|
|
file, err := os.OpenFile(
|
2021-12-06 03:42:13 +02:00
|
|
|
filepath,
|
2017-04-17 15:47:03 +02:00
|
|
|
os.O_APPEND|os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
|
2021-04-19 14:31:57 +02:00
|
|
|
0o644,
|
2017-04-17 15:47:03 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-26 05:48:10 +02:00
|
|
|
defer file.Close()
|
2017-12-18 03:04:29 +02:00
|
|
|
|
2020-07-06 22:09:22 +02:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2020-07-06 22:09:22 +02:00
|
|
|
func checksums(algorithm string, artifact *artifact.Artifact) (string, error) {
|
2021-12-22 19:42:18 +02:00
|
|
|
log.WithField("file", artifact.Name).Debug("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 {
|
2020-07-06 22:09:22 +02:00
|
|
|
return "", err
|
2017-04-14 18:50:25 +02:00
|
|
|
}
|
2020-07-06 22:09:22 +02:00
|
|
|
return fmt.Sprintf("%v %v\n", sha, artifact.Name), nil
|
2017-04-14 18:31:47 +02:00
|
|
|
}
|