Skip to content

Commit

Permalink
chore: add stricter linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Dj Gilcrease committed Oct 10, 2019
1 parent b47eac0 commit c2322d1
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 61 deletions.
4 changes: 4 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
FILES=$(git diff --staged --diff-filter=AM --no-renames --name-only)
export TEST_PATTERN=TestSimple
make fmt lint test && git add $FILES
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ nfpm.yaml
.DS_Store
bin
coverage.out
nfpm
/nfpm
35 changes: 35 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
linters:
enable-all: true
disable:
- godox
- wsl
linters-settings:
maligned:
# print struct with more effective memory layout or not, false by default
suggest-new: true
gocyclo:
# minimal code complexity to report, 30 by default (but we recommend 10-20)
min-complexity: 30
goimports:
# put imports beginning with prefix after 3rd-party packages;
# it's a comma-separated list of prefixes
local-prefixes: github.com/goreleaser/nfpm
govet:
check-shadowing: true
errcheck:
ignore: ^Close.*,fmt:.*,github.com/pkg/errors:^Wrap.*,os:^Setenv$
lll:
line-length: 200
golint:
min-confidence: .8
nakedret:
max-func-lines: 0
gocritic:
enabled-tags:
- style
- performance
issues:
exclude-rules:
- text: "G104" # gosec G104 is caught by errcheck
linters:
- gosec
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export GOPROXY := https://proxy.golang.org,https://gocenter.io,direct
setup:
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh
go mod tidy
git config core.hooksPath .githooks
.PHONY: setup


Expand All @@ -31,7 +32,7 @@ fmt:
.PHONY: fmt

lint: check
./bin/golangci-lint run --disable wsl --disable godox --enable-all ./...
./bin/golangci-lint run --exclude-use-default=false --fix -v
.PHONY: check

ci: build lint test
Expand All @@ -41,6 +42,12 @@ build:
go build -o nfpm ./cmd/nfpm/main.go
.PHONY: build

deps:
go get -u
go mod tidy
go mod verify
.PHONY: deps

todo:
@grep \
--exclude-dir=vendor \
Expand Down
1 change: 1 addition & 0 deletions acceptance/placeholder.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package acceptance contains acceptance tests
package acceptance

// This file only exists to make go test happy.
Expand Down
1 change: 1 addition & 0 deletions cmd/nfpm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"

"github.com/alecthomas/kingpin"

"github.com/goreleaser/nfpm"
_ "github.com/goreleaser/nfpm/deb"
_ "github.com/goreleaser/nfpm/rpm"
Expand Down
27 changes: 14 additions & 13 deletions deb/deb.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import (
"time"

"github.com/blakesmith/ar"
"github.com/pkg/errors"

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

// nolint: gochecknoinits
Expand All @@ -44,7 +45,7 @@ var Default = &Deb{}
type Deb struct{}

// 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) {
func (*Deb) Package(info *nfpm.Info, deb io.Writer) (err error) {
arch, ok := archToDebian[info.Arch]
if ok {
info.Arch = arch
Expand Down Expand Up @@ -87,7 +88,7 @@ func addArFile(w *ar.Writer, name string, body []byte) error {
return err
}

func createDataTarGz(info nfpm.Info) (dataTarGz, md5sums []byte, instSize int64, err error) {
func createDataTarGz(info *nfpm.Info) (dataTarGz, md5sums []byte, instSize int64, err error) {
var buf bytes.Buffer
var compress = gzip.NewWriter(&buf)
var out = tar.NewWriter(compress)
Expand Down Expand Up @@ -117,7 +118,7 @@ func createDataTarGz(info nfpm.Info) (dataTarGz, md5sums []byte, instSize int64,
return buf.Bytes(), md5buf.Bytes(), instSize, nil
}

func createFilesInsideTarGz(info nfpm.Info, out *tar.Writer, created map[string]bool) (bytes.Buffer, int64, error) {
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{
Expand All @@ -144,7 +145,7 @@ func createFilesInsideTarGz(info nfpm.Info, out *tar.Writer, created map[string]
return md5buf, instSize, nil
}

func createEmptyFoldersInsideTarGz(info nfpm.Info, out *tar.Writer, created map[string]bool) error {
func createEmptyFoldersInsideTarGz(info *nfpm.Info, out *tar.Writer, created map[string]bool) error {
for _, folder := range info.EmptyFolders {
// this .nope is actually not created, because createTree ignore the
// last part of the path, assuming it is a file.
Expand Down Expand Up @@ -191,7 +192,7 @@ func copyToTarAndDigest(tarw *tar.Writer, md5w io.Writer, src, dst string) (int6
return info.Size(), nil
}

func createControl(instSize int64, md5sums []byte, info nfpm.Info) (controlTarGz []byte, err error) {
func createControl(instSize int64, md5sums []byte, info *nfpm.Info) (controlTarGz []byte, err error) {
var buf bytes.Buffer
var compress = gzip.NewWriter(&buf)
var out = tar.NewWriter(compress)
Expand Down Expand Up @@ -241,8 +242,8 @@ func createControl(instSize int64, md5sums []byte, info nfpm.Info) (controlTarGz
return buf.Bytes(), nil
}

func newItemInsideTarGz(out *tar.Writer, content []byte, header tar.Header) error {
if err := out.WriteHeader(&header); err != nil {
func newItemInsideTarGz(out *tar.Writer, content []byte, header *tar.Header) error {
if err := out.WriteHeader(header); err != nil {
return errors.Wrapf(err, "cannot write header of %s file to control.tar.gz", header.Name)
}
if _, err := out.Write(content); err != nil {
Expand All @@ -252,7 +253,7 @@ func newItemInsideTarGz(out *tar.Writer, content []byte, header tar.Header) erro
}

func newFileInsideTarGz(out *tar.Writer, name string, content []byte) error {
return newItemInsideTarGz(out, content, tar.Header{
return newItemInsideTarGz(out, content, &tar.Header{
Name: filepath.ToSlash(name),
Size: int64(len(content)),
Mode: 0644,
Expand All @@ -262,7 +263,7 @@ func newFileInsideTarGz(out *tar.Writer, name string, content []byte) error {
})
}

func newScriptInsideTarGz(out *tar.Writer, path string, dest string) error {
func newScriptInsideTarGz(out *tar.Writer, path, dest string) error {
file, err := os.Open(path) //nolint:gosec
if err != nil {
return err
Expand All @@ -271,7 +272,7 @@ func newScriptInsideTarGz(out *tar.Writer, path string, dest string) error {
if err != nil {
return err
}
return newItemInsideTarGz(out, content, tar.Header{
return newItemInsideTarGz(out, content, &tar.Header{
Name: filepath.ToSlash(dest),
Size: int64(len(content)),
Mode: 0755,
Expand Down Expand Up @@ -323,7 +324,7 @@ func pathsToCreate(dst string) []string {
return result
}

func conffiles(info nfpm.Info) []byte {
func conffiles(info *nfpm.Info) []byte {
// nolint: prealloc
var confs []string
for _, dst := range info.ConfigFiles {
Expand Down Expand Up @@ -375,7 +376,7 @@ Description: {{.Info.Description}}
`

type controlData struct {
Info nfpm.Info
Info *nfpm.Info
InstalledSize int64
}

Expand Down
23 changes: 12 additions & 11 deletions deb/deb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ import (
"strconv"
"testing"

"github.com/goreleaser/nfpm"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/goreleaser/nfpm"
)

// nolint: gochecknoglobals
var update = flag.Bool("update", false, "update .golden files")

func exampleInfo() nfpm.Info {
return nfpm.WithDefaults(nfpm.Info{
func exampleInfo() *nfpm.Info {
return nfpm.WithDefaults(&nfpm.Info{
Name: "foo",
Arch: "amd64",
Description: "Foo does things",
Expand Down Expand Up @@ -119,7 +120,7 @@ func TestScripts(t *testing.T) {
func TestNoJoinsControl(t *testing.T) {
var w bytes.Buffer
assert.NoError(t, writeControl(&w, controlData{
Info: nfpm.WithDefaults(nfpm.Info{
Info: nfpm.WithDefaults(&nfpm.Info{
Name: "foo",
Arch: "amd64",
Description: "Foo does things",
Expand Down Expand Up @@ -153,7 +154,7 @@ func TestNoJoinsControl(t *testing.T) {

func TestDebFileDoesNotExist(t *testing.T) {
var err = Default.Package(
nfpm.WithDefaults(nfpm.Info{
nfpm.WithDefaults(&nfpm.Info{
Name: "foo",
Arch: "amd64",
Description: "Foo does things",
Expand Down Expand Up @@ -182,7 +183,7 @@ func TestDebFileDoesNotExist(t *testing.T) {

func TestDebNoFiles(t *testing.T) {
var err = Default.Package(
nfpm.WithDefaults(nfpm.Info{
nfpm.WithDefaults(&nfpm.Info{
Name: "foo",
Arch: "amd64",
Description: "Foo does things",
Expand All @@ -204,12 +205,12 @@ func TestDebNoFiles(t *testing.T) {
}

func TestDebNoInfo(t *testing.T) {
var err = Default.Package(nfpm.WithDefaults(nfpm.Info{}), ioutil.Discard)
var err = Default.Package(nfpm.WithDefaults(&nfpm.Info{}), ioutil.Discard)
assert.NoError(t, err)
}

func TestConffiles(t *testing.T) {
out := conffiles(nfpm.Info{
out := conffiles(&nfpm.Info{
Overridables: nfpm.Overridables{
ConfigFiles: map[string]string{
"fake": "/etc/fake",
Expand All @@ -236,7 +237,7 @@ func TestPathsToCreate(t *testing.T) {
func TestMinimalFields(t *testing.T) {
var w bytes.Buffer
assert.NoError(t, writeControl(&w, controlData{
Info: nfpm.WithDefaults(nfpm.Info{
Info: nfpm.WithDefaults(&nfpm.Info{
Name: "minimal",
Arch: "arm64",
Description: "Minimal does nothing",
Expand All @@ -257,7 +258,7 @@ func TestMinimalFields(t *testing.T) {
func TestDebEpoch(t *testing.T) {
var w bytes.Buffer
assert.NoError(t, writeControl(&w, controlData{
Info: nfpm.WithDefaults(nfpm.Info{
Info: nfpm.WithDefaults(&nfpm.Info{
Name: "withepoch",
Arch: "arm64",
Description: "Has an epoch added to it's version",
Expand All @@ -279,7 +280,7 @@ func TestDebEpoch(t *testing.T) {
func TestDebRules(t *testing.T) {
var w bytes.Buffer
assert.NoError(t, writeControl(&w, controlData{
Info: nfpm.WithDefaults(nfpm.Info{
Info: nfpm.WithDefaults(&nfpm.Info{
Name: "lala",
Arch: "arm64",
Description: "Has rules script",
Expand Down
2 changes: 1 addition & 1 deletion glob/glob.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func longestCommonPrefix(strs []string) string {
return lcp
}

func strlcp(a string, b string) string {
func strlcp(a, b string) string {
var min int
if len(a) > len(b) {
min = len(b)
Expand Down
28 changes: 15 additions & 13 deletions nfpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

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

"gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -53,15 +54,16 @@ func Parse(in io.Reader) (config Config, err error) {

config.Info.Version = os.ExpandEnv(config.Info.Version)
// parse the version as a semver so we can properly split the parts and support proper ordering for both rpm and deb
if v, err := semver.NewVersion(config.Info.Version); err == nil {
var v *semver.Version
if v, err = semver.NewVersion(config.Info.Version); err == nil {
config.Info.Version = fmt.Sprintf("%d.%d.%d", v.Major(), v.Minor(), v.Patch())
if config.Info.Release == "" {
config.Info.Release = v.Prerelease()
}
config.Info.Deb.VersionMetadata = v.Metadata()
}
err = config.Validate()
return
return config, err
}

// ParseFile decodes YAML data from a file path into a configuration struct
Expand All @@ -77,7 +79,7 @@ func ParseFile(path string) (config Config, err error) {

// Packager represents any packager implementation
type Packager interface {
Package(info Info, w io.Writer) error
Package(info *Info, w io.Writer) error
}

// Config contains the top level configuration for packages
Expand All @@ -88,21 +90,21 @@ type Config struct {

// Get returns the Info struct for the given packager format. Overrides
// for the given format are merged into the final struct
func (c *Config) Get(format string) (info Info, err error) {
func (c *Config) Get(format string) (info *Info, err error) {
info = &Info{}
// make a deep copy of info
if err = mergo.Merge(&info, c.Info); err != nil {
return
if err = mergo.Merge(info, c.Info); err != nil {
return nil, errors.Wrap(err, "Failed to merge config into info")
}
override, ok := c.Overrides[format]
if !ok {
// no overrides
return
return info, nil
}
err = mergo.Merge(&info.Overridables, override, mergo.WithOverride)
if err != nil {
return
if err = mergo.Merge(&info.Overridables, override, mergo.WithOverride); err != nil {
return nil, err
}
return
return info, nil
}

// Validate ensures that the config is well typed
Expand Down Expand Up @@ -178,7 +180,7 @@ type Scripts struct {
}

// Validate the given Info and returns an error if it is invalid.
func Validate(info Info) error {
func Validate(info *Info) error {
if info.Name == "" {
return fmt.Errorf("package name cannot be empty")
}
Expand All @@ -195,7 +197,7 @@ func Validate(info Info) error {
}

// WithDefaults set some sane defaults into the given Info
func WithDefaults(info Info) Info {
func WithDefaults(info *Info) *Info {
if info.Bindir == "" {
info.Bindir = "/usr/local/bin"
}
Expand Down
Loading

0 comments on commit c2322d1

Please sign in to comment.