Skip to content

Commit

Permalink
feat: Use conventional file names by default.
Browse files Browse the repository at this point in the history
  • Loading branch information
rtpt-erikgeiser committed Jul 9, 2020
1 parent 07a8534 commit 999c9d3
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 6 deletions.
35 changes: 29 additions & 6 deletions cmd/nfpm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"

"github.com/alecthomas/kingpin"
Expand All @@ -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").
Expand All @@ -50,20 +51,19 @@ func main() {
if err := doPackage(*config, *target, *packager); err != nil {
kingpin.Fatalf(err.Error())
}
fmt.Printf("created package: %s\n", *target)
}
}

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
}
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions deb/deb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions nfpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions nfpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
9 changes: 9 additions & 0 deletions rpm/rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit 999c9d3

Please sign in to comment.