Repository: goreleaser/nfpm
Pull request #59 (2019-07-05 14:07:08)

Adding Prefix and Group tags
Prefix and Group tags added for RPMs only since those tags do not exist for deb packages. 

Also defined some macros for rpmbuild to make nfpm less dependent on the build environment.

caarlos0: any thing like this for deb?

if its only for RPM, maybe prefix the config with `RPM` or inside a RPM block to make it clearer 🤔
AdrianWennberg: The `Section` tag which is already implemented for deb-only seems to be similar to the `Group` tag for RPMs, but it does not make sense to use one tag for both since they have different naming conventions. 

For reference:
RPM packages by group: https://www.rpmfind.net/linux/RPM/Groups.html
Deb packages by section: https://packages.debian.org/stable/

Putting the new tags into an `RPM` block seems like a good solution to avoid confusion.
caarlos0: Yeah seems good to me! And the deb specific ones we can put under a Deb block too..

willing to make this change?
AdrianWennberg: Moving the `Section` tag under a Deb block might break current config files if people update nfpm without updating their config. Do you think this will be an issue for people?

Other that that, I'd be happy to make the change! 
AdrianWennberg: Oops forgot to actually create the package in the test.
codecov-io: # [Codecov](https://codecov.io/gh/goreleaser/nfpm/pull/59?src=pr&el=h1) Report
> Merging [#59](https://codecov.io/gh/goreleaser/nfpm/pull/59?src=pr&el=desc) into [master](https://codecov.io/gh/goreleaser/nfpm/commit/793979844f1ece5d5c319a0d276d80b88fdfb185?src=pr&el=desc) will **increase** coverage by `0.11%`.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/goreleaser/nfpm/pull/59/graphs/tree.svg?width=650&token=9QKGIxn7wg&height=150&src=pr)](https://codecov.io/gh/goreleaser/nfpm/pull/59?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master      #59      +/-   ##
==========================================
+ Coverage   71.57%   71.68%   +0.11%     
==========================================
  Files           5        5              
  Lines         496      498       +2     
==========================================
+ Hits          355      357       +2     
  Misses         87       87              
  Partials       54       54
```


| [Impacted Files](https://codecov.io/gh/goreleaser/nfpm/pull/59?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [nfpm.go](https://codecov.io/gh/goreleaser/nfpm/pull/59/diff?src=pr&el=tree#diff-bmZwbS5nbw==) | `89.65% <ø> (ø)` | :arrow_up: |
| [rpm/rpm.go](https://codecov.io/gh/goreleaser/nfpm/pull/59/diff?src=pr&el=tree#diff-cnBtL3JwbS5nbw==) | `73.41% <ø> (+0.31%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/goreleaser/nfpm/pull/59?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/goreleaser/nfpm/pull/59?src=pr&el=footer). Last update [7939798...2d6c27f](https://codecov.io/gh/goreleaser/nfpm/pull/59?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

AdrianWennberg: Hey Carlos,

I think this is merge ready now. Give me a shout if there is anything else you would like me to add.

Patch:
diff --git a/nfpm.go b/nfpm.go
index 156f0b5..7bef1d9 100644
--- a/nfpm.go
+++ b/nfpm.go
@@ -116,6 +116,7 @@ type Info struct {
 	Homepage     string `yaml:"homepage,omitempty"`
 	License      string `yaml:"license,omitempty"`
 	Bindir       string `yaml:"bindir,omitempty"`
+	RPM          RPM    `yaml:"rpm,omitempty"`
 }
 
 // Overridables contain the field which are overridable in a package
@@ -132,6 +133,11 @@ type Overridables struct {
 	Scripts      Scripts           `yaml:"scripts,omitempty"`
 }
 
+type RPM struct {
+	Group  string `yaml:"group,omitempty"`
+	Prefix string `yaml:"prefix,omitempty"`
+}
+
 // Scripts contains information about maintainer scripts for packages
 type Scripts struct {
 	PreInstall  string `yaml:"preinstall,omitempty"`
diff --git a/rpm/rpm.go b/rpm/rpm.go
index d40195e..c1f5fb3 100644
--- a/rpm/rpm.go
+++ b/rpm/rpm.go
@@ -72,6 +72,8 @@ func (*RPM) Package(info nfpm.Info, w io.Writer) error {
 		"--verbose",
 		"--define", fmt.Sprintf("_topdir %s", temps.Root),
 		"--define", fmt.Sprintf("_tmppath %s/tmp", temps.Root),
+		"--define", fmt.Sprintf("_rpmfilename %s", filepath.Join(info.Arch, filepath.Base(temps.RPM))),
+		"--define", fmt.Sprintf("_sourcedir %s", filepath.Dir(temps.Source)),
 		"--target", fmt.Sprintf("%s-unknown-%s", info.Arch, info.Platform),
 		"-ba",
 		"SPECS/" + info.Name + ".spec",
@@ -328,7 +330,11 @@ Release: 1
 {{- with .Info.License }}
 License: {{ . }}
 {{- end }}
+{{- with .Info.RPM.Group }}
+Group: {{ . }}
+{{- else }}
 Group: Development/Tools
+{{- end }}
 SOURCE0 : %{name}-%{version}.tar.gz
 {{- with .Info.Homepage }}
 URL: {{ . }}
@@ -337,6 +343,9 @@ URL: {{ . }}
 Packager: {{ . }}
 {{- end }}
 BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root
+{{- with .Info.RPM.Prefix }}
+Prefix: {{ . }}
+{{- end }}
 
 {{ range $index, $element := .Info.Replaces }}
 Obsoletes: {{ . }}


Test patch:
diff --git a/rpm/rpm_test.go b/rpm/rpm_test.go
index a0950f5..5ecafe9 100644
--- a/rpm/rpm_test.go
+++ b/rpm/rpm_test.go
@@ -94,6 +94,36 @@ func TestRPM(t *testing.T) {
 	assert.NoError(t, err)
 }
 
+func TestWithRPMTags(t *testing.T) {
+	var info = exampleInfo()
+	info.RPM = nfpm.RPM{
+		Group:  "default",
+		Prefix: "/usr",
+	}
+	var err = Default.Package(info, ioutil.Discard)
+	assert.NoError(t, err)
+}
+
+func TestRPMTagsSpec(t *testing.T) {
+	var info = exampleInfo()
+	info.RPM = nfpm.RPM{
+		Group:  "default",
+		Prefix: "/usr",
+	}
+
+	vs := rpmbuildVersion{Major: 4, Minor: 15, Patch: 2}
+	golden := "testdata/spec_4.15.x.golden"
+
+	var w bytes.Buffer
+	assert.NoError(t, writeSpec(&w, info, vs))
+	if *update {
+		require.NoError(t, ioutil.WriteFile(golden, w.Bytes(), 0655))
+	}
+	bts, err := ioutil.ReadFile(golden)
+	assert.NoError(t, err)
+	assert.Equal(t, string(bts), w.String())
+}
+
 func TestRPMVersionWithDash(t *testing.T) {
 	info := exampleInfo()
 	info.Version = "1.0.0-beta"
@@ -111,8 +141,8 @@ func TestRPMScripts(t *testing.T) {
 		scripts.Preun:  info.Scripts.PreRemove,
 		scripts.Postun: info.Scripts.PostRemove,
 	} {
-		data, err := ioutil.ReadFile(src)          //nolint:gosec
-		fmt.Sprintf("%s %s %s", actual, src, data) //nolint.govet
+		data, err := ioutil.ReadFile(src)         //nolint:gosec
+		fmt.Printf("%s %s %s", actual, src, data) //nolint.govet
 		assert.NoError(t, err)
 	}
 }
diff --git a/rpm/testdata/spec_4.15.x.golden b/rpm/testdata/spec_4.15.x.golden
new file mode 100644
index 0000000..e195d62
--- /dev/null
+++ b/rpm/testdata/spec_4.15.x.golden
@@ -0,0 +1,113 @@
+
+%define __spec_install_post %{nil}
+%define debug_package %{nil}
+%define __os_install_post %{_dbpath}/brp-compress
+%define _arch amd64
+%define _bindir /usr/local/bin
+
+Name: foo
+Summary: Foo does things
+Version: 1.0.0
+Release: 1
+License: MIT
+Group: default
+SOURCE0 : %{name}-%{version}.tar.gz
+URL: http://carlosbecker.com
+Packager: Carlos A Becker <pkg@carlosbecker.com>
+BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root
+Prefix: /usr
+
+
+Obsoletes: svn
+
+
+
+Conflicts: zsh
+
+
+
+Provides: bzr
+
+
+
+Requires: bash
+
+
+
+
+Recommends: git
+
+
+
+Suggests: bash
+
+
+
+%description
+Foo does things
+
+%prep
+%setup -q
+
+%build
+# Empty section.
+
+%install
+rm -rf %{buildroot}
+mkdir -vp %{buildroot}
+
+mkdir -vp %{buildroot}/var/log/whatever
+
+mkdir -vp %{buildroot}/usr/share/whatever
+
+
+# in builddir
+cp -a * %{buildroot}
+
+%clean
+rm -rf %{buildroot}
+
+%files
+%defattr(-,root,root,-)
+
+/usr/local/bin/fake
+
+%{_bindir}/*
+
+/etc/fake/fake.conf
+
+
+%config(noreplace) /etc/fake/fake.conf
+
+
+/var/log/whatever
+
+/usr/share/whatever
+
+
+%pre
+#!/bin/bash
+
+echo "Preinstall" > /dev/null
+
+
+%post
+#!/bin/bash
+
+echo "Postinstall" > /dev/null
+
+
+%preun
+#!/bin/bash
+
+echo "Preremove" > /dev/null
+
+
+%postun
+#!/bin/bash
+
+echo "Postremove" > /dev/null
+
+
+%changelog
+# noop