forked from goreleaser/goreleaser
/
checksums.go
179 lines (158 loc) · 4.27 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Package checksums provides a Pipe that creates .checksums files for
// each artifact.
package checksums
import (
"errors"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"sync"
"github.com/caarlos0/log"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/extrafiles"
"github.com/goreleaser/goreleaser/internal/semerrgroup"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/context"
)
const (
artifactChecksumExtra = "Checksum"
)
var (
errNoArtifacts = errors.New("there are no artifacts to sign")
lock sync.Mutex
)
// Pipe for checksums.
type Pipe struct{}
func (Pipe) String() string { return "calculating checksums" }
func (Pipe) Skip(ctx *context.Context) bool { return ctx.Config.Checksum.Disable }
// 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) 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 {
lock.Lock()
defer lock.Unlock()
filter := artifact.Or(
artifact.ByType(artifact.UploadableArchive),
artifact.ByType(artifact.UploadableBinary),
artifact.ByType(artifact.UploadableSourceArchive),
artifact.ByType(artifact.LinuxPackage),
artifact.ByType(artifact.SBOM),
)
if len(ctx.Config.Checksum.IDs) > 0 {
filter = artifact.And(filter, artifact.ByIDs(ctx.Config.Checksum.IDs...))
}
artifactList := ctx.Artifacts.Filter(filter).List()
extraFiles, err := extrafiles.Find(ctx, 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,
})
}
if len(artifactList) == 0 {
return errNoArtifacts
}
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
}
artifactsMapping := make(map[string]int)
for i, a := range artifactList {
if a.Type == artifact.UploadableFile {
continue // skip extra files
}
// Path is the only unique field for an artifact
artifactsMapping[a.Path] = i
}
err = ctx.Artifacts.Visit(func(a *artifact.Artifact) error {
if index, ok := artifactsMapping[a.Path]; ok {
sumFields := strings.Fields(sumLines[index])
if len(sumFields) == 0 {
return fmt.Errorf("missing checksum in sumline of %s: %s", a.Path, sumFields)
}
sum := sumFields[0]
if a.Extra == nil {
a.Extra = make(artifact.Extras)
}
a.Extra[artifactChecksumExtra] = fmt.Sprintf("%s:%s", ctx.Config.Checksum.Algorithm, sum)
}
return nil
})
if err != nil {
return err
}
file, err := os.OpenFile(
filepath,
os.O_APPEND|os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
0o644,
)
if err != nil {
return err
}
defer file.Close()
// 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).Debug("checksumming")
sha, err := artifact.Checksum(algorithm)
if err != nil {
return "", err
}
return fmt.Sprintf("%v %v\n", sha, artifact.Name), nil
}