From 98a59ec16dac754a3205c1596c56ff50c86c7783 Mon Sep 17 00:00:00 2001 From: Aaron Jacobs Date: Wed, 6 Apr 2022 13:38:10 -0400 Subject: [PATCH] feat: add support for additional fields in Debian control files (#491) Debian control files (DCF) have a number of optional fields [0] -- including "Bugs" and "Tags" -- that are used by many existing packages. The original fpm supports adding arbitrary fields to DCFs with the --deb-field flag [1], but until now it was not possible to do so with nfpm. This commit introduces a new deb-specific override, "fields", that simply translates key-value pairs from a map directly into the DCF. Given the following nfpm config file entry: deb: fields: Bugs: https://github.com/goreleaser/nfpm/issues The resulting DCF would contain the entry Bugs: https://github.com/goreleaser/nfpm/issues To avoid generating malformed DCFs, we simply ignore empty entries. This commit includes unit tests and updates to the website documentation. Closes #490. [0]: https://man7.org/linux/man-pages/man5/deb-control.5.html [1]: https://fpm.readthedocs.io/en/latest/packages/deb.html?highlight=changelog#deb-specific-command-line-flags Signed-off-by: Aaron Jacobs --- deb/deb.go | 5 +++++ deb/deb_test.go | 31 +++++++++++++++++++++++++++++++ deb/testdata/control3.golden | 10 ++++++++++ nfpm.go | 13 +++++++------ www/docs/configuration.md | 4 ++++ 5 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 deb/testdata/control3.golden diff --git a/deb/deb.go b/deb/deb.go index 2b97312..1985013 100644 --- a/deb/deb.go +++ b/deb/deb.go @@ -718,6 +718,11 @@ Homepage: {{.Info.Homepage}} {{- end }} {{- /* Mandatory fields */}} Description: {{multiline .Info.Description}} +{{- range $key, $value := .Info.Deb.Fields }} +{{- if $value }} +{{$key}}: {{$value}} +{{- end }} +{{- end }} ` type controlData struct { diff --git a/deb/deb_test.go b/deb/deb_test.go index 45b5905..4b2c1b5 100644 --- a/deb/deb_test.go +++ b/deb/deb_test.go @@ -1267,3 +1267,34 @@ func TestArches(t *testing.T) { require.Equal(t, "foo64", info.Arch) }) } + +func TestFields(t *testing.T) { + var w bytes.Buffer + require.NoError(t, writeControl(&w, controlData{ + Info: nfpm.WithDefaults(&nfpm.Info{ + Name: "foo", + Description: "Foo does things", + Priority: "extra", + Maintainer: "Carlos A Becker ", + Version: "v1.0.0", + Section: "default", + Homepage: "http://carlosbecker.com", + Overridables: nfpm.Overridables{ + Deb: nfpm.Deb{ + Fields: map[string]string{ + "Bugs": "https://github.com/goreleaser/nfpm/issues", + "Empty": "", + }, + }, + }, + }), + InstalledSize: 10, + })) + golden := "testdata/control3.golden" + if *update { + require.NoError(t, ioutil.WriteFile(golden, w.Bytes(), 0o600)) + } + bts, err := ioutil.ReadFile(golden) //nolint:gosec + require.NoError(t, err) + require.Equal(t, string(bts), w.String()) +} diff --git a/deb/testdata/control3.golden b/deb/testdata/control3.golden new file mode 100644 index 0000000..6100b70 --- /dev/null +++ b/deb/testdata/control3.golden @@ -0,0 +1,10 @@ +Package: foo +Version: 1.0.0 +Section: default +Priority: extra +Architecture: amd64 +Maintainer: Carlos A Becker +Installed-Size: 10 +Homepage: http://carlosbecker.com +Description: Foo does things +Bugs: https://github.com/goreleaser/nfpm/issues diff --git a/nfpm.go b/nfpm.go index dfa7224..42e6baf 100644 --- a/nfpm.go +++ b/nfpm.go @@ -321,12 +321,13 @@ type APKScripts struct { // Deb is custom configs that are only available on deb packages. type Deb struct { - Arch string `yaml:"arch,omitempty" jsonschema:"title=architecture in deb nomenclature"` - Scripts DebScripts `yaml:"scripts,omitempty" jsonschema:"title=scripts"` - Triggers DebTriggers `yaml:"triggers,omitempty" jsonschema:"title=triggers"` - Breaks []string `yaml:"breaks,omitempty" jsonschema:"title=breaks"` - Signature DebSignature `yaml:"signature,omitempty" jsonschema:"title=signature"` - Compression string `yaml:"compression,omitempty" jsonschema:"title=compression algorithm to be used,enum=gzip,enum=xz,enum=none,default=gzip"` + Arch string `yaml:"arch,omitempty" jsonschema:"title=architecture in deb nomenclature"` + Scripts DebScripts `yaml:"scripts,omitempty" jsonschema:"title=scripts"` + Triggers DebTriggers `yaml:"triggers,omitempty" jsonschema:"title=triggers"` + Breaks []string `yaml:"breaks,omitempty" jsonschema:"title=breaks"` + Signature DebSignature `yaml:"signature,omitempty" jsonschema:"title=signature"` + Compression string `yaml:"compression,omitempty" jsonschema:"title=compression algorithm to be used,enum=gzip,enum=xz,enum=none,default=gzip"` + Fields map[string]string `yaml:"fields,omitempty" jsonschema:"title=fields"` } type DebSignature struct { diff --git a/www/docs/configuration.md b/www/docs/configuration.md index ae227fc..281e64c 100644 --- a/www/docs/configuration.md +++ b/www/docs/configuration.md @@ -305,6 +305,10 @@ deb: # This will expand any env var you set in the field, eg key_id: ${DEB_SIGNING_KEY_ID} key_id: bc8acdd415bd80b3 + # Additional fields for the control file. Empty fields are ignored. + fields: + Bugs: https://github.com/goreleaser/nfpm/issues + apk: # apk specific architecture name that overrides "arch" without performing any replacements. apk_arch: armhf