1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-24 04:16:27 +02:00

84 lines
2.0 KiB
Go
Raw Normal View History

// Package checksums provides a Pipe that creates .checksums files for
// each artifact.
2017-04-14 13:31:47 -03:00
package checksums
import (
2017-04-14 13:50:25 -03:00
"fmt"
2017-04-17 10:47:03 -03:00
"os"
2017-04-14 13:31:47 -03:00
"path/filepath"
2017-06-22 00:09:14 -03:00
"github.com/apex/log"
2017-12-17 16:37:19 -02:00
"golang.org/x/sync/errgroup"
2017-04-14 13:50:25 -03:00
"github.com/goreleaser/goreleaser/checksum"
2017-04-14 13:31:47 -03:00
"github.com/goreleaser/goreleaser/context"
2017-12-17 16:37:19 -02:00
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/semaphore"
"github.com/goreleaser/goreleaser/internal/tmpl"
2017-04-14 13:31:47 -03:00
)
// Pipe for checksums
type Pipe struct{}
func (Pipe) String() string {
return "calculating checksums"
2017-04-14 13:31:47 -03:00
}
2017-12-17 16: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"
}
return nil
}
2017-04-14 13:31:47 -03:00
// Run the pipe
func (Pipe) Run(ctx *context.Context) (err error) {
filename, err := tmpl.New(ctx).Apply(ctx.Config.Checksum.NameTemplate)
2017-08-27 20:45:33 -03:00
if err != nil {
return err
}
2017-04-17 10:47:03 -03:00
file, err := os.OpenFile(
2017-08-27 20:45:33 -03:00
filepath.Join(ctx.Config.Dist, filename),
2017-04-17 10:47:03 -03:00
os.O_APPEND|os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
0444,
2017-04-17 10:47:03 -03:00
)
if err != nil {
return err
}
2017-12-17 23:04:29 -02:00
defer file.Close() // nolint: errcheck
2017-04-14 13:50:25 -03:00
var g errgroup.Group
var sem = semaphore.New(ctx.Parallelism)
2017-12-17 16:59:54 -02:00
for _, artifact := range ctx.Artifacts.Filter(
artifact.Or(
artifact.ByType(artifact.UploadableArchive),
artifact.ByType(artifact.UploadableBinary),
2017-12-17 20:33:28 -02:00
artifact.ByType(artifact.LinuxPackage),
2017-12-17 16:59:54 -02:00
),
).List() {
sem.Acquire()
2017-04-14 13:50:25 -03:00
artifact := artifact
g.Go(func() error {
defer sem.Release()
2018-07-04 00:42:24 -07:00
return checksums(file, artifact)
2017-04-14 13:50:25 -03:00
})
}
2017-12-17 23:04:29 -02:00
ctx.Artifacts.Add(artifact.Artifact{
Type: artifact.Checksum,
Path: file.Name(),
Name: filename,
})
2017-04-14 13:50:25 -03:00
return g.Wait()
}
2018-07-04 00:42:24 -07:00
func checksums(file *os.File, artifact artifact.Artifact) error {
2017-12-17 16:37:19 -02:00
log.WithField("file", artifact.Name).Info("checksumming")
sha, err := checksum.SHA256(artifact.Path)
2017-04-14 13:50:25 -03:00
if err != nil {
return err
}
2017-12-17 16:37:19 -02:00
_, err = file.WriteString(fmt.Sprintf("%v %v\n", sha, artifact.Name))
2017-04-17 10:47:03 -03:00
return err
2017-04-14 13:31:47 -03:00
}