Skip to content

Commit

Permalink
ensure correct file modes
Browse files Browse the repository at this point in the history
  • Loading branch information
wg committed Jun 5, 2020
1 parent 8a44c62 commit f6d2b1a
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 31 deletions.
19 changes: 0 additions & 19 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"fmt"
"os"
"strconv"
"strings"

"github.com/jessevdk/go-flags"
Expand Down Expand Up @@ -45,24 +44,6 @@ func ParseArgs() (*Args, error) {
return args, err
}

func (a *Args) Validate() error {
for _, info := range a.Inputs.Config.Files {
s, err := os.Stat(info.File)
if err != nil || !s.Mode().IsRegular() {
return fmt.Errorf("'%s' is not a file", info.File)
}

if mode, err := strconv.ParseInt(info.Mode, 8, 0); err != nil {
mode := os.FileMode(mode)
path := info.File
if err := os.Chmod(path, mode); err != nil {
return errors.Wrapf(err, "chmod %s", mode)
}
}
}
return nil
}

func (a *Args) Packages() []Package {
wanted := map[Format]struct{}{}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/jessevdk/go-flags v1.4.0
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.6.0
github.com/twpayne/go-vfs v1.4.2
golang.org/x/tools v0.0.0-20200601175630-2caf76543d99 // indirect
gopkg.in/yaml.v2 v2.3.0
)
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/blakesmith/ar v0.0.0-20190502131153-809d4375e1fb h1:m935MPodAbYS46DG4pJSv7WO+VECIWUQ7OJYSoTrMh4=
github.com/blakesmith/ar v0.0.0-20190502131153-809d4375e1fb/go.mod h1:PkYb9DJNAwrSvRx5DYA+gUcOIgTGVMNkfSCbZM8cWpI=
github.com/bmatcuk/doublestar v1.3.0 h1:1jLE2y0VpSrOn/QR9G4f2RmrCtkM3AuATcWradjHUvM=
github.com/bmatcuk/doublestar v1.3.0/go.mod h1:wiQtGV+rzVYxB7WIlirSN++5HPtPlXEo9MEoZQC/PmE=
github.com/bombsimon/wsl/v3 v3.0.0 h1:w9f49xQatuaeTJFaNP4SpiWSR5vfT6IstPtM62JjcqA=
github.com/bombsimon/wsl/v3 v3.0.0/go.mod h1:st10JtZYLE4D5sC7b8xV4zTKZwAQjCH/Hy2Pm1FNZIc=
github.com/cavaliercoder/go-cpio v0.0.0-20180626203310-925f9528c45e h1:hHg27A0RSSp2Om9lubZpiMgVbvn39bsUmW9U5h0twqc=
Expand Down Expand Up @@ -140,6 +142,8 @@ github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgf
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hectane/go-acl v0.0.0-20190523051433-dfeb47f3e2ef h1:kKfJP3gHIuNfCZdSAbtKjDpkBwVnXh/SVMtxHourrDE=
github.com/hectane/go-acl v0.0.0-20190523051433-dfeb47f3e2ef/go.mod h1:xk/21OELzVCkl0NZCoB+eLISXe1p+YDiha8WaQDD1d8=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/imdario/mergo v0.3.9 h1:UauaLniWCFHWd+Jp9oCEkTBj8VO/9DKg3PV3VCNMDIg=
Expand Down Expand Up @@ -295,6 +299,8 @@ github.com/timakin/bodyclose v0.0.0-20190930140734-f7f2e9bca95e/go.mod h1:Qimiff
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/tommy-muehle/go-mnd v1.3.1-0.20200224220436-e6f9a994e8fa h1:RC4maTWLKKwb7p1cnoygsbKIgNlJqSYBeAFON3Ar8As=
github.com/tommy-muehle/go-mnd v1.3.1-0.20200224220436-e6f9a994e8fa/go.mod h1:dSUh0FtTP8VhvkL1S+gUR1OKd9ZnSaozuI6r3m6wOig=
github.com/twpayne/go-vfs v1.4.2 h1:sheXsgu25owG7JP+jRl7wfumyqaJjWfKG/dX34RIfgo=
github.com/twpayne/go-vfs v1.4.2/go.mod h1:kG5wCT9b5BWdFBHTKOoC4Mxba5NuiTyErJH1wuTIXIE=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/ulikunitz/xz v0.5.6 h1:jGHAfXawEGZQ3blwU5wnWKQJvAraT7Ftq9EXjnXYgt8=
Expand Down Expand Up @@ -359,6 +365,7 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190523142557-0e01d883c5c5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e h1:N7DeIrjYszNmSW409R3frPPwglRwMkXSBzwVbkOjLLA=
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
44 changes: 32 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"fmt"
"log"
"os"
"strconv"

"github.com/Knetic/govaluate"
"github.com/goreleaser/nfpm"
_ "github.com/goreleaser/nfpm/deb"
_ "github.com/goreleaser/nfpm/rpm"
"github.com/pkg/errors"
"github.com/twpayne/go-vfs"
)

type (
Expand Down Expand Up @@ -75,39 +77,35 @@ func main() {

log.Printf("pkg %s", BuildVersion)

if err := build(args); err != nil {
if err := build(args, vfs.HostOSFS); err != nil {
log.Fatalf("%+v", err)
os.Exit(1)
}
}

func build(args *Args) error {
if err := args.Validate(); err != nil {
return err
}

func build(args *Args, fs vfs.FS) error {
for _, pkg := range args.Packages() {
info, err := pkg.Info()
if err != nil {
return errors.WithStack(err)
}

log.Printf("building %s", info.Target)

if err := nfpm.Validate(info); err != nil {
return errors.WithStack(err)
}

pkg, err := pkg.Packager()
log.Printf("building %s", info.Target)

f, err := fs.Create(info.Target)
if err != nil {
return errors.WithStack(err)
}
defer f.Close()

f, err := os.Create(info.Target)
pkg, err := pkg.Prepare(fs)
if err != nil {
return errors.WithStack(err)
}
defer f.Close()

if err := pkg.Package(info, f); err != nil {
return errors.WithStack(err)
Expand Down Expand Up @@ -181,6 +179,8 @@ func (p *Package) Info() (*nfpm.Info, error) {
} else {
confs[info.File] = spec
}

p.Files[path] = info
}

return nfpm.WithDefaults(&nfpm.Info{
Expand All @@ -203,7 +203,27 @@ func (p *Package) Info() (*nfpm.Info, error) {
}), nil
}

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

mode, err := strconv.ParseInt(info.Mode, 8, 0)
if err != nil {
return nil, fmt.Errorf("invalid mode: '%s'", info.Mode)
}

if err := fs.Chmod(info.File, os.FileMode(mode)); err != nil {
return nil, errors.Wrapf(err, "chmod %s", info.Mode)
}
}

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

Expand Down
39 changes: 39 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/goreleaser/nfpm"
"github.com/stretchr/testify/assert"
"github.com/twpayne/go-vfs/vfst"
)

func TestPackageInfo(t *testing.T) {
Expand Down Expand Up @@ -155,3 +156,41 @@ func TestPackageTarget(t *testing.T) {
assert.NoError(err)
assert.Equal("test-1.0.0-1.x86_64.rpm", info.Target)
}

func TestPackagePrepare(t *testing.T) {
assert := assert.New(t)

fs, cleanup, err := vfst.NewTestFS(map[string]interface{}{
"bin0": &vfst.File{Perm: 0o777, Contents: []byte{}},
"bin1": &vfst.File{Perm: 0o777, Contents: []byte{}},
"cfg0": &vfst.File{Perm: 0o777},
})
if err != nil {
t.Error(err)
}
defer cleanup()

args := testArgs()
args.Inputs.Config.Files = map[string]File{
"bin0": File{
File: "/bin0",
},
"bin1": File{
File: "/bin1",
Mode: "0755",
User: "toor",
},
}

pkg := args.Packages()[0]

_, err = pkg.Info()
assert.NoError(err)
_, err = pkg.Prepare(fs)
assert.NoError(err)

vfst.RunTests(t, fs, "prepare",
vfst.TestPath("/bin0", vfst.TestModePerm(0o644)),
vfst.TestPath("/bin1", vfst.TestModePerm(0o755)),
)
}

0 comments on commit f6d2b1a

Please sign in to comment.