forked from goreleaser/goreleaser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checksums.go
70 lines (63 loc) · 1.56 KB
/
checksums.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Package checksums provides a Pipe that creates .checksums files for
// each artifact.
package checksums
import (
"fmt"
"os"
"path/filepath"
"github.com/apex/log"
"github.com/goreleaser/goreleaser/checksum"
"github.com/goreleaser/goreleaser/context"
"golang.org/x/sync/errgroup"
)
// Pipe for checksums
type Pipe struct{}
func (Pipe) String() string {
return "calculating checksums"
}
// Run the pipe
func (Pipe) Run(ctx *context.Context) (err error) {
filename, err := filenameFor(ctx)
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,
0444,
)
if err != nil {
return err
}
defer func() {
if err := file.Close(); err != nil {
log.WithError(err).Errorf("failed to close %s", file.Name())
}
ctx.AddArtifact(file.Name())
}()
var g errgroup.Group
for _, artifact := range ctx.Artifacts {
artifact := artifact
g.Go(func() error {
return checksums(ctx, file, artifact)
})
}
return g.Wait()
}
// 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
}
func checksums(ctx *context.Context, file *os.File, name string) error {
log.WithField("file", name).Info("checksumming")
var artifact = filepath.Join(ctx.Config.Dist, name)
sha, err := checksum.SHA256(artifact)
if err != nil {
return err
}
_, err = file.WriteString(fmt.Sprintf("%v %v\n", sha, name))
return err
}