1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-04-25 12:24:44 +02:00

feat(nfpm): allow to template contents.dst (#2269)

* feat(nfpm): allow to template contents.dst

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>

* fix: revert unwanted change

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>

* fix: gofumpt

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>
This commit is contained in:
Carlos Alexandro Becker 2021-05-31 00:28:11 -03:00 committed by GitHub
parent d3bc4fe2c5
commit 3ca9e6d4dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 26 deletions

View File

@ -192,9 +192,13 @@ func create(ctx *context.Context, fpm config.NFPM, format, arch string, binaries
if err != nil { if err != nil {
return err return err
} }
dst, err := tmpl.Apply(content.Destination)
if err != nil {
return err
}
contents = append(contents, &files.Content{ contents = append(contents, &files.Content{
Source: src, Source: src,
Destination: content.Destination, Destination: dst,
Type: content.Type, Type: content.Type,
Packager: content.Packager, Packager: content.Packager,
FileInfo: content.FileInfo, FileInfo: content.FileInfo,

View File

@ -131,7 +131,7 @@ func TestRunPipe(t *testing.T) {
}, },
{ {
Source: "./testdata/testfile-{{ .Arch }}.txt", Source: "./testdata/testfile-{{ .Arch }}.txt",
Destination: "/etc/nope3.conf", Destination: "/etc/nope3_{{ .ProjectName }}.conf",
}, },
}, },
Replacements: map[string]string{ Replacements: map[string]string{
@ -178,7 +178,7 @@ func TestRunPipe(t *testing.T) {
"/etc/nope.conf", "/etc/nope.conf",
"/etc/nope-rpm.conf", "/etc/nope-rpm.conf",
"/etc/nope2.conf", "/etc/nope2.conf",
"/etc/nope3.conf", "/etc/nope3_mybin.conf",
"/usr/bin/mybin", "/usr/bin/mybin",
}, destinations(pkg.ExtraOr("Files", files.Contents{}).(files.Contents))) }, destinations(pkg.ExtraOr("Files", files.Contents{}).(files.Contents)))
} }
@ -186,32 +186,70 @@ func TestRunPipe(t *testing.T) {
} }
func TestInvalidNameTemplate(t *testing.T) { func TestInvalidNameTemplate(t *testing.T) {
ctx := &context.Context{ makeCtx := func() *context.Context {
Parallelism: runtime.NumCPU(), ctx := &context.Context{
Artifacts: artifact.New(), Version: "1.2.3",
Config: config.Project{ Parallelism: runtime.NumCPU(),
NFPMs: []config.NFPM{ Artifacts: artifact.New(),
{ Config: config.Project{
NFPMOverridables: config.NFPMOverridables{ NFPMs: []config.NFPM{
PackageName: "foo", {
FileNameTemplate: "{{.Foo}", Formats: []string{"deb"},
Builds: []string{"default"},
}, },
Formats: []string{"deb"},
Builds: []string{"default"},
}, },
}, },
}, }
ctx.Artifacts.Add(&artifact.Artifact{
Name: "mybin",
Goos: "linux",
Goarch: "amd64",
Type: artifact.Binary,
Extra: map[string]interface{}{
"ID": "default",
},
})
return ctx
} }
ctx.Artifacts.Add(&artifact.Artifact{
Name: "mybin", t.Run("filename_template", func(t *testing.T) {
Goos: "linux", ctx := makeCtx()
Goarch: "amd64", ctx.Config.NFPMs[0].NFPMOverridables = config.NFPMOverridables{
Type: artifact.Binary, PackageName: "foo",
Extra: map[string]interface{}{ FileNameTemplate: "{{.Foo}",
"ID": "default", }
}, require.Contains(t, Pipe{}.Run(ctx).Error(), `template: tmpl:1: unexpected "}" in operand`)
})
t.Run("source", func(t *testing.T) {
ctx := makeCtx()
ctx.Config.NFPMs[0].NFPMOverridables = config.NFPMOverridables{
PackageName: "foo",
FileNameTemplate: "Foo",
Contents: files.Contents{
{
Source: "{{ .NOPE_SOURCE }}",
Destination: "/foo",
},
},
}
require.Contains(t, Pipe{}.Run(ctx).Error(), `template: tmpl:1:3: executing "tmpl" at <.NOPE_SOURCE>: map has no entry for key "NOPE_SOURCE"`)
})
t.Run("target", func(t *testing.T) {
ctx := makeCtx()
ctx.Config.NFPMs[0].NFPMOverridables = config.NFPMOverridables{
PackageName: "foo",
FileNameTemplate: "Foo",
Contents: files.Contents{
{
Source: "./testdata/testfile.txt",
Destination: "{{ .NOPE_TARGET }}",
},
},
}
require.Contains(t, Pipe{}.Run(ctx).Error(), `template: tmpl:1:3: executing "tmpl" at <.NOPE_TARGET>: map has no entry for key "NOPE_TARGET"`)
}) })
require.Contains(t, Pipe{}.Run(ctx).Error(), `template: tmpl:1: unexpected "}" in operand`)
} }
func TestRunPipeInvalidContentsSourceTemplate(t *testing.T) { func TestRunPipeInvalidContentsSourceTemplate(t *testing.T) {

View File

@ -148,9 +148,9 @@ nfpms:
dst: /etc/bar.conf dst: /etc/bar.conf
type: "config|noreplace" type: "config|noreplace"
# The src attribute also supports name templates # The src and dst attributes also supports name templates
- src: path/{{ .Os }}-{{ .Arch }}/bar.conf - src: path/{{ .Os }}-{{ .Arch }}/bar.conf
dst: /etc/foo/bar.conf dst: /etc/foo/bar-{{ .ProjectName }}.conf
# These files are not actually present in the package, but the file names # These files are not actually present in the package, but the file names
# are added to the package header. From the RPM directives documentation: # are added to the package header. From the RPM directives documentation: