From a38ebfcc8102eed327d8d3e1ca3e01ab5f36f376 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Fri, 14 Apr 2017 13:31:47 -0300 Subject: [PATCH] checksumming wip --- main.go | 6 ++-- pipeline/checksums/checksums.go | 41 ++++++++++++++++++++++++++++ pipeline/checksums/checksums_test.go | 21 ++++++++++++++ 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 pipeline/checksums/checksums.go create mode 100644 pipeline/checksums/checksums_test.go diff --git a/main.go b/main.go index 8410a7173..fa44f0c9e 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( "github.com/goreleaser/goreleaser/pipeline/archive" "github.com/goreleaser/goreleaser/pipeline/brew" "github.com/goreleaser/goreleaser/pipeline/build" + "github.com/goreleaser/goreleaser/pipeline/checksums" "github.com/goreleaser/goreleaser/pipeline/defaults" "github.com/goreleaser/goreleaser/pipeline/env" "github.com/goreleaser/goreleaser/pipeline/fpm" @@ -87,8 +88,9 @@ func pipes(buildOnly bool) []pipeline.Pipe { if !buildOnly { pipes = append( pipes, - release.Pipe{}, // release to github - brew.Pipe{}, // push to brew tap + checksums.Pipe{}, // checksums of the files + release.Pipe{}, // release to github + brew.Pipe{}, // push to brew tap ) } return pipes diff --git a/pipeline/checksums/checksums.go b/pipeline/checksums/checksums.go new file mode 100644 index 000000000..be33a5a0c --- /dev/null +++ b/pipeline/checksums/checksums.go @@ -0,0 +1,41 @@ +package checksums + +import ( + "os" + "path/filepath" + + "github.com/goreleaser/goreleaser/context" + "github.com/goreleaser/goreleaser/sha256sum" +) + +// 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) { + file, err := os.OpenFile( + filepath.Join(ctx.Config.Dist, "CHECKSUMS.txt"), + os.O_APPEND|os.O_WRONLY|os.O_CREATE, + 0600, + ) + if err != nil { + return + } + defer func() { _ = file.Close() }() + for _, artifact := range ctx.Artifacts { + sha, err := sha256sum.For(filepath.Join(ctx.Config.Dist, artifact)) + if err != nil { + return err + } + if _, err = file.WriteString(artifact + " sha256sum: " + sha + "\n"); err != nil { + return err + } + } + ctx.AddArtifact(file.Name()) + return +} diff --git a/pipeline/checksums/checksums_test.go b/pipeline/checksums/checksums_test.go new file mode 100644 index 000000000..6ec1fe02a --- /dev/null +++ b/pipeline/checksums/checksums_test.go @@ -0,0 +1,21 @@ +package checksums + +import ( + "testing" + + "github.com/goreleaser/goreleaser/config" + "github.com/goreleaser/goreleaser/context" + "github.com/stretchr/testify/assert" +) + +func TestDescription(t *testing.T) { + assert.NotEmpty(t, Pipe{}.Description()) +} + +func TestBlah(t *testing.T) { + var assert = assert.New(t) + var ctx = &context.Context{ + Config: config.Project{}, + } + assert.NoError(Pipe{}.Run(ctx)) +}