1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-10 03:47:03 +02:00
goreleaser/pipeline/checksums/checksums.go

65 lines
1.3 KiB
Go
Raw Normal View History

2017-04-14 18:31:47 +02:00
package checksums
import (
2017-04-14 18:50:25 +02:00
"fmt"
"log"
2017-04-14 18:31:47 +02:00
"os"
"path/filepath"
2017-04-14 18:50:25 +02:00
"github.com/goreleaser/goreleaser/checksum"
2017-04-14 18:31:47 +02:00
"github.com/goreleaser/goreleaser/context"
2017-04-14 19:07:46 +02:00
"golang.org/x/sync/errgroup"
2017-04-14 18:31:47 +02:00
)
// Pipe for checksums
type Pipe struct{}
// Description of the pipe
func (Pipe) Description() string {
return "Calculating checksums"
}
// Run the pipe
func (Pipe) Run(ctx *context.Context) (err error) {
2017-04-14 18:50:25 +02:00
var g errgroup.Group
for _, artifact := range ctx.Artifacts {
artifact := artifact
g.Go(func() error {
return checksums(ctx, artifact)
})
}
return g.Wait()
}
func checksums(ctx *context.Context, name string) error {
log.Println("Checksumming", name)
var artifact = filepath.Join(ctx.Config.Dist, name)
var checksums = fmt.Sprintf("%v.%v", name, "checksums")
sha, err := checksum.SHA256(artifact)
if err != nil {
return err
}
md5, err := checksum.MD5(artifact)
if err != nil {
return err
}
2017-04-14 18:31:47 +02:00
file, err := os.OpenFile(
2017-04-14 18:50:25 +02:00
filepath.Join(ctx.Config.Dist, checksums),
os.O_APPEND|os.O_WRONLY|os.O_CREATE|os.O_EXCL,
2017-04-14 18:31:47 +02:00
0600,
)
2017-04-14 19:11:17 +02:00
if err != nil {
return err
}
2017-04-14 18:31:47 +02:00
defer func() { _ = file.Close() }()
2017-04-14 18:50:25 +02:00
var template = "%v %v\n"
if _, err = file.WriteString(fmt.Sprintf(template, "md5sum", md5)); err != nil {
return err
}
if _, err = file.WriteString(fmt.Sprintf(template, "sha256sum", sha)); err != nil {
return err
2017-04-14 18:31:47 +02:00
}
ctx.AddArtifact(file.Name())
2017-04-14 18:50:25 +02:00
return nil
2017-04-14 18:31:47 +02:00
}