mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-02-09 13:36:56 +02:00
feat: allow to use Release and Epoch on nfpm name template (#1396)
Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
This commit is contained in:
parent
c65875fc76
commit
22c9e04efd
@ -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
|
||||
|
@ -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")
|
||||
|
@ -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").
|
||||
|
@ -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)
|
||||
|
||||
}
|
||||
|
@ -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 |
|
||||
|
Loading…
x
Reference in New Issue
Block a user