Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: feat: Use conventional file names by default #157

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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