From 22c9e04efdc87dc4eebd14012ad257a236465756 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sun, 22 Mar 2020 13:54:47 -0300 Subject: [PATCH] feat: allow to use Release and Epoch on nfpm name template (#1396) Signed-off-by: Carlos Alexandro Becker --- internal/pipe/nfpm/nfpm.go | 5 +++++ internal/pipe/nfpm/nfpm_test.go | 8 ++++++-- internal/tmpl/tmpl.go | 20 +++++++++++++++----- internal/tmpl/tmpl_test.go | 11 ++++++++++- www/content/templates.md | 7 +++++++ 5 files changed, 43 insertions(+), 8 deletions(-) diff --git a/internal/pipe/nfpm/nfpm.go b/internal/pipe/nfpm/nfpm.go index a68eb18e1c8..1597a85e7ce 100644 --- a/internal/pipe/nfpm/nfpm.go +++ b/internal/pipe/nfpm/nfpm.go @@ -126,6 +126,10 @@ func create(ctx *context.Context, fpm config.NFPM, format, arch string, binaries } name, err := tmpl.New(ctx). WithArtifact(binaries[0], overridden.Replacements). + WithExtraFields(tmpl.Fields{ + "Release": fpm.Release, + "Epoch": fpm.Epoch, + }). Apply(overridden.FileNameTemplate) if err != nil { return err @@ -207,6 +211,7 @@ func create(ctx *context.Context, fpm config.NFPM, format, arch string, binaries Extra: map[string]interface{}{ "Builds": binaries, "ID": fpm.ID, + "Format": format, }, }) return nil diff --git a/internal/pipe/nfpm/nfpm_test.go b/internal/pipe/nfpm/nfpm_test.go index 6bd0915c69f..c612b94d003 100644 --- a/internal/pipe/nfpm/nfpm_test.go +++ b/internal/pipe/nfpm/nfpm_test.go @@ -96,13 +96,15 @@ func TestRunPipe(t *testing.T) { Vendor: "asdf", Homepage: "https://goreleaser.github.io", NFPMOverridables: config.NFPMOverridables{ - FileNameTemplate: defaultNameTemplate, + FileNameTemplate: defaultNameTemplate + "-{{ .Release }}-{{ .Epoch }}", PackageName: "foo", Dependencies: []string{"make"}, Recommends: []string{"svn"}, Suggests: []string{"bzr"}, Conflicts: []string{"git"}, EmptyFolders: []string{"/var/log/foobar"}, + Release: "10", + Epoch: "20", Files: map[string]string{ "./testdata/testfile.txt": "/usr/share/testfile.txt", }, @@ -143,7 +145,9 @@ func TestRunPipe(t *testing.T) { var packages = ctx.Artifacts.Filter(artifact.ByType(artifact.LinuxPackage)).List() require.Len(t, packages, 4) for _, pkg := range packages { - require.Contains(t, pkg.Name, "mybin_1.0.0_Tux_", "linux should have been replaced by Tux") + var format = pkg.ExtraOr("Format", "").(string) + require.NotEmpty(t, format) + require.Equal(t, pkg.Name, "mybin_1.0.0_Tux_"+pkg.Goarch+"-10-20."+format) require.Equal(t, pkg.ExtraOr("ID", ""), "someid") } require.Len(t, ctx.Config.NFPMs[0].Files, 1, "should not modify the config file list") diff --git a/internal/tmpl/tmpl.go b/internal/tmpl/tmpl.go index fe995d59f34..e3f6ee199fd 100644 --- a/internal/tmpl/tmpl.go +++ b/internal/tmpl/tmpl.go @@ -13,10 +13,11 @@ import ( // Template holds data that can be applied to a template string type Template struct { - fields fields + fields Fields } -type fields map[string]interface{} +// Fields that will be available to the template engine. +type Fields map[string]interface{} const ( // general keys @@ -48,7 +49,7 @@ const ( // New Template func New(ctx *context.Context) *Template { return &Template{ - fields: fields{ + fields: Fields{ projectName: ctx.Config.ProjectName, version: ctx.Version, tag: ctx.Git.CurrentTag, @@ -84,7 +85,16 @@ func (t *Template) WithEnv(e map[string]string) *Template { return t } -// WithArtifact populates fields from the artifact and replacements +// WithExtraFields allows to add new more custom fields to the template. +// It will override fields with the same name. +func (t *Template) WithExtraFields(f Fields) *Template { + for k, v := range f { + t.fields[k] = v + } + return t +} + +// WithArtifact populates Fields from the artifact and replacements func (t *Template) WithArtifact(a *artifact.Artifact, replacements map[string]string) *Template { var bin = a.Extra[binary] if bin == nil { @@ -104,7 +114,7 @@ func (t *Template) WithArtifact(a *artifact.Artifact, replacements map[string]st return t } -// Apply applies the given string against the fields stored in the template. +// Apply applies the given string against the Fields stored in the template. func (t *Template) Apply(s string) (string, error) { var out bytes.Buffer tmpl, err := template.New("tmpl"). diff --git a/internal/tmpl/tmpl_test.go b/internal/tmpl/tmpl_test.go index 01472887b16..c84f07de14b 100644 --- a/internal/tmpl/tmpl_test.go +++ b/internal/tmpl/tmpl_test.go @@ -96,7 +96,7 @@ func TestWithArtifact(t *testing.T) { assert.Equal(tt, ctx.Config.ProjectName, result) }) - t.Run("template using artifact fields with no artifact", func(tt *testing.T) { + t.Run("template using artifact Fields with no artifact", func(tt *testing.T) { tt.Parallel() result, err := New(ctx).Apply("{{ .Os }}") assert.EqualError(tt, err, `template: tmpl:1:3: executing "tmpl" at <.Os>: map has no entry for key "Os"`) @@ -215,3 +215,12 @@ func TestEnvNotFound(t *testing.T) { assert.Empty(t, result) assert.EqualError(t, err, `template: tmpl:1:6: executing "tmpl" at <.Env.FOO>: map has no entry for key "FOO"`) } + +func TestWithExtraFields(t *testing.T) { + var ctx = context.New(config.Project{}) + out, _ := New(ctx).WithExtraFields(Fields{ + "MyCustomField": "foo", + }).Apply("{{ .MyCustomField }}") + assert.Equal(t, "foo", out) + +} diff --git a/www/content/templates.md b/www/content/templates.md index ee519d82544..1f3e86bf8d5 100644 --- a/www/content/templates.md +++ b/www/content/templates.md @@ -41,6 +41,13 @@ may have some extra fields: | `.Binary` | Binary name | | `.ArtifactName` | Archive name | +On the NFPM name template field, you can use those extra fields as well: + +| Key | Description | +| :-------------: | :-----------------------------------: | +| `.Release` | Release from the nfpm config | +| `.Epoch` | Epoch from the nfpm config | + On all fields, you have these available functions: | Usage | Description |