Skip to content

Commit

Permalink
fix: switch FileToCopy to Content and make the file package not internal
Browse files Browse the repository at this point in the history
  • Loading branch information
Dj Gilcrease committed Nov 25, 2020
1 parent b926a22 commit 00a8895
Show file tree
Hide file tree
Showing 33 changed files with 1,188 additions and 733 deletions.
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ linters:
- gci
- exhaustivestruct
- wrapcheck
- paralleltest
- godot
linters-settings:
maligned:
# print struct with more effective memory layout or not, false by default
Expand Down Expand Up @@ -39,6 +41,8 @@ linters-settings:
- style
- performance
issues:
exclude:
- composites
exclude-rules:
- text: "G104" # gosec G104 is caught by errcheck
linters:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export GOPROXY := https://proxy.golang.org,https://gocenter.io,direct
# Install all the build and lint dependencies
setup:
go mod download
go generate -v ./...
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s
git config core.hooksPath .githooks
.PHONY: setup

Expand Down
31 changes: 15 additions & 16 deletions apk/apk.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ import (
"time"

"github.com/goreleaser/nfpm"
"github.com/goreleaser/nfpm/internal/files"
"github.com/goreleaser/nfpm/files"
"github.com/goreleaser/nfpm/internal/sign"
)

const packagerName = "apk"

// nolint: gochecknoinits
func init() {
nfpm.Register(packagerName, Default)
Expand Down Expand Up @@ -103,6 +104,9 @@ func (a *Apk) ConventionalFileName(info *nfpm.Info) string {

// Package writes a new apk package to the given writer using the given info.
func (*Apk) Package(info *nfpm.Info, apk io.Writer) (err error) {
if err = nfpm.Validate(info); err != nil {
return err
}
arch, ok := archToAlpine[info.Arch]
if ok {
info.Arch = arch
Expand Down Expand Up @@ -392,14 +396,9 @@ func createBuilderData(info *nfpm.Info, sizep *int64) func(tw *tar.Writer) error
}
}

func createFilesInsideTarGz(info *nfpm.Info, tw *tar.Writer, created map[string]bool, sizep *int64) error {
filesToCopy, err := files.ExpandFiles(info.Files, info.DisableGlobbing)
if err != nil {
return err
}

for _, file := range filesToCopy {
if file.Packager != "" || file.Packager != packagerName {
func createFilesInsideTarGz(info *nfpm.Info, tw *tar.Writer, created map[string]bool, sizep *int64) (err error) {
for _, file := range info.Files {
if file.Packager != "" && file.Packager != packagerName {
continue
}

Expand All @@ -425,14 +424,14 @@ func createFilesInsideTarGz(info *nfpm.Info, tw *tar.Writer, created map[string]
return nil
}

func createSymlinkInsideTarGz(file *files.FileToCopy, out *tar.Writer) error {
func createSymlinkInsideTarGz(file *files.Content, out *tar.Writer) error {
return newItemInsideTarGz(out, []byte{}, &tar.Header{
Name: strings.TrimLeft(file.Source, "/"),
Linkname: file.Destination,
Typeflag: tar.TypeSymlink,
ModTime: time.Now(),
Format: tar.FormatGNU,
})
Name: strings.TrimLeft(file.Source, "/"),
Linkname: file.Destination,
Typeflag: tar.TypeSymlink,
ModTime: time.Now(),
Format: tar.FormatGNU,
})
}

func copyToTarAndDigest(src, dst string, tw *tar.Writer, sizep *int64, created map[string]bool) error {
Expand Down
63 changes: 52 additions & 11 deletions apk/apk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (
"errors"
"flag"
"fmt"
"github.com/goreleaser/nfpm/internal/files"
"io"
"io/ioutil"
"testing"

"github.com/goreleaser/nfpm/files"

"github.com/stretchr/testify/require"

"github.com/goreleaser/nfpm"
Expand All @@ -25,6 +26,7 @@ import (
// nolint: gochecknoglobals
var update = flag.Bool("update", false, "update apk .golden files")

// nolint:funlen
func exampleInfo() *nfpm.Info {
return nfpm.WithDefaults(&nfpm.Info{
Name: "foo",
Expand Down Expand Up @@ -62,23 +64,27 @@ func exampleInfo() *nfpm.Info {
"zsh",
"foobarsh",
},
Files: []*files.FileToCopy{
// nolint:govet
Files: files.Contents{
{
"../testdata/fake",
"/usr/local/bin/fake",
"",
0,
"/usr/local/bin/fake",
"",
"apk",
0,
},
{
"../testdata/whatever.conf",
"/usr/share/doc/fake/fake.txt",
"",
"apk",
0,
},
{
"../testdata/whatever.conf",
"/etc/fake/fake.conf",
"config",
"apk",
0,
},
},
Expand All @@ -91,7 +97,7 @@ func exampleInfo() *nfpm.Info {
}

func TestArchToAlpine(t *testing.T) {
verifyArch(t, "", "")
verifyArch(t, "", "x86_64")
verifyArch(t, "abc", "abc")
verifyArch(t, "386", "x86")
verifyArch(t, "amd64", "x86_64")
Expand All @@ -104,13 +110,20 @@ func TestArchToAlpine(t *testing.T) {
func verifyArch(t *testing.T, nfpmArch, expectedArch string) {
info := exampleInfo()
info.Arch = nfpmArch
info = nfpm.WithDefaults(info)
if err := nfpm.Validate(info); err != nil {
t.Fatal(err)
}

assert.NoError(t, Default.Package(info, ioutil.Discard))
assert.Equal(t, expectedArch, info.Arch)
}

func TestCreateBuilderData(t *testing.T) {
info := exampleInfo()
if err := nfpm.Validate(info); err != nil {
t.Fatal(err)
}
size := int64(0)
builderData := createBuilderData(info, &size)

Expand Down Expand Up @@ -152,8 +165,12 @@ func TestPathsToCreate(t *testing.T) {
func TestDefaultWithArch(t *testing.T) {
for _, arch := range []string{"386", "amd64"} {
arch := arch
info := exampleInfo()
if err := nfpm.Validate(info); err != nil {
t.Fatal(err)
}
t.Run(arch, func(t *testing.T) {
info := exampleInfo()
info := info
info.Arch = arch
var err = Default.Package(info, ioutil.Discard)
assert.NoError(t, err)
Expand Down Expand Up @@ -182,17 +199,21 @@ func TestFileDoesNotExist(t *testing.T) {
Depends: []string{
"bash",
},
Files: []*files.FileToCopy{
// nolint:govet
// ignore composites: `*github.com/goreleaser/nfpm/files.Content` composite literal uses unkeyed fields
Files: files.Contents{
{
"../testdata/fake",
"/usr/local/bin/fake",
"",
"apk",
0,
},
{
"../testdata/whatever.confzzz",
"/etc/fake/fake.conf",
"config",
"apk",
0,
},
},
Expand Down Expand Up @@ -228,6 +249,9 @@ func TestNoFiles(t *testing.T) {

func TestCreateBuilderControl(t *testing.T) {
info := exampleInfo()
if err := nfpm.Validate(info); err != nil {
t.Fatal(err)
}
size := int64(12345)
builderControl := createBuilderControl(info, size, sha256.New().Sum(nil))

Expand All @@ -253,6 +277,9 @@ func TestCreateBuilderControlScripts(t *testing.T) {
PreRemove: "../testdata/scripts/preremove.sh",
PostRemove: "../testdata/scripts/postremove.sh",
}
if err := nfpm.Validate(info); err != nil {
t.Fatal(err)
}

size := int64(12345)
builderControl := createBuilderControl(info, size, sha256.New().Sum(nil))
Expand All @@ -273,8 +300,12 @@ func TestCreateBuilderControlScripts(t *testing.T) {

func TestControl(t *testing.T) {
var w bytes.Buffer
info := exampleInfo()
if err := nfpm.Validate(info); err != nil {
t.Fatal(err)
}
assert.NoError(t, writeControl(&w, controlData{
Info: exampleInfo(),
Info: info,
InstalledSize: 10,
}))
var golden = "testdata/TestControl.golden"
Expand All @@ -291,6 +322,9 @@ func TestSignature(t *testing.T) {
info.APK.Signature.KeyFile = "../internal/sign/testdata/rsa.priv"
info.APK.Signature.KeyName = "testkey.rsa.pub"
info.APK.Signature.KeyPassphrase = "hunter2"
if err := nfpm.Validate(info); err != nil {
t.Fatal(err)
}

digest := sha1.New().Sum(nil) // nolint:gosec

Expand All @@ -311,6 +345,9 @@ func TestSignatureError(t *testing.T) {
info.APK.Signature.KeyFile = "../internal/sign/testdata/rsa.priv"
info.APK.Signature.KeyName = "testkey.rsa.pub"
info.APK.Signature.KeyPassphrase = "hunter2"
if err := nfpm.Validate(info); err != nil {
t.Fatal(err)
}

// wrong hash format
digest := sha256.New().Sum(nil)
Expand All @@ -327,8 +364,12 @@ func TestSignatureError(t *testing.T) {
func TestDisableGlobbing(t *testing.T) {
info := exampleInfo()
info.DisableGlobbing = true
info.Files = []*files.FileToCopy{
{"../testdata/{file}[", "/test/{file}[", "", 0},
// nolint:govet
info.Files = files.Contents{
{"../testdata/{file}[", "/test/{file}[", "", "apk", 0},
}
if err := nfpm.Validate(info); err != nil {
t.Fatal(err)
}

size := int64(0)
Expand Down
44 changes: 20 additions & 24 deletions deb/deb.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ import (
"github.com/goreleaser/chglog"

"github.com/goreleaser/nfpm"
"github.com/goreleaser/nfpm/internal/files"
"github.com/goreleaser/nfpm/files"
"github.com/goreleaser/nfpm/internal/sign"
)

const packagerName = "deb"

// nolint: gochecknoinits
func init() {
nfpm.Register("packagerName", Default)
Expand Down Expand Up @@ -80,6 +81,9 @@ var ErrInvalidSignatureType = errors.New("invalid signature type")

// Package writes a new deb package to the given writer using the given info.
func (*Deb) Package(info *nfpm.Info, deb io.Writer) (err error) { // nolint: funlen
if err = nfpm.Validate(info); err != nil {
return err
}
arch, ok := archToDebian[info.Arch]
if ok {
info.Arch = arch
Expand Down Expand Up @@ -190,27 +194,19 @@ func createDataTarGz(info *nfpm.Info) (dataTarGz, md5sums []byte, instSize int64
return buf.Bytes(), md5buf.Bytes(), instSize, nil
}

func createSymlinkInsideTarGz(file *files.FileToCopy, out *tar.Writer) error {
return newItemInsideTarGz(out, []byte{}, &tar.Header{
Name: normalizePath(file.Source),
Linkname: file.Destination,
Typeflag: tar.TypeSymlink,
ModTime: time.Now(),
Format: tar.FormatGNU,
})
func createSymlinkInsideTarGz(file *files.Content, out *tar.Writer) error {
return newItemInsideTarGz(out, []byte{}, &tar.Header{
Name: normalizePath(file.Source),
Linkname: file.Destination,
Typeflag: tar.TypeSymlink,
ModTime: time.Now(),
Format: tar.FormatGNU,
})
}

func createFilesInsideTarGz(info *nfpm.Info, out *tar.Writer, created map[string]bool) (bytes.Buffer, int64, error) {
var md5buf bytes.Buffer
var instSize int64

filesToCopy, err := files.ExpandFiles(info.Files, info.DisableGlobbing)
if err != nil {
return md5buf, 0, err
}

for _, file := range filesToCopy {
if file.Packager != "" || file.Packager != packagerName {
func createFilesInsideTarGz(info *nfpm.Info, out *tar.Writer, created map[string]bool) (md5buf bytes.Buffer, instSize int64, err error) {
for _, file := range info.Files {
if file.Packager != "" && file.Packager != packagerName {
continue
}
if err = createTree(out, file.Destination, created); err != nil {
Expand All @@ -224,11 +220,11 @@ func createFilesInsideTarGz(info *nfpm.Info, out *tar.Writer, created map[string
default:
var size int64 // declare early to avoid shadowing err
size, err = copyToTarAndDigest(out, &md5buf, file.Source, file.Destination)
if err != nil {
return md5buf, 0, err
}
instSize += size
}
if err != nil {
return md5buf, 0, err
}
}

if info.Changelog != "" {
Expand Down Expand Up @@ -540,7 +536,7 @@ func conffiles(info *nfpm.Info) []byte {
// nolint: prealloc
var confs []string
for _, file := range info.Files {
if file.Packager != "" || file.Packager != packagerName {
if file.Packager != "" && file.Packager != packagerName {
continue
}
switch file.Type {
Expand Down
Loading

0 comments on commit 00a8895

Please sign in to comment.