mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-01-10 03:47:03 +02:00
e3f8178b35
* Add IDs filter to checksums pipe This allows specific artifacts to be excluded from the checksums file. The reason for introducing this, is the requirement of Terraform Registry releases to only contain the prescribed archives in the checksums file. If the file contains more, the release is not accepted by the Terraform Registry. * Add test case for IDs filter of checksums pipe * Document IDs filter of checksum pipe * Always apply type filter for artifacts This is more in line with how the other ID-filters work.
112 lines
2.6 KiB
Go
112 lines
2.6 KiB
Go
// Package checksums provides a Pipe that creates .checksums files for
|
|
// each artifact.
|
|
package checksums
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/apex/log"
|
|
"github.com/goreleaser/goreleaser/internal/artifact"
|
|
"github.com/goreleaser/goreleaser/internal/pipe"
|
|
"github.com/goreleaser/goreleaser/internal/semerrgroup"
|
|
"github.com/goreleaser/goreleaser/internal/tmpl"
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
|
)
|
|
|
|
// Pipe for checksums.
|
|
type Pipe struct{}
|
|
|
|
func (Pipe) String() string {
|
|
return "calculating checksums"
|
|
}
|
|
|
|
// 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"
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Run the pipe.
|
|
func (Pipe) Run(ctx *context.Context) (err error) {
|
|
if ctx.Config.Checksum.Disable {
|
|
return pipe.Skip("checksum.disable is set")
|
|
}
|
|
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
|
|
}
|
|
|
|
var 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)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
file, err := os.OpenFile(
|
|
filepath.Join(ctx.Config.Dist, filename),
|
|
os.O_APPEND|os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
|
|
0644,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer file.Close()
|
|
|
|
ctx.Artifacts.Add(&artifact.Artifact{
|
|
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
|
|
}
|
|
|
|
func checksums(algorithm string, artifact *artifact.Artifact) (string, error) {
|
|
log.WithField("file", artifact.Name).Info("checksumming")
|
|
sha, err := artifact.Checksum(algorithm)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return fmt.Sprintf("%v %v\n", sha, artifact.Name), nil
|
|
}
|