Skip to content

Commit

Permalink
feat: allow to use Release and Epoch on nfpm name template (#1396)
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
  • Loading branch information
caarlos0 committed Mar 22, 2020
1 parent c65875f commit 22c9e04
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 8 deletions.
5 changes: 5 additions & 0 deletions internal/pipe/nfpm/nfpm.go
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions internal/pipe/nfpm/nfpm_test.go
Expand Up @@ -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",
},
Expand Down Expand Up @@ -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")
Expand Down
20 changes: 15 additions & 5 deletions internal/tmpl/tmpl.go
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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").
Expand Down
11 changes: 10 additions & 1 deletion internal/tmpl/tmpl_test.go
Expand Up @@ -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"`)
Expand Down Expand Up @@ -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)

}
7 changes: 7 additions & 0 deletions www/content/templates.md
Expand Up @@ -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 |
Expand Down

0 comments on commit 22c9e04

Please sign in to comment.