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

better checksums

This commit is contained in:
Carlos Alexandro Becker 2017-04-17 10:47:03 -03:00
parent 7eec7d20d0
commit 8e633f28c2
No known key found for this signature in database
GPG Key ID: E61E2F7DC14AB940
2 changed files with 26 additions and 15 deletions

View File

@ -4,8 +4,8 @@ package checksums
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/goreleaser/goreleaser/checksum"
@ -23,31 +23,38 @@ func (Pipe) Description() string {
// Run the pipe
func (Pipe) Run(ctx *context.Context) (err error) {
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())
}()
var g errgroup.Group
for _, artifact := range ctx.Artifacts {
artifact := artifact
g.Go(func() error {
return checksums(ctx, artifact)
return checksums(ctx, file, artifact)
})
}
return g.Wait()
}
func checksums(ctx *context.Context, name string) error {
func checksums(ctx *context.Context, file *os.File, name string) error {
log.Println("Checksumming", name)
var artifact = filepath.Join(ctx.Config.Dist, name)
sha, err := checksum.SHA256(artifact)
if err != nil {
return err
}
var file = filepath.Join(
ctx.Config.Dist,
fmt.Sprintf("%v.%v", name, "checksums"),
)
var content = fmt.Sprintf("%v\t%v\n", sha, name)
if err := ioutil.WriteFile(file, []byte(content), 0644); err != nil {
return err
}
ctx.AddArtifact(file)
return nil
_, err = file.WriteString(fmt.Sprintf("%v\t%v\n", sha, name))
return err
}

View File

@ -16,6 +16,7 @@ func TestDescription(t *testing.T) {
func TestPipe(t *testing.T) {
var assert = assert.New(t)
var checksums = "binary_checksums.txt"
folder, err := ioutil.TempDir("", "goreleasertest")
assert.NoError(err)
var file = filepath.Join(folder, "binary")
@ -23,12 +24,15 @@ func TestPipe(t *testing.T) {
var ctx = &context.Context{
Config: config.Project{
Dist: folder,
Build: config.Build{
Binary: "binary",
},
},
}
ctx.AddArtifact(file)
assert.NoError(Pipe{}.Run(ctx))
assert.Contains(ctx.Artifacts, "binary.checksums", "binary")
bts, err := ioutil.ReadFile(filepath.Join(folder, "binary.checksums"))
assert.Contains(ctx.Artifacts, checksums, "binary")
bts, err := ioutil.ReadFile(filepath.Join(folder, checksums))
assert.NoError(err)
assert.Contains(string(bts), "61d034473102d7dac305902770471fd50f4c5b26f6831a56dd90b5184b3c30fc binary")
}