1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-26 04:22:05 +02:00

61 lines
1.2 KiB
Go
Raw Normal View History

2017-04-14 15:39:32 -03:00
// 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"
"log"
2017-04-17 10:47:03 -03:00
"os"
2017-04-14 13:31:47 -03:00
"path/filepath"
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-04-14 14:07:46 -03:00
"golang.org/x/sync/errgroup"
2017-04-14 13:31:47 -03: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-17 10:47:03 -03:00
file, err := os.OpenFile(
filepath.Join(
ctx.Config.Dist,
fmt.Sprintf("%v_checksums.txt", ctx.Config.Build.Binary),
),
os.O_APPEND|os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
0644,
)
if err != nil {
return err
}
defer func() {
_ = file.Close()
ctx.AddArtifact(file.Name())
}()
2017-04-14 13:50:25 -03:00
var g errgroup.Group
for _, artifact := range ctx.Artifacts {
artifact := artifact
g.Go(func() error {
2017-04-17 10:47:03 -03:00
return checksums(ctx, file, artifact)
2017-04-14 13:50:25 -03:00
})
}
return g.Wait()
}
2017-04-17 10:47:03 -03:00
func checksums(ctx *context.Context, file *os.File, name string) error {
2017-04-14 13:50:25 -03:00
log.Println("Checksumming", name)
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))
2017-04-17 10:47:03 -03:00
return err
2017-04-14 13:31:47 -03:00
}