From 999c9d3d0ee54883dc46ab820d7a565852f6e164 Mon Sep 17 00:00:00 2001 From: Erik Geiser Date: Thu, 9 Jul 2020 13:22:06 +0200 Subject: [PATCH] feat: Use conventional file names by default. --- cmd/nfpm/main.go | 35 +++++++++++++++++++++++++++++------ deb/deb.go | 13 +++++++++++++ nfpm.go | 1 + nfpm_test.go | 4 ++++ rpm/rpm.go | 9 +++++++++ 5 files changed, 56 insertions(+), 6 deletions(-) diff --git a/cmd/nfpm/main.go b/cmd/nfpm/main.go index 5f0422aa..e9040182 100644 --- a/cmd/nfpm/main.go +++ b/cmd/nfpm/main.go @@ -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 diff --git a/deb/deb.go b/deb/deb.go index 57ab15ce..a6abd471 100644 --- a/deb/deb.go +++ b/deb/deb.go @@ -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] diff --git a/nfpm.go b/nfpm.go index 0225b9ae..4b594823 100644 --- a/nfpm.go +++ b/nfpm.go @@ -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. diff --git a/nfpm_test.go b/nfpm_test.go index e1b0b9d5..483b0fae 100644 --- a/nfpm_test.go +++ b/nfpm_test.go @@ -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 } diff --git a/rpm/rpm.go b/rpm/rpm.go index 1dedb21d..c52c3da7 100644 --- a/rpm/rpm.go +++ b/rpm/rpm.go @@ -44,6 +44,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 (