mirror of
https://github.com/goreleaser/nfpm.git
synced 2026-06-19 08:05:04 +00:00
create cmd cli
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
|
||||
"github.com/alecthomas/kingpin"
|
||||
"github.com/apex/log"
|
||||
"github.com/caarlos0/pkg"
|
||||
"github.com/caarlos0/pkg/deb"
|
||||
)
|
||||
|
||||
var (
|
||||
app = kingpin.New("pkg", "packages apps")
|
||||
config = app.Flag("config", "config file").ExistingFile()
|
||||
format = app.Flag("format", "format to package").Default("deb").String()
|
||||
files = app.Flag("file", "file to add to the package, in the src=dst format").Required().Strings()
|
||||
target = app.Flag("target", "where to save the package").Required().String()
|
||||
)
|
||||
|
||||
func main() {
|
||||
kingpin.MustParse(app.Parse(os.Args[1:]))
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
||||
defer cancel()
|
||||
|
||||
bts, err := ioutil.ReadFile(*config)
|
||||
kingpin.FatalIfError(err, "%v")
|
||||
|
||||
var info pkg.Info
|
||||
kingpin.FatalIfError(yaml.Unmarshal(bts, &info), "%v")
|
||||
|
||||
var packager pkg.Packager
|
||||
switch *format {
|
||||
case "deb":
|
||||
packager, err = deb.New(ctx, info, *target)
|
||||
default:
|
||||
log.Fatalf("packager not found for %s", *format)
|
||||
}
|
||||
kingpin.FatalIfError(err, "%v")
|
||||
|
||||
for _, file := range *files {
|
||||
s := strings.Split(file, "=")
|
||||
kingpin.FatalIfError(packager.Add(s[0], s[1]), "%v")
|
||||
}
|
||||
|
||||
kingpin.FatalIfError(packager.Close(), "%v")
|
||||
}
|
||||
+8
-6
@@ -20,10 +20,11 @@ type Deb struct {
|
||||
ctx context.Context
|
||||
Info pkg.Info
|
||||
Path string
|
||||
Tmp string
|
||||
}
|
||||
|
||||
// New deb package with the given ctx and info
|
||||
func New(ctx context.Context, info pkg.Info) (*Deb, error) {
|
||||
func New(ctx context.Context, info pkg.Info, path string) (*Deb, error) {
|
||||
folder, err := ioutil.TempDir("", "deb")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -32,7 +33,8 @@ func New(ctx context.Context, info pkg.Info) (*Deb, error) {
|
||||
return &Deb{
|
||||
ctx: ctx,
|
||||
Info: info,
|
||||
Path: folder,
|
||||
Tmp: folder,
|
||||
Path: path,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -42,14 +44,14 @@ func (d *Deb) Add(src, dst string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Join(d.Path, filepath.Dir(dst)), 0755); err != nil {
|
||||
if err := os.MkdirAll(filepath.Join(d.Tmp, filepath.Dir(dst)), 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
bts, err := ioutil.ReadFile(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ioutil.WriteFile(filepath.Join(d.Path, dst), bts, info.Mode())
|
||||
return ioutil.WriteFile(filepath.Join(d.Tmp, dst), bts, info.Mode())
|
||||
}
|
||||
|
||||
// Close closes the package
|
||||
@@ -57,7 +59,7 @@ func (d *Deb) Close() error {
|
||||
if err := d.createControl(); err != nil {
|
||||
return err
|
||||
}
|
||||
cmd := exec.CommandContext(d.ctx, "dpkg-deb", "--build", d.Path, d.Info.Filename+".deb")
|
||||
cmd := exec.CommandContext(d.ctx, "dpkg-deb", "--build", d.Tmp, d.Path)
|
||||
bts, err := cmd.CombinedOutput()
|
||||
log.Println(string(bts))
|
||||
return err
|
||||
@@ -84,7 +86,7 @@ func (d *Deb) createControl() error {
|
||||
if err := template.Must(t.Parse(controlTemplate)).Execute(&b, d.Info); err != nil {
|
||||
return err
|
||||
}
|
||||
control := filepath.Join(d.Path, "DEBIAN", "control")
|
||||
control := filepath.Join(d.Tmp, "DEBIAN", "control")
|
||||
if err := os.Mkdir(filepath.Dir(control), 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -12,15 +12,14 @@ type Packager interface {
|
||||
|
||||
// Info contains information about the package
|
||||
type Info struct {
|
||||
Filename string
|
||||
Name string
|
||||
Arch string
|
||||
Version string
|
||||
Section string
|
||||
Priority string
|
||||
Depends []string
|
||||
Maintainer string
|
||||
Description string
|
||||
Vendor string
|
||||
Homepage string
|
||||
Name string `yaml:"name,omitempty"`
|
||||
Arch string `yaml:"arch,omitempty"`
|
||||
Version string `yaml:"version,omitempty"`
|
||||
Section string `yaml:"section,omitempty"`
|
||||
Priority string `yaml:"priority,omitempty"`
|
||||
Depends []string `yaml:"depends,omitempty"`
|
||||
Maintainer string `yaml:"maintainer,omitempty"`
|
||||
Description string `yaml:"description,omitempty"`
|
||||
Vendor string `yaml:"vendor,omitempty"`
|
||||
Homepage string `yaml:"homepage,omitempty"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user