Skip to content

Commit

Permalink
support pre/post scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
wg committed Jun 16, 2020
1 parent f6d2b1a commit 83f8816
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 16 deletions.
31 changes: 25 additions & 6 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ type Inputs struct {
}

type Config struct {
Meta Meta `yaml:"meta"`
Files map[string]File `yaml:"files"`
Dirs []string `yaml:"dirs"`
Units []string `yaml:"units"`
Cond []Cond `yaml:"conditional"`
User string `yaml:"user"`
Meta Meta `yaml:"meta"`
Files map[string]File `yaml:"files"`
Dirs []string `yaml:"dirs"`
Units []string `yaml:"units"`
Scripts map[Phase]string `yaml:"scripts"`
Cond []Cond `yaml:"conditional"`
User string `yaml:"user"`
}

func ParseArgs() (*Args, error) {
Expand Down Expand Up @@ -71,6 +72,7 @@ func (a *Args) Packages() []Package {
Files: a.Inputs.Config.Files,
Dirs: a.Inputs.Config.Dirs,
Units: a.Inputs.Config.Units,
Scripts: a.Inputs.Config.Scripts,
Cond: a.Inputs.Config.Cond,
User: a.Inputs.Config.User,
})
Expand Down Expand Up @@ -124,3 +126,20 @@ func (c *Config) UnmarshalFlag(value string) error {

return nil
}

func (p *Phase) UnmarshalYAML(un func(interface{}) error) error {
var phase string

if err := un(&phase); err != nil {
return errors.WithStack(err)
}

switch Phase(phase) {
case PreInstall, PostInstall, PreRemove, PostRemove:
*p = Phase(phase)
default:
return fmt.Errorf("invalid phase '%s'", phase)
}

return nil
}
23 changes: 23 additions & 0 deletions args_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -60,3 +61,25 @@ func TestUnmarshalFormat(t *testing.T) {
assert.Equal(fmt, tmp)
}
}

func TestUnmarshalPhase(t *testing.T) {
assert := assert.New(t)
expect := map[string]Phase{
"pre-install": PreInstall,
"post-install": PostInstall,
"pre-remove": PreRemove,
"post-remove": PostRemove,
}

for value, phase := range expect {
tmp := Phase("")

err := tmp.UnmarshalYAML(func(v interface{}) error {
reflect.ValueOf(v).Elem().SetString(value)
return nil
})

assert.NoError(err)
assert.Equal(phase, tmp)
}
}
54 changes: 44 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
type (
Arch string
Format string
Phase string
)

const (
Expand All @@ -26,6 +27,11 @@ const (

DEB Format = "deb"
RPM Format = "rpm"

PreInstall Phase = "pre-install"
PostInstall Phase = "post-install"
PreRemove Phase = "pre-remove"
PostRemove Phase = "post-remove"
)

var (
Expand All @@ -42,6 +48,7 @@ type Package struct {
Files map[string]File
Dirs []string
Units []string
Scripts map[Phase]string
Cond []Cond
User string
}
Expand Down Expand Up @@ -133,10 +140,11 @@ func (p *Package) Filename() string {

func (p *Package) Info() (*nfpm.Info, error) {
var (
files = map[string]string{}
confs = map[string]string{}
dirs = append([]string(nil), p.Dirs...)
units = append([]string(nil), p.Units...)
files = map[string]string{}
confs = map[string]string{}
dirs = append([]string(nil), p.Dirs...)
units = append([]string(nil), p.Units...)
scripts nfpm.Scripts
)

for _, cond := range p.Cond {
Expand Down Expand Up @@ -183,6 +191,19 @@ func (p *Package) Info() (*nfpm.Info, error) {
p.Files[path] = info
}

for phase, file := range p.Scripts {
switch phase {
case PreInstall:
scripts.PreInstall = file
case PostInstall:
scripts.PostInstall = file
case PreRemove:
scripts.PreRemove = file
case PostRemove:
scripts.PostRemove = file
}
}

return nfpm.WithDefaults(&nfpm.Info{
Name: p.Name,
Arch: p.Format.Translate(p.Arch),
Expand All @@ -196,6 +217,7 @@ func (p *Package) Info() (*nfpm.Info, error) {
Files: files,
ConfigFiles: confs,
EmptyFolders: dirs,
Scripts: scripts,
SystemdUnits: units,
User: p.User,
},
Expand All @@ -204,14 +226,20 @@ func (p *Package) Info() (*nfpm.Info, error) {
}

func (p *Package) Prepare(fs vfs.FS) (nfpm.Packager, error) {
for _, info := range p.Files {
s, err := fs.Stat(info.File)
check := func(path string) error {
s, err := fs.Stat(path)
if err == nil && !s.Mode().IsRegular() {
return nil, fmt.Errorf("'%s' is not a file", info.File)
return fmt.Errorf("'%s' is not a file", path)
} else if os.IsNotExist(err) {
return nil, fmt.Errorf("'%s' does not exist", info.File)
} else if err != nil {
return nil, errors.WithStack(err)
return fmt.Errorf("'%s' does not exist", path)
} else {
return errors.WithStack(err)
}
}

for _, info := range p.Files {
if err := check(info.File); err != nil {
return nil, err
}

mode, err := strconv.ParseInt(info.Mode, 8, 0)
Expand All @@ -224,6 +252,12 @@ func (p *Package) Prepare(fs vfs.FS) (nfpm.Packager, error) {
}
}

for _, file := range p.Scripts {
if err := check(file); err != nil {
return nil, err
}
}

return nfpm.Get(string(p.Format))
}

Expand Down
12 changes: 12 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ func TestPackageOverridables(t *testing.T) {
args.Inputs.Config.Units = []string{
"unit0.service",
}
args.Inputs.Config.Scripts = map[Phase]string{
PreInstall: "script0",
PostInstall: "script1",
PreRemove: "script2",
PostRemove: "script3",
}

pkg := args.Packages()[0]

Expand All @@ -78,6 +84,12 @@ func TestPackageOverridables(t *testing.T) {
SystemdUnits: []string{
"unit0.service",
},
Scripts: nfpm.Scripts{
PreInstall: "script0",
PostInstall: "script1",
PreRemove: "script2",
PostRemove: "script3",
},
}, info.Overridables)
}

Expand Down

0 comments on commit 83f8816

Please sign in to comment.