Skip to content

Commit

Permalink
chore: improved cli
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Feb 16, 2018
1 parent 4b73ac9 commit 00335c9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 32 deletions.
58 changes: 44 additions & 14 deletions cmd/nfpm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,58 @@ import (
)

var (
app = kingpin.New("pkg", "packages apps")
config = app.Flag("config", "config file").ExistingFile()
format = app.Flag("format", "format to package").Default("deb").String()
target = app.Flag("target", "where to save the package").Required().String()
app = kingpin.New("nfpm", "not-fpm packages apps in some formats")
config = app.Flag("config", "config file").Default("nfpm.yaml").String()

pkgCmd = app.Command("pkg", "package based on the config file")
format = pkgCmd.Flag("format", "format to package").Default("deb").String()
target = pkgCmd.Flag("target", "where to save the package").Required().String()

initCmd = app.Command("init", "create an empty config file")
)

func main() {
kingpin.MustParse(app.Parse(os.Args[1:]))
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
case initCmd.FullCommand():
if err := initFile(*config); err != nil {
kingpin.Fatalf(err.Error())
}
fmt.Printf("created empty config file at %s, edit at will\n", *config)
case pkgCmd.FullCommand():
if err := doPackage(*config, *format, *target); err != nil {
kingpin.Fatalf(err.Error())
}
}
}

func initFile(config string) error {
yml, err := yaml.Marshal(nfpm.Info{})
if err != nil {
return err
}
return ioutil.WriteFile(config, yml, 0666)
}

bts, err := ioutil.ReadFile(*config)
kingpin.FatalIfError(err, "")
func doPackage(config, format, target string) error {
bts, err := ioutil.ReadFile(config)
if err != nil {
return err
}

var info nfpm.Info
kingpin.FatalIfError(yaml.Unmarshal(bts, &info), "%v")
err = yaml.Unmarshal(bts, &info)
if err != nil {
return err
}

pkg, err := nfpm.Get(*format)
pkg, err := nfpm.Get(format)
if err != nil {
kingpin.Fatalf(err.Error())
return err
}

f, err := os.Create(*target)
kingpin.FatalIfError(err, "")
kingpin.FatalIfError(pkg.Package(info, f), "")
fmt.Println("done:", *target)
f, err := os.Create(target)
if err != nil {
return err
}
return pkg.Package(info, f)
}
36 changes: 18 additions & 18 deletions nfpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,24 @@ type Packager interface {

// Info contains information about the package
type Info struct {
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"`
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"`
Name string `yaml:"name"`
Arch string `yaml:"arch"`
Platform string `yaml:"platform"`
Version string `yaml:"version"`
Section string `yaml:"section"`
Priority string `yaml:"priority"`
Replaces []string `yaml:"replaces"`
Provides []string `yaml:"provides"`
Depends []string `yaml:"depends"`
Conflicts []string `yaml:"conflicts"`
Maintainer string `yaml:"maintainer"`
Description string `yaml:"description"`
Vendor string `yaml:"vendor"`
Homepage string `yaml:"homepage"`
License string `yaml:"license"`
Bindir string `yaml:"bindir"`
Files map[string]string `yaml:"files"`
ConfigFiles map[string]string `yaml:"config_files"`
}

// WithDefaults set some sane defaults into the given Info
Expand Down

0 comments on commit 00335c9

Please sign in to comment.