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

refactor: add a method on Info to list files to copy to the package #136

Merged
merged 8 commits into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 13 additions & 27 deletions deb/deb.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/pkg/errors"

"github.com/goreleaser/nfpm"
"github.com/goreleaser/nfpm/glob"
)

// nolint: gochecknoinits
Expand Down Expand Up @@ -122,33 +121,20 @@ func createDataTarGz(info *nfpm.Info) (dataTarGz, md5sums []byte, instSize int64
func createFilesInsideTarGz(info *nfpm.Info, out *tar.Writer, created map[string]bool) (bytes.Buffer, int64, error) {
var md5buf bytes.Buffer
var instSize int64
for _, files := range []map[string]string{
info.Files,
info.ConfigFiles,
} {
for srcglob, dstroot := range files {
globbed, err := glob.Glob(srcglob, dstroot)
if err != nil {
return md5buf, 0, err
}
for src, dst := range globbed {
// when used as a lib, target may not be set.
// in that case, src will always have the empty sufix, and all
// files will be ignored.
if info.Target != "" && strings.HasSuffix(src, info.Target) {
fmt.Printf("skipping %s because it has the suffix %s", src, info.Target)
continue
}
if err := createTree(out, dst, created); err != nil {
return md5buf, 0, err
}
size, err := copyToTarAndDigest(out, &md5buf, src, dst)
if err != nil {
return md5buf, 0, err
}
instSize += size
}

files, err := info.FilesToCopy()
if err != nil {
return md5buf, 0, err
}
for _, file := range files {
if err := createTree(out, file.Destination, created); err != nil {
return md5buf, 0, err
}
size, err := copyToTarAndDigest(out, &md5buf, file.Source, file.Destination)
if err != nil {
return md5buf, 0, err
}
instSize += size
}
return md5buf, instSize, nil
}
Expand Down
44 changes: 44 additions & 0 deletions nfpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import (
"fmt"
"io"
"os"
"sort"
"strings"
"sync"

"github.com/Masterminds/semver/v3"
"github.com/imdario/mergo"
"github.com/pkg/errors"

"github.com/goreleaser/nfpm/glob"
"github.com/goreleaser/nfpm/internal/helpers"

"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -230,3 +233,44 @@ func WithDefaults(info *Info) *Info {

return info
}

// FileToCopy describes the source and destination of one file to copy into a
// package and whether it is a config file.
type FileToCopy struct {
Source string
Destination string
Config bool
}

// FilesToCopy lists all of the real files to be copied into the package.
func (info *Info) FilesToCopy() ([]FileToCopy, error) {
var files []FileToCopy
for i, filesMap := range []map[string]string{info.Files, info.ConfigFiles} {
for srcglob, dstroot := range filesMap {
globbed, err := glob.Glob(srcglob, dstroot)
if err != nil {
return nil, err
}
for src, dst := range globbed {
// avoid including a partial file with the name of the target in the target
// itself. when used as a lib, target may not be set. in that case, src will
// always have the empty sufix, and all files will be ignored.
if info.Target != "" && strings.HasSuffix(src, info.Target) {
fmt.Printf("skipping %s because it has the suffix %s", src, info.Target)
continue
}

files = append(files, FileToCopy{src, dst, i == 1})
}
}
}
// sort the files for reproducibility and general cleanliness
sort.Slice(files, func(i, j int) bool {
a, b := files[i], files[j]
if a.Source != b.Source {
return a.Source < b.Source
}
return a.Destination < b.Destination
})
return files, nil
}
25 changes: 25 additions & 0 deletions nfpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,31 @@ func TestOverrides(t *testing.T) {
assert.True(t, reflect.DeepEqual(&config.Info, info))
}

func TestListFilesToCopy(t *testing.T) {
info := &Info{
Overridables: Overridables{
ConfigFiles: map[string]string{
"testdata/whatever.conf": "/whatever",
},
Files: map[string]string{
"testdata/scripts/**/*": "/test",
},
},
}

files, err := info.FilesToCopy()
assert.NoError(t, err)

// all the input files described in the config in sorted order by source path
assert.Equal(t, []FileToCopy{
{"testdata/scripts/postinstall.sh", "/test/postinstall.sh", false},
{"testdata/scripts/postremove.sh", "/test/postremove.sh", false},
{"testdata/scripts/preinstall.sh", "/test/preinstall.sh", false},
{"testdata/scripts/preremove.sh", "/test/preremove.sh", false},
{"testdata/whatever.conf", "/whatever", true},
}, files)
}

type fakePackager struct{}

func (*fakePackager) Package(info *Info, w io.Writer) error {
Expand Down
34 changes: 6 additions & 28 deletions rpm/rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/pkg/errors"

"github.com/goreleaser/nfpm"
"github.com/goreleaser/nfpm/glob"
)

// nolint: gochecknoinits
Expand Down Expand Up @@ -216,36 +215,15 @@ func addEmptyDirsRPM(info *nfpm.Info, rpm *rpmpack.RPM) {
}

func createFilesInsideRPM(info *nfpm.Info, rpm *rpmpack.RPM) error {
copyFunc := func(files map[string]string, config bool) error {
for srcglob, dstroot := range files {
globbed, err := glob.Glob(srcglob, dstroot)
if err != nil {
return err
}
for src, dst := range globbed {
// when used as a lib, target may not be set.
// in that case, src will always have the empty sufix, and all
// files will be ignored.
if info.Target != "" && strings.HasSuffix(src, info.Target) {
fmt.Printf("skipping %s because it has the suffix %s", src, info.Target)
continue
}
err := copyToRPM(rpm, src, dst, config)
if err != nil {
return err
}
}
}

return nil
}
err := copyFunc(info.Files, false)
files, err := info.FilesToCopy()
if err != nil {
return err
}
err = copyFunc(info.ConfigFiles, true)
if err != nil {
return err
for _, file := range files {
err := copyToRPM(rpm, file.Source, file.Destination, file.Config)
if err != nil {
return err
}
}
return nil
}
Expand Down