1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-19 20:57:53 +02:00

feat: add support for name templates to NFPM contents (#2229)

This commit is contained in:
Marc Auberer 2021-05-20 19:21:48 +02:00 committed by GitHub
parent 46f452ecbe
commit 8db935ff15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 79 additions and 5 deletions

View File

@ -174,19 +174,32 @@ func create(ctx *context.Context, fpm config.NFPM, format, arch string, binaries
if err != nil { if err != nil {
return err return err
} }
name, err := tmpl.New(ctx). tmpl := tmpl.New(ctx).
WithArtifact(binaries[0], overridden.Replacements). WithArtifact(binaries[0], overridden.Replacements).
WithExtraFields(tmpl.Fields{ WithExtraFields(tmpl.Fields{
"Release": fpm.Release, "Release": fpm.Release,
"Epoch": fpm.Epoch, "Epoch": fpm.Epoch,
"PackageName": fpm.PackageName, "PackageName": fpm.PackageName,
}). })
Apply(overridden.FileNameTemplate) name, err := tmpl.Apply(overridden.FileNameTemplate)
if err != nil { if err != nil {
return err return err
} }
contents := append(files.Contents{}, overridden.Contents...) contents := files.Contents{}
for _, content := range overridden.Contents {
src, err := tmpl.Apply(content.Source)
if err != nil {
return err
}
contents = append(contents, &files.Content{
Source: src,
Destination: content.Destination,
Type: content.Type,
Packager: content.Packager,
FileInfo: content.FileInfo,
})
}
// FPM meta package should not contain binaries at all // FPM meta package should not contain binaries at all
if !fpm.Meta { if !fpm.Meta {

View File

@ -129,6 +129,10 @@ func TestRunPipe(t *testing.T) {
Destination: "/etc/nope2.conf", Destination: "/etc/nope2.conf",
Type: "symlink", Type: "symlink",
}, },
{
Source: "./testdata/testfile-{{ .Arch }}.txt",
Destination: "/etc/nope3.conf",
},
}, },
Replacements: map[string]string{ Replacements: map[string]string{
"linux": "Tux", "linux": "Tux",
@ -161,15 +165,24 @@ func TestRunPipe(t *testing.T) {
require.NotEmpty(t, format) require.NotEmpty(t, format)
require.Equal(t, pkg.Name, "foo_1.0.0_Tux_"+pkg.Goarch+"-10-20."+format) require.Equal(t, pkg.Name, "foo_1.0.0_Tux_"+pkg.Goarch+"-10-20."+format)
require.Equal(t, pkg.ExtraOr("ID", ""), "someid") require.Equal(t, pkg.ExtraOr("ID", ""), "someid")
require.ElementsMatch(t, []string{
"./testdata/testfile.txt",
"./testdata/testfile.txt",
"./testdata/testfile.txt",
"/etc/nope.conf",
"./testdata/testfile-" + pkg.Goarch + ".txt",
binPath,
}, sources(pkg.ExtraOr("Files", files.Contents{}).(files.Contents)))
require.ElementsMatch(t, []string{ require.ElementsMatch(t, []string{
"/usr/share/testfile.txt", "/usr/share/testfile.txt",
"/etc/nope.conf", "/etc/nope.conf",
"/etc/nope-rpm.conf", "/etc/nope-rpm.conf",
"/etc/nope2.conf", "/etc/nope2.conf",
"/etc/nope3.conf",
"/usr/bin/mybin", "/usr/bin/mybin",
}, destinations(pkg.ExtraOr("Files", files.Contents{}).(files.Contents))) }, destinations(pkg.ExtraOr("Files", files.Contents{}).(files.Contents)))
} }
require.Len(t, ctx.Config.NFPMs[0].Contents, 4, "should not modify the config file list") require.Len(t, ctx.Config.NFPMs[0].Contents, 5, "should not modify the config file list")
} }
func TestInvalidNameTemplate(t *testing.T) { func TestInvalidNameTemplate(t *testing.T) {
@ -201,6 +214,40 @@ func TestInvalidNameTemplate(t *testing.T) {
require.Contains(t, Pipe{}.Run(ctx).Error(), `template: tmpl:1: unexpected "}" in operand`) require.Contains(t, Pipe{}.Run(ctx).Error(), `template: tmpl:1: unexpected "}" in operand`)
} }
func TestRunPipeInvalidContentsSourceTemplate(t *testing.T) {
ctx := &context.Context{
Parallelism: runtime.NumCPU(),
Artifacts: artifact.New(),
Config: config.Project{
NFPMs: []config.NFPM{
{
NFPMOverridables: config.NFPMOverridables{
PackageName: "foo",
Contents: []*files.Content{
{
Source: "{{.asdsd}",
Destination: "testfile",
},
},
},
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",
},
})
require.EqualError(t, Pipe{}.Run(ctx), `template: tmpl:1: unexpected "}" in operand`)
}
func TestNoBuildsFound(t *testing.T) { func TestNoBuildsFound(t *testing.T) {
ctx := &context.Context{ ctx := &context.Context{
Parallelism: runtime.NumCPU(), Parallelism: runtime.NumCPU(),
@ -902,3 +949,11 @@ func TestSkipSign(t *testing.T) {
require.NoError(t, Pipe{}.Run(ctx)) require.NoError(t, Pipe{}.Run(ctx))
}) })
} }
func sources(contents files.Contents) []string {
result := make([]string, 0, len(contents))
for _, f := range contents {
result = append(result, f.Source)
}
return result
}

View File

@ -0,0 +1 @@
this is a test file

View File

@ -0,0 +1 @@
this is a test file

View File

@ -148,6 +148,10 @@ nfpms:
dst: /etc/bar.conf dst: /etc/bar.conf
type: "config|noreplace" type: "config|noreplace"
# The src attribute also supports name templates
- src: path/{{ .Os }}-{{ .Arch }}/bar.conf
dst: /etc/foo/bar.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:
# #