1
1
Fork 0
mirror of https://github.com/goreleaser/nfpm synced 2024-05-24 05:56:16 +02:00

feat: support recommending packages

This commit is contained in:
Carlos Alexandro Becker 2018-02-18 17:13:47 -03:00
parent ce0f322386
commit 2d6378cbe4
No known key found for this signature in database
GPG Key ID: E61E2F7DC14AB940
8 changed files with 221 additions and 59 deletions

View File

@ -11,6 +11,8 @@ provides:
depends:
- foo
- bar
recommends:
- whatever
conflicts:
- not-foo
- not-bar

View File

@ -152,6 +152,7 @@ Installed-Size: {{.InstalledSize}}
Replaces: {{join .Info.Replaces}}
Provides: {{join .Info.Provides}}
Depends: {{join .Info.Depends}}
Recommends: {{join .Info.Recommends}}
Conflicts: {{join .Info.Conflicts}}
Homepage: {{.Info.Homepage}}
Description: {{.Info.Description}}
@ -172,13 +173,7 @@ func createControl(now time.Time, instSize int64, md5sums []byte, info nfpm.Info
defer compress.Close() // nolint: errcheck
var body bytes.Buffer
var tmpl = template.New("control")
tmpl.Funcs(template.FuncMap{
"join": func(strs []string) string {
return strings.Trim(strings.Join(strs, ", "), " ")
},
})
if err := template.Must(tmpl.Parse(controlTemplate)).Execute(&body, controlData{
if err := writeControl(&body, controlData{
Info: info,
InstalledSize: instSize / 1024,
}); err != nil {
@ -204,6 +199,16 @@ func createControl(now time.Time, instSize int64, md5sums []byte, info nfpm.Info
return buf.Bytes(), nil
}
func writeControl(w io.Writer, data controlData) error {
var tmpl = template.New("control")
tmpl.Funcs(template.FuncMap{
"join": func(strs []string) string {
return strings.Trim(strings.Join(strs, ", "), " ")
},
})
return template.Must(tmpl.Parse(controlTemplate)).Execute(w, data)
}
func newFileInsideTarGz(out *tar.Writer, name string, content []byte, now time.Time) error {
var header = tar.Header{
Name: name,

View File

@ -1,6 +1,8 @@
package deb
import (
"bytes"
"flag"
"io/ioutil"
"testing"
@ -8,33 +10,61 @@ import (
"github.com/stretchr/testify/assert"
)
var update = flag.Bool("update", false, "update .golden files")
var info = nfpm.WithDefaults(nfpm.Info{
Name: "foo",
Arch: "amd64",
Depends: []string{
"bash",
},
Recommends: []string{
"git",
},
Replaces: []string{
"svn",
},
Provides: []string{
"bzr",
},
Conflicts: []string{
"zsh",
},
Description: "Foo does things",
Priority: "extra",
Maintainer: "Carlos A Becker <pkg@carlosbecker.com>",
Version: "1.0.0",
Section: "default",
Homepage: "http://carlosbecker.com",
Vendor: "nope",
Files: map[string]string{
"../testdata/fake": "/usr/local/bin/fake",
},
ConfigFiles: map[string]string{
"../testdata/whatever.conf": "/etc/fake/fake.conf",
},
})
func TestDeb(t *testing.T) {
var err = Default.Package(
nfpm.WithDefaults(nfpm.Info{
Name: "foo",
Arch: "amd64",
Depends: []string{
"bash",
},
Description: "Foo does things",
Priority: "extra",
Maintainer: "Carlos A Becker <pkg@carlosbecker.com>",
Version: "1.0.0",
Section: "default",
Homepage: "http://carlosbecker.com",
Vendor: "nope",
Files: map[string]string{
"../testdata/fake": "/usr/local/bin/fake",
},
ConfigFiles: map[string]string{
"../testdata/whatever.conf": "/etc/fake/fake.conf",
},
}),
ioutil.Discard,
)
var err = Default.Package(info, ioutil.Discard)
assert.NoError(t, err)
}
func TestControl(t *testing.T) {
var w bytes.Buffer
assert.NoError(t, writeControl(&w, controlData{
Info: info,
InstalledSize: 10,
}))
var golden = "testdata/control.golden"
if *update {
ioutil.WriteFile(golden, w.Bytes(), 0655)
}
bts, err := ioutil.ReadFile(golden)
assert.NoError(t, err)
assert.Equal(t, string(bts), w.String())
}
func TestDebFileDoesNotExist(t *testing.T) {
var err = Default.Package(
nfpm.WithDefaults(nfpm.Info{

15
deb/testdata/control.golden vendored Normal file
View File

@ -0,0 +1,15 @@
Package: foo
Version: 1.0.0
Section: default
Priority: extra
Architecture: amd64
Maintainer: Carlos A Becker <pkg@carlosbecker.com>
Vendor: nope
Installed-Size: 10
Replaces: svn
Provides: bzr
Depends: bash
Recommends: git
Conflicts: zsh
Homepage: http://carlosbecker.com
Description: Foo does things

View File

@ -45,6 +45,7 @@ type Info struct {
Replaces []string `yaml:"replaces,omitempty"`
Provides []string `yaml:"provides,omitempty"`
Depends []string `yaml:"depends,omitempty"`
Recommends []string `yaml:"recommends,omitempty"`
Conflicts []string `yaml:"conflicts,omitempty"`
Maintainer string `yaml:"maintainer,omitempty"`
Description string `yaml:"description,omitempty"`

View File

@ -71,7 +71,14 @@ func (*RPM) Package(info nfpm.Info, w io.Writer) error {
}
func createSpec(info nfpm.Info, path string) error {
var body bytes.Buffer
file, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0655)
if err != nil {
return errors.Wrap(err, "failed to create spec")
}
return writeSpec(file, info)
}
func writeSpec(w io.Writer, info nfpm.Info) error {
var tmpl = template.New("spec")
tmpl.Funcs(template.FuncMap{
"join": func(strs []string) string {
@ -81,10 +88,10 @@ func createSpec(info nfpm.Info, path string) error {
return strings.Split(str, "\n")[0]
},
})
if err := template.Must(tmpl.Parse(specTemplate)).Execute(&body, info); err != nil {
if err := template.Must(tmpl.Parse(specTemplate)).Execute(w, info); err != nil {
return errors.Wrap(err, "failed to parse spec template")
}
return errors.Wrap(ioutil.WriteFile(path, body.Bytes(), 0644), "failed to write spec file")
return nil
}
type tempFiles struct {
@ -210,7 +217,7 @@ URL: {{ .Homepage }}
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root
{{ range $index, $element := .Replaces }}
Obsolotes: {{ . }}
Obsoletes: {{ . }}
{{ end }}
{{ range $index, $element := .Conflicts }}
@ -225,6 +232,10 @@ Provides: {{ . }}
Requires: {{ . }}
{{ end }}
{{ range $index, $element := .Recommends }}
Recommends: {{ . }}
{{ end }}
%description
{{ .Description }}

View File

@ -1,6 +1,8 @@
package rpm
import (
"bytes"
"flag"
"io/ioutil"
"os"
"testing"
@ -9,32 +11,57 @@ import (
"github.com/stretchr/testify/assert"
)
var update = flag.Bool("update", false, "update .golden files")
var info = nfpm.WithDefaults(nfpm.Info{
Name: "foo",
Arch: "amd64",
Depends: []string{
"bash",
},
Recommends: []string{
"git",
},
Replaces: []string{
"svn",
},
Provides: []string{
"bzr",
},
Conflicts: []string{
"zsh",
},
Description: "Foo does things",
Priority: "extra",
Maintainer: "Carlos A Becker <pkg@carlosbecker.com>",
Version: "1.0.0",
Section: "default",
Homepage: "http://carlosbecker.com",
Vendor: "nope",
License: "MIT",
Bindir: "/usr/local/bin",
Files: map[string]string{
"../testdata/fake": "/usr/local/bin/fake",
},
ConfigFiles: map[string]string{
"../testdata/whatever.conf": "/etc/fake/fake.conf",
},
})
func TestSpec(t *testing.T) {
var w bytes.Buffer
assert.NoError(t, writeSpec(&w, info))
var golden = "testdata/spec.golden"
if *update {
ioutil.WriteFile(golden, w.Bytes(), 0655)
}
bts, err := ioutil.ReadFile(golden)
assert.NoError(t, err)
assert.Equal(t, string(bts), w.String())
}
func TestRPM(t *testing.T) {
var err = Default.Package(
nfpm.WithDefaults(nfpm.Info{
Name: "foo",
Arch: "amd64",
Depends: []string{
"bash",
},
Description: "Foo does things",
Priority: "extra",
Maintainer: "Carlos A Becker <pkg@carlosbecker.com>",
Version: "1.0.0",
Section: "default",
Homepage: "http://carlosbecker.com",
Vendor: "nope",
License: "MIT",
Bindir: "/usr/local/bin",
Files: map[string]string{
"../testdata/fake": "/usr/local/bin/fake",
},
ConfigFiles: map[string]string{
"../testdata/whatever.conf": "/etc/fake/fake.conf",
},
}),
ioutil.Discard,
)
var err = Default.Package(info, ioutil.Discard)
assert.NoError(t, err)
}

71
rpm/testdata/spec.golden vendored Normal file
View File

@ -0,0 +1,71 @@
%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: Development/Tools
SOURCE0 : %{name}-%{version}.tar.gz
URL: http://carlosbecker.com
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root
Obsoletes: svn
Conflicts: zsh
Provides: bzr
Requires: bash
Recommends: git
%description
Foo does things
%prep
%setup -q
%build
# Empty section.
%install
rm -rf %{buildroot}
mkdir -p %{buildroot}
# 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
%changelog
# noop