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

102 lines
3.0 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-02-28 12:26:29 +01:00
"strings"
2018-02-12 16:50:25 +01:00
"sync"
)
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
// 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-01-04 13:49:15 +01:00
// Info contains information about the package
2018-01-04 13:31:22 +01:00
type Info struct {
2018-02-16 22:23:11 +01:00
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"`
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"`
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"`
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
}