1
1
Fork 0
mirror of https://github.com/goreleaser/nfpm synced 2024-05-18 20:46:19 +02:00
nfpm/nfpm.go

156 lines
4.2 KiB
Go
Raw Normal View History

2018-02-05 02:53:22 +01:00
// Package nfpm provides ways to package programs in some linux packaging
2018-01-04 13:49:15 +01:00
// formats.
2018-02-05 02:53:22 +01:00
package nfpm
2018-01-04 13:31:22 +01:00
2018-02-12 16:50:25 +01:00
import (
"fmt"
"io"
2018-04-10 01:04:06 +02:00
"os"
2018-02-28 12:26:29 +01:00
"strings"
2018-02-12 16:50:25 +01:00
"sync"
2018-04-10 01:04:06 +02:00
"github.com/imdario/mergo"
yaml "gopkg.in/yaml.v2"
2018-02-12 16:50:25 +01:00
)
var (
packagers = map[string]Packager{}
lock sync.Mutex
)
// Register a new packager for the given format
func Register(format string, p Packager) {
lock.Lock()
packagers[format] = p
lock.Unlock()
}
// Get a packager for the given format
func Get(format string) (Packager, error) {
p, ok := packagers[format]
if !ok {
return nil, fmt.Errorf("no packager registered for the format %s", format)
}
return p, nil
}
2018-01-10 14:16:07 +01:00
2018-04-10 01:04:06 +02:00
// Parse decodes YAML data from an io.Reader into a configuration struct
func Parse(in io.Reader) (config Config, err error) {
dec := yaml.NewDecoder(in)
dec.SetStrict(true)
err = dec.Decode(&config)
return
}
// ParseFile decodes YAML data from a file path into a configuration struct
func ParseFile(path string) (config Config, err error) {
var file *os.File
file, err = os.Open(path)
if err != nil {
return
}
2018-04-10 01:32:55 +02:00
defer file.Close() // nolint: errcheck
2018-04-10 01:04:06 +02:00
return Parse(file)
}
2018-01-10 14:16:07 +01:00
// Packager represents any packager implementation
type Packager interface {
2018-01-11 16:55:44 +01:00
Package(info Info, w io.Writer) error
2018-01-04 13:31:22 +01:00
}
2018-04-10 01:04:06 +02:00
// Config contains the top level configuration for packages
type Config struct {
Info `yaml:",inline"`
Overrides map[string]Overridables `yaml:"overrides,omitempty"`
2018-04-10 01:04:06 +02:00
}
// Get returns the Info struct for the given packager format. Overrides
// for the given format are merged into the final struct
func (c Config) Get(format string) (info Info, err error) {
// make a deep copy of info
2018-04-10 01:04:06 +02:00
if err = mergo.Merge(&info, c.Info); err != nil {
return
}
override, ok := c.Overrides[format]
2018-04-10 01:04:06 +02:00
if !ok {
// no overrides
return
}
err = mergo.Merge(&info.Overridables, override, mergo.WithOverride)
if err != nil {
2018-04-10 01:04:06 +02:00
return
}
return
}
// Info contains information about a single package
2018-01-04 13:31:22 +01:00
type Info struct {
Overridables `yaml:",inline"`
Name string `yaml:"name,omitempty"`
Arch string `yaml:"arch,omitempty"`
Platform string `yaml:"platform,omitempty"`
Version string `yaml:"version,omitempty"`
Section string `yaml:"section,omitempty"`
Priority string `yaml:"priority,omitempty"`
Maintainer string `yaml:"maintainer,omitempty"`
Description string `yaml:"description,omitempty"`
Vendor string `yaml:"vendor,omitempty"`
Homepage string `yaml:"homepage,omitempty"`
License string `yaml:"license,omitempty"`
Bindir string `yaml:"bindir,omitempty"`
}
// Overridables contain the field which are overridable in a package
type Overridables struct {
2018-02-16 22:23:11 +01:00
Replaces []string `yaml:"replaces,omitempty"`
Provides []string `yaml:"provides,omitempty"`
Depends []string `yaml:"depends,omitempty"`
2018-02-18 21:13:47 +01:00
Recommends []string `yaml:"recommends,omitempty"`
Suggests []string `yaml:"suggests,omitempty"`
2018-02-16 22:23:11 +01:00
Conflicts []string `yaml:"conflicts,omitempty"`
Files map[string]string `yaml:"files,omitempty"`
ConfigFiles map[string]string `yaml:"config_files,omitempty"`
2018-04-08 20:43:09 +02:00
Scripts Scripts `yaml:"scripts,omitempty"`
}
// Scripts contains information about maintainer scripts for packages
type Scripts struct {
PreInstall string `yaml:"preinstall,omitempty"`
PostInstall string `yaml:"postinstall,omitempty"`
PreRemove string `yaml:"preremove,omitempty"`
PostRemove string `yaml:"postremove,omitempty"`
2018-01-04 13:31:22 +01:00
}
2018-02-12 22:15:37 +01:00
2018-04-05 04:13:47 +02:00
// Validate the given Info and returns an error if it is invalid.
func Validate(info Info) error {
if info.Name == "" {
return fmt.Errorf("package name cannot be empty")
}
if info.Arch == "" {
return fmt.Errorf("package arch must be provided")
}
if info.Version == "" {
return fmt.Errorf("package version must be provided")
}
if len(info.Files)+len(info.ConfigFiles) == 0 {
return fmt.Errorf("no files were provided")
}
return nil
}
2018-02-12 22:15:37 +01:00
// WithDefaults set some sane defaults into the given Info
func WithDefaults(info Info) Info {
if info.Bindir == "" {
info.Bindir = "/usr/local/bin"
}
if info.Platform == "" {
info.Platform = "linux"
}
if info.Description == "" {
info.Description = "no description given"
}
2018-02-28 12:26:29 +01:00
info.Version = strings.TrimPrefix(info.Version, "v")
2018-02-12 22:15:37 +01:00
return info
}