From c541d99c58658a5888ff9bf3b5401fd7f104d965 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sun, 27 Aug 2017 20:45:33 -0300 Subject: [PATCH] checksums name template support --- .goreleaser.yml | 4 ++- README.md | 15 +++++++++++ config/config.go | 46 ++++++++++++++++++++------------- internal/name/name.go | 13 ++++++++++ internal/name/name_test.go | 31 ++++++++++++++++++---- pipeline/checksums/checksums.go | 10 ++++--- pipeline/defaults/defaults.go | 8 +++++- 7 files changed, 98 insertions(+), 29 deletions(-) diff --git a/.goreleaser.yml b/.goreleaser.yml index 42d6ae947..316fb5e32 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -11,8 +11,10 @@ builds: - amd64 - arm - arm64 +checksum: + name_template: '{{ .ProjectName }}_checksums.txt' archive: - name_template: '{{ .Binary }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}' + name_template: '{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}' replacements: darwin: Darwin linux: Linux diff --git a/README.md b/README.md index c76fa59d3..72ea68759 100644 --- a/README.md +++ b/README.md @@ -339,6 +339,21 @@ snapshot: name_template: SNAPSHOT-{{.Commit}} ``` +### Checksums file customization + +```yml +# .goreleaser.yml +checksum: + # You can change the name of the checksums file. + # This is parsed with Golang template engine and the following variables + # are available: + # - ProjectName + # - Tag + # - Version (Tag with the `v` prefix stripped) + # The default is `{{ .ProjectName }}_{{ .Version }}_checksums.txt` + name_template: "{{ .ProjectName }}_checksums.txt" +``` + ### Homebrew tap customization The brew section specifies how the formula should be created. diff --git a/config/config.go b/config/config.go index ba2d6bebd..57711863c 100644 --- a/config/config.go +++ b/config/config.go @@ -151,6 +151,14 @@ type Snapshot struct { XXX map[string]interface{} `yaml:",inline"` } +// Checksum config +type Checksum struct { + NameTemplate string `yaml:"name_template,omitempty"` + + // Capture all undefined fields and should be empty after loading + XXX map[string]interface{} `yaml:",inline"` +} + // Project includes all project configuration type Project struct { ProjectName string `yaml:"project_name,omitempty"` @@ -161,6 +169,7 @@ type Project struct { FPM FPM `yaml:",omitempty"` Snapcraft Snapcraft `yaml:",omitempty"` Snapshot Snapshot `yaml:",omitempty"` + Checksum Checksum `yaml:",omitempty"` // this is a hack ¯\_(ツ)_/¯ SingleBuild Build `yaml:"build,omitempty"` @@ -196,32 +205,33 @@ func LoadReader(fd io.Reader) (config Project, err error) { } func checkOverflows(config Project) error { - var checker = &overflowChecker{} - checker.check(config.XXX, "") - checker.check(config.Archive.XXX, "archive") + var overflow = &overflowChecker{} + overflow.check(config.XXX, "") + overflow.check(config.Archive.XXX, "archive") for i, ov := range config.Archive.FormatOverrides { - checker.check(ov.XXX, fmt.Sprintf("archive.format_overrides[%d]", i)) + overflow.check(ov.XXX, fmt.Sprintf("archive.format_overrides[%d]", i)) } - checker.check(config.Brew.XXX, "brew") - checker.check(config.Brew.GitHub.XXX, "brew.github") + overflow.check(config.Brew.XXX, "brew") + overflow.check(config.Brew.GitHub.XXX, "brew.github") for i, build := range config.Builds { - checker.check(build.XXX, fmt.Sprintf("builds[%d]", i)) - checker.check(build.Hooks.XXX, fmt.Sprintf("builds[%d].hooks", i)) + overflow.check(build.XXX, fmt.Sprintf("builds[%d]", i)) + overflow.check(build.Hooks.XXX, fmt.Sprintf("builds[%d].hooks", i)) for j, ignored := range build.Ignore { - checker.check(ignored.XXX, fmt.Sprintf("builds[%d].ignored_builds[%d]", i, j)) + overflow.check(ignored.XXX, fmt.Sprintf("builds[%d].ignored_builds[%d]", i, j)) } } - checker.check(config.FPM.XXX, "fpm") - checker.check(config.Snapcraft.XXX, "snapcraft") - checker.check(config.Release.XXX, "release") - checker.check(config.Release.GitHub.XXX, "release.github") - checker.check(config.SingleBuild.XXX, "build") - checker.check(config.SingleBuild.Hooks.XXX, "builds.hooks") + overflow.check(config.FPM.XXX, "fpm") + overflow.check(config.Snapcraft.XXX, "snapcraft") + overflow.check(config.Release.XXX, "release") + overflow.check(config.Release.GitHub.XXX, "release.github") + overflow.check(config.SingleBuild.XXX, "build") + overflow.check(config.SingleBuild.Hooks.XXX, "builds.hooks") for i, ignored := range config.SingleBuild.Ignore { - checker.check(ignored.XXX, fmt.Sprintf("builds.ignored_builds[%d]", i)) + overflow.check(ignored.XXX, fmt.Sprintf("builds.ignored_builds[%d]", i)) } - checker.check(config.Snapshot.XXX, "snapshot") - return checker.err() + overflow.check(config.Snapshot.XXX, "snapshot") + overflow.check(config.Checksum.XXX, "checksum") + return overflow.err() } type overflowChecker struct { diff --git a/internal/name/name.go b/internal/name/name.go index 9085fac00..ca5dbec12 100644 --- a/internal/name/name.go +++ b/internal/name/name.go @@ -54,6 +54,19 @@ func For(ctx *context.Context, target buildtarget.Target) (string, error) { ) } +// ForChecksums returns the filename for the checksums file based on its +// template +func ForChecksums(ctx *context.Context) (string, error) { + return apply( + nameData{ + ProjectName: ctx.Config.ProjectName, + Tag: ctx.Git.CurrentTag, + Version: ctx.Version, + }, + ctx.Config.Checksum.NameTemplate, + ) +} + func apply(data nameData, templateStr string) (string, error) { var out bytes.Buffer t, err := template.New(data.ProjectName).Parse(templateStr) diff --git a/internal/name/name_test.go b/internal/name/name_test.go index 5cf0ef399..8c106c695 100644 --- a/internal/name/name_test.go +++ b/internal/name/name_test.go @@ -10,8 +10,30 @@ import ( "github.com/stretchr/testify/assert" ) +func TestChecksums(t *testing.T) { + var assert = assert.New(t) + + var config = config.Project{ + Checksum: config.Checksum{ + NameTemplate: "{{.ProjectName }}_{{.Tag}}_{{.Version}}_checksums.txt", + }, + ProjectName: "testcheck", + } + var ctx = &context.Context{ + Config: config, + Version: "1.0.0", + Git: context.GitInfo{ + CurrentTag: "v1.0.0", + }, + } + + name, err := ForChecksums(ctx) + assert.NoError(err) + assert.Equal("testcheck_v1.0.0_1.0.0_checksums.txt", name) +} + func TestNameFor(t *testing.T) { - assert := assert.New(t) + var assert = assert.New(t) var config = config.Project{ Archive: config.Archive{ @@ -37,7 +59,7 @@ func TestNameFor(t *testing.T) { } func TestNameForBuild(t *testing.T) { - assert := assert.New(t) + var assert = assert.New(t) var ctx = &context.Context{ Config: config.Project{ @@ -83,8 +105,8 @@ func TestInvalidNameTemplate(t *testing.T) { assert.Error(err) } -func TestNameDefaltTemplate(t *testing.T) { - assert := assert.New(t) +func TestNameDefaultTemplate(t *testing.T) { + var assert = assert.New(t) var ctx = &context.Context{ Config: config.Project{ Archive: config.Archive{ @@ -108,5 +130,4 @@ func TestNameDefaltTemplate(t *testing.T) { assert.Equal(key, name) }) } - } diff --git a/pipeline/checksums/checksums.go b/pipeline/checksums/checksums.go index 717b79a21..4fd05047e 100644 --- a/pipeline/checksums/checksums.go +++ b/pipeline/checksums/checksums.go @@ -10,6 +10,7 @@ import ( "github.com/apex/log" "github.com/goreleaser/goreleaser/checksum" "github.com/goreleaser/goreleaser/context" + "github.com/goreleaser/goreleaser/internal/name" "golang.org/x/sync/errgroup" ) @@ -23,11 +24,12 @@ func (Pipe) Description() string { // Run the pipe func (Pipe) Run(ctx *context.Context) (err error) { + filename, err := name.ForChecksums(ctx) + if err != nil { + return err + } file, err := os.OpenFile( - filepath.Join( - ctx.Config.Dist, - fmt.Sprintf("%v_checksums.txt", ctx.Config.ProjectName), - ), + filepath.Join(ctx.Config.Dist, filename), os.O_APPEND|os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644, ) diff --git a/pipeline/defaults/defaults.go b/pipeline/defaults/defaults.go index 0289b52e6..5f6a8070c 100644 --- a/pipeline/defaults/defaults.go +++ b/pipeline/defaults/defaults.go @@ -12,11 +12,14 @@ import ( ) // NameTemplate default name_template for the archive. -const NameTemplate = "{{ .Binary }}_{{.Version}}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}" +const NameTemplate = "{{ .Binary }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}" // SnapshotNameTemplate represents the default format for snapshot release names. const SnapshotNameTemplate = "SNAPSHOT-{{ .Commit }}" +// ChecksumNameTemplate is the default name_template for the checksum file. +const ChecksumNameTemplate = "{{ .ProjectName }}_{{ .Version }}_checksums.txt" + // Pipe for brew deployment type Pipe struct{} @@ -31,6 +34,9 @@ func (Pipe) Run(ctx *context.Context) error { if ctx.Config.Snapshot.NameTemplate == "" { ctx.Config.Snapshot.NameTemplate = SnapshotNameTemplate } + if ctx.Config.Checksum.NameTemplate == "" { + ctx.Config.Checksum.NameTemplate = ChecksumNameTemplate + } if err := setReleaseDefaults(ctx); err != nil { return err }