From 0311214cbeb67375e1745fb537aa51c1f76f3cc4 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sun, 8 Jul 2018 21:05:38 -0700 Subject: [PATCH] refactor: using tmpl on checksums as well --- pipeline/checksums/checksums.go | 27 ++------------------------- pipeline/checksums/checksums_test.go | 8 ++++++-- 2 files changed, 8 insertions(+), 27 deletions(-) diff --git a/pipeline/checksums/checksums.go b/pipeline/checksums/checksums.go index 002d14fc4..d985fac82 100644 --- a/pipeline/checksums/checksums.go +++ b/pipeline/checksums/checksums.go @@ -3,11 +3,9 @@ package checksums import ( - "bytes" "fmt" "os" "path/filepath" - "text/template" "github.com/apex/log" "golang.org/x/sync/errgroup" @@ -15,6 +13,7 @@ import ( "github.com/goreleaser/goreleaser/checksum" "github.com/goreleaser/goreleaser/context" "github.com/goreleaser/goreleaser/internal/artifact" + "github.com/goreleaser/goreleaser/internal/tmpl" ) // Pipe for checksums @@ -34,7 +33,7 @@ func (Pipe) Default(ctx *context.Context) error { // Run the pipe func (Pipe) Run(ctx *context.Context) (err error) { - filename, err := filenameFor(ctx) + filename, err := tmpl.New(ctx).Apply(ctx.Config.Checksum.NameTemplate) if err != nil { return err } @@ -83,25 +82,3 @@ func checksums(file *os.File, artifact artifact.Artifact) error { _, err = file.WriteString(fmt.Sprintf("%v %v\n", sha, artifact.Name)) return err } - -func filenameFor(ctx *context.Context) (string, error) { - var out bytes.Buffer - t, err := template.New("checksums"). - Option("missingkey=error"). - Parse(ctx.Config.Checksum.NameTemplate) - if err != nil { - return "", err - } - err = t.Execute(&out, struct { - ProjectName string - Tag string - Version string - Env map[string]string - }{ - ProjectName: ctx.Config.ProjectName, - Tag: ctx.Git.CurrentTag, - Version: ctx.Version, - Env: ctx.Env, - }) - return out.String(), err -} diff --git a/pipeline/checksums/checksums_test.go b/pipeline/checksums/checksums_test.go index 4099be9d9..cd13ee4e4 100644 --- a/pipeline/checksums/checksums_test.go +++ b/pipeline/checksums/checksums_test.go @@ -31,6 +31,7 @@ func TestPipe(t *testing.T) { }, }, ) + ctx.Git.CurrentTag = "1.2.3" ctx.Env = map[string]string{"FOO": "bar"} ctx.Artifacts.Add(artifact.Artifact{ Name: binary, @@ -65,6 +66,7 @@ func TestPipeFileNotExist(t *testing.T) { }, }, ) + ctx.Git.CurrentTag = "1.2.3" ctx.Artifacts.Add(artifact.Artifact{ Name: "nope", Path: "/nope", @@ -77,8 +79,8 @@ func TestPipeFileNotExist(t *testing.T) { func TestPipeInvalidNameTemplate(t *testing.T) { for template, eerr := range map[string]string{ - "{{ .Pro }_checksums.txt": `template: checksums:1: unexpected "}" in operand`, - "{{.Env.NOPE}}": `template: checksums:1:6: executing "checksums" at <.Env.NOPE>: map has no entry for key "NOPE"`, + "{{ .Pro }_checksums.txt": `template: tmpl:1: unexpected "}" in operand`, + "{{.Env.NOPE}}": `template: tmpl:1:6: executing "tmpl" at <.Env.NOPE>: map has no entry for key "NOPE"`, } { t.Run(template, func(tt *testing.T) { folder, err := ioutil.TempDir("", "goreleasertest") @@ -92,6 +94,7 @@ func TestPipeInvalidNameTemplate(t *testing.T) { }, }, ) + ctx.Git.CurrentTag = "1.2.3" ctx.Artifacts.Add(artifact.Artifact{ Name: "whatever", Type: artifact.UploadableBinary, @@ -116,6 +119,7 @@ func TestPipeCouldNotOpenChecksumsTxt(t *testing.T) { }, }, ) + ctx.Git.CurrentTag = "1.2.3" ctx.Artifacts.Add(artifact.Artifact{ Name: "whatever", Type: artifact.UploadableBinary,