mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-03-17 20:47:50 +02:00
feat: Support per-format NFPM overrides
Support per-format NFPM overrides (as recently implemented in NFPM), plus a pair or Goreleaser specific overrides, for "Replacements" and "NameTemplate" (to address #640). This keeps backward compatibility with existing config files: settings at the root of the nfpm config section will still work as previously (which is also useful to define common/shared settings that we don't want to override anyway).
This commit is contained in:
parent
46aa41e27e
commit
edb77adc24
@ -123,23 +123,16 @@ type Release struct {
|
|||||||
|
|
||||||
// NFPM config
|
// NFPM config
|
||||||
type NFPM struct {
|
type NFPM struct {
|
||||||
NameTemplate string `yaml:"name_template,omitempty"`
|
NFPMOverridables `yaml:",inline"`
|
||||||
Replacements map[string]string `yaml:",omitempty"`
|
Overrides map[string]NFPMOverridables `yaml:"overrides,omitempty"`
|
||||||
|
|
||||||
Formats []string `yaml:",omitempty"`
|
Formats []string `yaml:",omitempty"`
|
||||||
Dependencies []string `yaml:",omitempty"`
|
Vendor string `yaml:",omitempty"`
|
||||||
Recommends []string `yaml:",omitempty"`
|
Homepage string `yaml:",omitempty"`
|
||||||
Suggests []string `yaml:",omitempty"`
|
Maintainer string `yaml:",omitempty"`
|
||||||
Conflicts []string `yaml:",omitempty"`
|
Description string `yaml:",omitempty"`
|
||||||
Vendor string `yaml:",omitempty"`
|
License string `yaml:",omitempty"`
|
||||||
Homepage string `yaml:",omitempty"`
|
Bindir string `yaml:",omitempty"`
|
||||||
Maintainer string `yaml:",omitempty"`
|
|
||||||
Description string `yaml:",omitempty"`
|
|
||||||
License string `yaml:",omitempty"`
|
|
||||||
Bindir string `yaml:",omitempty"`
|
|
||||||
Files map[string]string `yaml:",omitempty"`
|
|
||||||
ConfigFiles map[string]string `yaml:"config_files,omitempty"`
|
|
||||||
Scripts NFPMScripts `yaml:"scripts,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NFPMScripts is used to specify maintainer scripts
|
// NFPMScripts is used to specify maintainer scripts
|
||||||
@ -150,6 +143,19 @@ type NFPMScripts struct {
|
|||||||
PostRemove string `yaml:"postremove,omitempty"`
|
PostRemove string `yaml:"postremove,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NFPMOverridables is used to specify per package format settings
|
||||||
|
type NFPMOverridables struct {
|
||||||
|
NameTemplate string `yaml:"name_template,omitempty"`
|
||||||
|
Replacements map[string]string `yaml:",omitempty"`
|
||||||
|
Dependencies []string `yaml:",omitempty"`
|
||||||
|
Recommends []string `yaml:",omitempty"`
|
||||||
|
Suggests []string `yaml:",omitempty"`
|
||||||
|
Conflicts []string `yaml:",omitempty"`
|
||||||
|
Files map[string]string `yaml:",omitempty"`
|
||||||
|
ConfigFiles map[string]string `yaml:"config_files,omitempty"`
|
||||||
|
Scripts NFPMScripts `yaml:"scripts,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
// Sign config
|
// Sign config
|
||||||
type Sign struct {
|
type Sign struct {
|
||||||
Cmd string `yaml:"cmd,omitempty"`
|
Cmd string `yaml:"cmd,omitempty"`
|
||||||
|
@ -112,6 +112,28 @@ nfpm:
|
|||||||
postinstall: "scripts/postinstall.sh"
|
postinstall: "scripts/postinstall.sh"
|
||||||
preremove: "scripts/preremove.sh"
|
preremove: "scripts/preremove.sh"
|
||||||
postremove: "scripts/postremove.sh"
|
postremove: "scripts/postremove.sh"
|
||||||
|
|
||||||
|
# Some attributes can be overrided per package format.
|
||||||
|
overrides:
|
||||||
|
deb:
|
||||||
|
conflicts:
|
||||||
|
- subversion
|
||||||
|
dependencies:
|
||||||
|
- git
|
||||||
|
suggests:
|
||||||
|
- gitk
|
||||||
|
recommends:
|
||||||
|
- tig
|
||||||
|
rpm:
|
||||||
|
replacements:
|
||||||
|
amd64: x86_64
|
||||||
|
name_template: "{{ .ProjectName }}-{{ .Version }}-{{ .Arch }}"
|
||||||
|
files:
|
||||||
|
"tmp/man.gz": "/usr/share/man/man8/app.8.gz"
|
||||||
|
config_files:
|
||||||
|
"tmp/app_generated.conf": /etc/app-rpm.conf"
|
||||||
|
scripts:
|
||||||
|
preinstall: "scripts/preinstall-rpm.sh"
|
||||||
```
|
```
|
||||||
|
|
||||||
Note that GoReleaser will not install `rpmbuild` or any dependencies for you.
|
Note that GoReleaser will not install `rpmbuild` or any dependencies for you.
|
||||||
|
@ -9,6 +9,7 @@ import (
|
|||||||
|
|
||||||
"github.com/apex/log"
|
"github.com/apex/log"
|
||||||
"github.com/goreleaser/nfpm"
|
"github.com/goreleaser/nfpm"
|
||||||
|
"github.com/imdario/mergo"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
// blank imports here because the formats implementations need register
|
// blank imports here because the formats implementations need register
|
||||||
@ -16,6 +17,7 @@ import (
|
|||||||
_ "github.com/goreleaser/nfpm/deb"
|
_ "github.com/goreleaser/nfpm/deb"
|
||||||
_ "github.com/goreleaser/nfpm/rpm"
|
_ "github.com/goreleaser/nfpm/rpm"
|
||||||
|
|
||||||
|
"github.com/goreleaser/goreleaser/config"
|
||||||
"github.com/goreleaser/goreleaser/context"
|
"github.com/goreleaser/goreleaser/context"
|
||||||
"github.com/goreleaser/goreleaser/internal/artifact"
|
"github.com/goreleaser/goreleaser/internal/artifact"
|
||||||
"github.com/goreleaser/goreleaser/internal/filenametemplate"
|
"github.com/goreleaser/goreleaser/internal/filenametemplate"
|
||||||
@ -79,16 +81,35 @@ func doRun(ctx *context.Context) error {
|
|||||||
return g.Wait()
|
return g.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func mergeOverrides(ctx *context.Context, format string) (*config.NFPMOverridables, error) {
|
||||||
|
var overrided config.NFPMOverridables
|
||||||
|
if err := mergo.Merge(&overrided, ctx.Config.NFPM.NFPMOverridables); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
perFormat, ok := ctx.Config.NFPM.Overrides[format]
|
||||||
|
if ok {
|
||||||
|
err := mergo.Merge(&overrided, perFormat, mergo.WithOverride)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return &overrided, nil
|
||||||
|
}
|
||||||
|
|
||||||
func create(ctx *context.Context, format, arch string, binaries []artifact.Artifact) error {
|
func create(ctx *context.Context, format, arch string, binaries []artifact.Artifact) error {
|
||||||
|
overrided, err := mergeOverrides(ctx, format)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
name, err := filenametemplate.Apply(
|
name, err := filenametemplate.Apply(
|
||||||
ctx.Config.NFPM.NameTemplate,
|
overrided.NameTemplate,
|
||||||
filenametemplate.NewFields(ctx, ctx.Config.NFPM.Replacements, binaries...),
|
filenametemplate.NewFields(ctx, overrided.Replacements, binaries...),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
var files = map[string]string{}
|
var files = map[string]string{}
|
||||||
for k, v := range ctx.Config.NFPM.Files {
|
for k, v := range overrided.Files {
|
||||||
files[k] = v
|
files[k] = v
|
||||||
}
|
}
|
||||||
var log = log.WithField("package", name+"."+format)
|
var log = log.WithField("package", name+"."+format)
|
||||||
@ -114,17 +135,17 @@ func create(ctx *context.Context, format, arch string, binaries []artifact.Artif
|
|||||||
License: ctx.Config.NFPM.License,
|
License: ctx.Config.NFPM.License,
|
||||||
Bindir: ctx.Config.NFPM.Bindir,
|
Bindir: ctx.Config.NFPM.Bindir,
|
||||||
Overridables: nfpm.Overridables{
|
Overridables: nfpm.Overridables{
|
||||||
Conflicts: ctx.Config.NFPM.Conflicts,
|
Conflicts: overrided.Conflicts,
|
||||||
Depends: ctx.Config.NFPM.Dependencies,
|
Depends: overrided.Dependencies,
|
||||||
Recommends: ctx.Config.NFPM.Recommends,
|
Recommends: overrided.Recommends,
|
||||||
Suggests: ctx.Config.NFPM.Suggests,
|
Suggests: overrided.Suggests,
|
||||||
Files: files,
|
Files: files,
|
||||||
ConfigFiles: ctx.Config.NFPM.ConfigFiles,
|
ConfigFiles: overrided.ConfigFiles,
|
||||||
Scripts: nfpm.Scripts{
|
Scripts: nfpm.Scripts{
|
||||||
PreInstall: ctx.Config.NFPM.Scripts.PreInstall,
|
PreInstall: overrided.Scripts.PreInstall,
|
||||||
PostInstall: ctx.Config.NFPM.Scripts.PostInstall,
|
PostInstall: overrided.Scripts.PostInstall,
|
||||||
PreRemove: ctx.Config.NFPM.Scripts.PreRemove,
|
PreRemove: overrided.Scripts.PreRemove,
|
||||||
PostRemove: ctx.Config.NFPM.Scripts.PostRemove,
|
PostRemove: overrided.Scripts.PostRemove,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -33,10 +33,12 @@ func TestRunPipeInvalidFormat(t *testing.T) {
|
|||||||
var ctx = context.New(config.Project{
|
var ctx = context.New(config.Project{
|
||||||
ProjectName: "nope",
|
ProjectName: "nope",
|
||||||
NFPM: config.NFPM{
|
NFPM: config.NFPM{
|
||||||
Bindir: "/usr/bin",
|
Bindir: "/usr/bin",
|
||||||
NameTemplate: defaultNameTemplate,
|
Formats: []string{"nope"},
|
||||||
Formats: []string{"nope"},
|
NFPMOverridables: config.NFPMOverridables{
|
||||||
Files: map[string]string{},
|
NameTemplate: defaultNameTemplate,
|
||||||
|
Files: map[string]string{},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
ctx.Git = context.GitInfo{
|
ctx.Git = context.GitInfo{
|
||||||
@ -69,23 +71,32 @@ func TestRunPipe(t *testing.T) {
|
|||||||
ProjectName: "mybin",
|
ProjectName: "mybin",
|
||||||
Dist: dist,
|
Dist: dist,
|
||||||
NFPM: config.NFPM{
|
NFPM: config.NFPM{
|
||||||
Bindir: "/usr/bin",
|
Bindir: "/usr/bin",
|
||||||
NameTemplate: defaultNameTemplate,
|
Formats: []string{"deb", "rpm"},
|
||||||
Formats: []string{"deb", "rpm"},
|
Description: "Some description",
|
||||||
Dependencies: []string{"make"},
|
License: "MIT",
|
||||||
Recommends: []string{"svn"},
|
Maintainer: "me@me",
|
||||||
Suggests: []string{"bzr"},
|
Vendor: "asdf",
|
||||||
Conflicts: []string{"git"},
|
Homepage: "https://goreleaser.github.io",
|
||||||
Description: "Some description",
|
NFPMOverridables: config.NFPMOverridables{
|
||||||
License: "MIT",
|
NameTemplate: defaultNameTemplate,
|
||||||
Maintainer: "me@me",
|
Dependencies: []string{"make"},
|
||||||
Vendor: "asdf",
|
Recommends: []string{"svn"},
|
||||||
Homepage: "https://goreleaser.github.io",
|
Suggests: []string{"bzr"},
|
||||||
Files: map[string]string{
|
Conflicts: []string{"git"},
|
||||||
"./testdata/testfile.txt": "/usr/share/testfile.txt",
|
Files: map[string]string{
|
||||||
|
"./testdata/testfile.txt": "/usr/share/testfile.txt",
|
||||||
|
},
|
||||||
|
ConfigFiles: map[string]string{
|
||||||
|
"./testdata/testfile.txt": "/etc/nope.conf",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
ConfigFiles: map[string]string{
|
Overrides: map[string]config.NFPMOverridables{
|
||||||
"./testdata/testfile.txt": "/etc/nope.conf",
|
"rpm": config.NFPMOverridables{
|
||||||
|
ConfigFiles: map[string]string{
|
||||||
|
"./testdata/testfile.txt": "/etc/nope-rpm.conf",
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@ -111,8 +122,8 @@ func TestInvalidNameTemplate(t *testing.T) {
|
|||||||
Artifacts: artifact.New(),
|
Artifacts: artifact.New(),
|
||||||
Config: config.Project{
|
Config: config.Project{
|
||||||
NFPM: config.NFPM{
|
NFPM: config.NFPM{
|
||||||
NameTemplate: "{{.Foo}",
|
NFPMOverridables: config.NFPMOverridables{NameTemplate: "{{.Foo}"},
|
||||||
Formats: []string{"deb"},
|
Formats: []string{"deb"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -136,8 +147,10 @@ func TestCreateFileDoesntExist(t *testing.T) {
|
|||||||
ProjectName: "asd",
|
ProjectName: "asd",
|
||||||
NFPM: config.NFPM{
|
NFPM: config.NFPM{
|
||||||
Formats: []string{"deb", "rpm"},
|
Formats: []string{"deb", "rpm"},
|
||||||
Files: map[string]string{
|
NFPMOverridables: config.NFPMOverridables{
|
||||||
"testdata/testfile.txt": "/var/lib/test/testfile.txt",
|
Files: map[string]string{
|
||||||
|
"testdata/testfile.txt": "/var/lib/test/testfile.txt",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@ -191,8 +204,10 @@ func TestDefaultSet(t *testing.T) {
|
|||||||
var ctx = &context.Context{
|
var ctx = &context.Context{
|
||||||
Config: config.Project{
|
Config: config.Project{
|
||||||
NFPM: config.NFPM{
|
NFPM: config.NFPM{
|
||||||
Bindir: "/bin",
|
Bindir: "/bin",
|
||||||
NameTemplate: "foo",
|
NFPMOverridables: config.NFPMOverridables{
|
||||||
|
NameTemplate: "foo",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -200,3 +215,28 @@ func TestDefaultSet(t *testing.T) {
|
|||||||
assert.Equal(t, "/bin", ctx.Config.NFPM.Bindir)
|
assert.Equal(t, "/bin", ctx.Config.NFPM.Bindir)
|
||||||
assert.Equal(t, "foo", ctx.Config.NFPM.NameTemplate)
|
assert.Equal(t, "foo", ctx.Config.NFPM.NameTemplate)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestOverrides(t *testing.T) {
|
||||||
|
var ctx = &context.Context{
|
||||||
|
Config: config.Project{
|
||||||
|
NFPM: config.NFPM{
|
||||||
|
Bindir: "/bin",
|
||||||
|
NFPMOverridables: config.NFPMOverridables{
|
||||||
|
NameTemplate: "foo",
|
||||||
|
},
|
||||||
|
Overrides: map[string]config.NFPMOverridables{
|
||||||
|
"deb": config.NFPMOverridables{
|
||||||
|
NameTemplate: "bar",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
assert.NoError(t, Pipe{}.Default(ctx))
|
||||||
|
merged, err := mergeOverrides(ctx, "deb")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, "/bin", ctx.Config.NFPM.Bindir)
|
||||||
|
assert.Equal(t, "foo", ctx.Config.NFPM.NameTemplate)
|
||||||
|
assert.Equal(t, "bar", ctx.Config.NFPM.Overrides["deb"].NameTemplate)
|
||||||
|
assert.Equal(t, "bar", merged.NameTemplate)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user