1
1
mirror of https://github.com/goreleaser/nfpm synced 2024-11-19 07:34:48 +01:00
nfpm/rpm/rpm.go

257 lines
5.1 KiB
Go
Raw Normal View History

// Package rpm implements nfpm.Packager providing .rpm bindings using
// google/rpmpack.
2018-02-03 20:42:56 +01:00
package rpm
import (
"io"
"io/ioutil"
"os"
"strings"
"time"
2018-02-03 20:42:56 +01:00
"github.com/google/rpmpack"
"github.com/pkg/errors"
2018-02-05 02:53:22 +01:00
"github.com/goreleaser/nfpm"
"github.com/goreleaser/nfpm/glob"
2018-02-03 20:42:56 +01:00
)
2019-03-04 14:14:05 +01:00
// nolint: gochecknoinits
2018-02-12 16:50:25 +01:00
func init() {
nfpm.Register("rpm", Default)
}
2018-02-03 20:42:56 +01:00
2019-08-30 03:05:25 +02:00
// Default RPM packager
2019-03-04 14:14:05 +01:00
// nolint: gochecknoglobals
2018-02-03 20:42:56 +01:00
var Default = &RPM{}
// RPM is a RPM packager implementation
type RPM struct{}
2019-03-20 01:48:14 +01:00
// nolint: gochecknoglobals
2019-03-20 01:34:06 +01:00
var archToRPM = map[string]string{
"amd64": "x86_64",
"386": "i386",
"arm64": "aarch64",
}
func ensureValidArch(info *nfpm.Info) *nfpm.Info {
2019-03-20 01:34:06 +01:00
arch, ok := archToRPM[info.Arch]
if ok {
info.Arch = arch
}
return info
}
// Package writes a new RPM package to the given writer using the given info
func (*RPM) Package(info *nfpm.Info, w io.Writer) error {
var (
err error
meta *rpmpack.RPMMetaData
rpm *rpmpack.RPM
)
info = ensureValidArch(info)
if err = nfpm.Validate(info); err != nil {
return err
2018-02-03 20:42:56 +01:00
}
if meta, err = buildRPMMeta(info); err != nil {
return err
}
if rpm, err = rpmpack.NewRPM(*meta); err != nil {
return err
}
2018-03-06 01:02:26 +01:00
addEmptyDirsRPM(info, rpm)
if err = createFilesInsideRPM(info, rpm); err != nil {
return err
}
if err = addScriptFiles(info, rpm); err != nil {
return err
}
2018-04-08 23:37:25 +02:00
if err = rpm.Write(w); err != nil {
2018-04-08 23:37:25 +02:00
return err
}
2018-02-18 21:13:47 +01:00
return nil
2018-02-06 03:28:30 +01:00
}
func buildRPMMeta(info *nfpm.Info) (*rpmpack.RPMMetaData, error) {
var (
err error
provides,
depends,
replaces,
suggests,
conflicts rpmpack.Relations
)
if provides, err = toRelation(info.Provides); err != nil {
return nil, err
}
if depends, err = toRelation(info.Depends); err != nil {
return nil, err
}
if replaces, err = toRelation(info.Replaces); err != nil {
return nil, err
}
if suggests, err = toRelation(info.Suggests); err != nil {
return nil, err
}
if conflicts, err = toRelation(info.Conflicts); err != nil {
return nil, err
}
return &rpmpack.RPMMetaData{
Name: info.Name,
Summary: strings.Split(info.Description, "\n")[0],
Description: info.Description,
Version: info.Version,
Release: defaultTo(info.Release, "1"),
Arch: info.Arch,
OS: info.Platform,
Licence: info.License,
URL: info.Homepage,
Vendor: info.Vendor,
Packager: info.Maintainer,
Group: defaultTo(info.RPM.Group, "Development/Tools"),
Provides: provides,
Requires: depends,
Obsoletes: replaces,
Suggests: suggests,
Conflicts: conflicts,
Compressor: info.RPM.Compression,
}, nil
}
func defaultTo(in, def string) string {
if in == "" {
return def
}
return in
}
func toRelation(items []string) (rpmpack.Relations, error) {
relations := make(rpmpack.Relations, 0)
for idx := range items {
if err := relations.Set(items[idx]); err != nil {
return nil, err
}
}
return relations, nil
}
func addScriptFiles(info *nfpm.Info, rpm *rpmpack.RPM) error {
if info.Scripts.PreInstall != "" {
data, err := ioutil.ReadFile(info.Scripts.PreInstall)
if err != nil {
return err
2018-04-08 23:37:25 +02:00
}
rpm.AddPrein(string(data))
2018-04-08 23:37:25 +02:00
}
if info.Scripts.PreRemove != "" {
data, err := ioutil.ReadFile(info.Scripts.PreRemove)
if err != nil {
return err
}
rpm.AddPreun(string(data))
2018-02-16 02:58:37 +01:00
}
if info.Scripts.PostInstall != "" {
data, err := ioutil.ReadFile(info.Scripts.PostInstall)
if err != nil {
return err
}
rpm.AddPostin(string(data))
2018-02-16 02:58:37 +01:00
}
if info.Scripts.PostRemove != "" {
data, err := ioutil.ReadFile(info.Scripts.PostRemove)
if err != nil {
return err
2018-02-06 03:28:30 +01:00
}
rpm.AddPostun(string(data))
2018-02-06 03:28:30 +01:00
}
2018-02-06 03:28:30 +01:00
return nil
}
func addEmptyDirsRPM(info *nfpm.Info, rpm *rpmpack.RPM) {
for _, dir := range info.EmptyFolders {
rpm.AddFile(
rpmpack.RPMFile{
Name: dir,
Mode: uint(040755),
MTime: uint32(time.Now().Unix()),
},
)
}
}
func createFilesInsideRPM(info *nfpm.Info, rpm *rpmpack.RPM) error {
copyFunc := func(files map[string]string, config bool) error {
for srcglob, dstroot := range files {
globbed, err := glob.Glob(srcglob, dstroot)
if err != nil {
2018-02-16 03:12:54 +01:00
return err
2018-02-06 03:28:30 +01:00
}
for src, dst := range globbed {
err := copyToRPM(rpm, src, dst, config)
if err != nil {
return err
}
}
2018-02-06 03:28:30 +01:00
}
return nil
}
err := copyFunc(info.Files, false)
if err != nil {
return err
}
err = copyFunc(info.ConfigFiles, true)
if err != nil {
return err
2018-02-06 03:28:30 +01:00
}
2018-02-16 02:58:37 +01:00
return nil
2018-02-06 03:28:30 +01:00
}
func copyToRPM(rpm *rpmpack.RPM, src, dst string, config bool) error {
file, err := os.OpenFile(src, os.O_RDONLY, 0600) //nolint:gosec
2018-02-16 03:12:54 +01:00
if err != nil {
return errors.Wrap(err, "could not add file to the archive")
}
// don't care if it errs while closing...
2018-02-16 03:12:54 +01:00
defer file.Close() // nolint: errcheck
info, err := file.Stat()
if err != nil {
return err
}
if info.IsDir() {
// TODO: this should probably return an error
2018-02-16 03:12:54 +01:00
return nil
}
data, err := ioutil.ReadAll(file)
if err != nil {
return err
2018-02-16 03:12:54 +01:00
}
2018-02-03 20:42:56 +01:00
rpmFile := rpmpack.RPMFile{
Name: dst,
Body: data,
Mode: uint(info.Mode()),
MTime: uint32(info.ModTime().Unix()),
}
if config {
rpmFile.Type = rpmpack.ConfigFile
}
rpm.AddFile(rpmFile)
2018-02-03 20:42:56 +01:00
return nil
}