From 8e633f28c24875001a9e396060d106e138fe7471 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Mon, 17 Apr 2017 10:47:03 -0300 Subject: [PATCH] better checksums --- pipeline/checksums/checksums.go | 33 +++++++++++++++++----------- pipeline/checksums/checksums_test.go | 8 +++++-- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/pipeline/checksums/checksums.go b/pipeline/checksums/checksums.go index a8323a0ff..df8bd8c42 100644 --- a/pipeline/checksums/checksums.go +++ b/pipeline/checksums/checksums.go @@ -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 } diff --git a/pipeline/checksums/checksums_test.go b/pipeline/checksums/checksums_test.go index 27c9d8d7a..1ce00ceb6 100644 --- a/pipeline/checksums/checksums_test.go +++ b/pipeline/checksums/checksums_test.go @@ -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") }