1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-11-06 09:09:29 +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:
Carlos Alexandro Becker
2020-03-22 13:54:47 -03:00
committed by GitHub
parent c65875fc76
commit 22c9e04efd
5 changed files with 43 additions and 8 deletions

View File

@@ -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").

View File

@@ -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)
}