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

feat: use conventional file names by default (#157)

Co-authored-by: Erik Geiser <erik.geiser@redteam-pentesting.de>
This commit is contained in:
Erik G 2020-07-09 15:16:04 +02:00 committed by GitHub
parent b93ff6f946
commit 4804199f0a
Signed by: GitHub
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 56 additions and 6 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"github.com/alecthomas/kingpin"
@ -25,8 +26,8 @@ var (
String()
pkgCmd = app.Command("pkg", "package based on the config file").Alias("package")
target = pkgCmd.Flag("target", "where to save the generated package").
Default("/tmp/foo.deb").
target = pkgCmd.Flag("target", "where to save the generated package (filename, folder or blank for current folder)").
Default("").
Short('t').
String()
packager = pkgCmd.Flag("packager", "which packager implementation to use").
@ -50,7 +51,6 @@ func main() {
if err := doPackage(*config, *target, *packager); err != nil {
kingpin.Fatalf(err.Error())
}
fmt.Printf("created package: %s\n", *target)
}
}
@ -58,12 +58,12 @@ func initFile(config string) error {
return ioutil.WriteFile(config, []byte(example), 0600)
}
func doPackage(path, target, packager string) error {
func doPackage(configPath, target, packager string) error {
if packager == "" {
fmt.Printf("guessing packager from target file extension...")
packager = filepath.Ext(target)[1:]
}
config, err := nfpm.ParseFile(path)
config, err := nfpm.ParseFile(configPath)
if err != nil {
return err
}
@ -85,13 +85,36 @@ func doPackage(path, target, packager string) error {
return err
}
if target == "" {
// if no target was specified create a package in
// current directory with a conventional file name
target = pkg.ConventionalFileName(info)
} else {
// if a directory was specified as target, create
// a package with conventional file name there
var stat os.FileInfo
stat, err = os.Stat(target)
if err == nil && stat.IsDir() {
target = path.Join(target, pkg.ConventionalFileName(info))
}
}
f, err := os.Create(target)
if err != nil {
return err
}
info.Target = target
return pkg.Package(info, f)
err = pkg.Package(info, f)
_ = f.Close()
if err != nil {
os.Remove(target)
return err
}
fmt.Printf("created package: %s\n", target)
return nil
}
const example = `# nfpm example config file

View File

@ -44,6 +44,19 @@ var Default = &Deb{}
// Deb is a deb packager implementation.
type Deb struct{}
// ConventionalFileName returns a file name according
// to the conventions for debian packages. See:
// https://manpages.debian.org/buster/dpkg-dev/dpkg-name.1.en.html
func (*Deb) ConventionalFileName(info *nfpm.Info) string {
arch, ok := archToDebian[info.Arch]
if !ok {
arch = info.Arch
}
// package_version_architecture.package-type
return fmt.Sprintf("%s_%s_%s.deb", info.Name, info.Version, arch)
}
// Package writes a new deb package to the given writer using the given info.
func (*Deb) Package(info *nfpm.Info, deb io.Writer) (err error) {
arch, ok := archToDebian[info.Arch]

View File

@ -81,6 +81,7 @@ func ParseFile(path string) (config Config, err error) {
// Packager represents any packager implementation.
type Packager interface {
Package(info *Info, w io.Writer) error
ConventionalFileName(info *Info) string
}
// Config contains the top level configuration for packages.

View File

@ -213,6 +213,10 @@ func TestListFilesToCopy(t *testing.T) {
type fakePackager struct{}
func (*fakePackager) ConventionalFileName(info *Info) string {
return ""
}
func (*fakePackager) Package(info *Info, w io.Writer) error {
return nil
}

View File

@ -45,6 +45,15 @@ func ensureValidArch(info *nfpm.Info) *nfpm.Info {
return info
}
// ConventionalFileName returns a file name according
// to the conventions for RPM packages. See:
// http://ftp.rpm.org/max-rpm/ch-rpm-file-format.html
func (*RPM) ConventionalFileName(info *nfpm.Info) string {
info = ensureValidArch(info)
// name-version-release.architecture.rpm
return fmt.Sprintf("%s_%s.%s.rpm", info.Name, info.Version, info.Arch)
}
// 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 (