From c94b6a14335a0eb799fee972065004883e2c7e5e Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 29 Mar 2025 08:57:48 -0300 Subject: [PATCH] fix: lint issues, modernize codebase (#922) --- .golangci.yml | 74 +++++++++++++++++++++++------------- arch/arch.go | 8 +--- deb/deb.go | 6 +-- deprecation/deprecation.go | 2 +- files/files.go | 2 +- files/files_test.go | 4 +- files/fs.go | 9 ++--- internal/glob/glob.go | 11 ++---- internal/glob/glob_test.go | 4 +- internal/sign/pgp.go | 2 +- ipk/ipk_test.go | 2 +- nfpm.go | 77 +++++++++++++++++++------------------- nfpm_test.go | 20 +++++----- rpm/rpm_test.go | 8 ++-- 14 files changed, 119 insertions(+), 110 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 465887b..28902d4 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,36 +1,56 @@ +version: "2" run: - go: "1.20" - timeout: 5m + go: "1.24" linters: enable: + - depguard + - forbidigo + - misspell + - revive + - tagliatelle + - testifylint - thelper - - gofumpt - tparallel - unconvert + - usetesting - unparam - wastedassign - - revive - - forbidigo - - tagliatelle - - misspell - - depguard - - testifylint -linters-settings: - forbidigo: - forbid: - - 'ioutil\.*' - tagliatelle: - case: - use-field-name: false + settings: + depguard: rules: - yaml: snake - json: snake - depguard: - rules: - main: - deny: - - pkg: "github.com/pkg/errors" - desc: "use stdlib instead" - testifylint: - disable: - - encoded-compare + main: + deny: + - pkg: github.com/pkg/errors + desc: use stdlib instead + forbidigo: + forbid: + - pattern: ioutil\.* + tagliatelle: + case: + rules: + json: snake + yaml: snake + use-field-name: false + testifylint: + disable: + - encoded-compare + exclusions: + generated: lax + presets: + - comments + - common-false-positives + - legacy + - std-error-handling + paths: + - third_party$ + - builtin$ + - examples$ +formatters: + enable: + - gofumpt + exclusions: + generated: lax + paths: + - third_party$ + - builtin$ + - examples$ diff --git a/arch/arch.go b/arch/arch.go index df4ced1..e193caa 100644 --- a/arch/arch.go +++ b/arch/arch.go @@ -10,6 +10,7 @@ import ( "fmt" "io" "os" + "slices" "strconv" "strings" "time" @@ -107,12 +108,7 @@ func mapValidChar(r rune) rune { // isOneOf checks whether a rune is one of the runes in rr func isOneOf(r rune, rr ...rune) bool { - for _, char := range rr { - if r == char { - return true - } - } - return false + return slices.Contains(rr, r) } // Package writes a new archlinux package to the given writer using the given info. diff --git a/deb/deb.go b/deb/deb.go index a2f8efb..3a06254 100644 --- a/deb/deb.go +++ b/deb/deb.go @@ -607,15 +607,15 @@ func createControl(instSize int64, md5sums []byte, info *nfpm.Info) (controlTarG mode: 0o755, }, "rules": { - fileName: info.Overridables.Deb.Scripts.Rules, + fileName: info.Deb.Scripts.Rules, mode: 0o755, }, "templates": { - fileName: info.Overridables.Deb.Scripts.Templates, + fileName: info.Deb.Scripts.Templates, mode: 0o644, }, "config": { - fileName: info.Overridables.Deb.Scripts.Config, + fileName: info.Deb.Scripts.Config, mode: 0o755, }, } diff --git a/deprecation/deprecation.go b/deprecation/deprecation.go index aab9ada..d4a2d95 100644 --- a/deprecation/deprecation.go +++ b/deprecation/deprecation.go @@ -26,6 +26,6 @@ func Println(s string) { } // Printf printfs the given string to the Noticer. -func Printf(format string, a ...interface{}) { +func Printf(format string, a ...any) { fmt.Fprintf(Noticer, format, a...) } diff --git a/files/files.go b/files/files.go index fe0856e..a3b74a8 100644 --- a/files/files.go +++ b/files/files.go @@ -187,7 +187,7 @@ func (c *Content) IsDir() bool { } // Sys to part of the os.FileInfo interface -func (c *Content) Sys() interface{} { +func (c *Content) Sys() any { return nil } diff --git a/files/files_test.go b/files/files_test.go index 6474687..c7f021c 100644 --- a/files/files_test.go +++ b/files/files_test.go @@ -282,7 +282,7 @@ contents: errs := make(chan error, 10) t.Cleanup(func() { close(errs) }) var wg sync.WaitGroup - for i := 0; i < 10; i++ { + for range 10 { wg.Add(1) go func() { defer wg.Done() @@ -298,7 +298,7 @@ contents: } wg.Wait() - for i := 0; i < 10; i++ { + for range 10 { require.NoError(t, <-errs) } } diff --git a/files/fs.go b/files/fs.go index 47980ce..d6add66 100644 --- a/files/fs.go +++ b/files/fs.go @@ -1,13 +1,10 @@ package files +import "slices" + func ownedByFilesystem(path string) bool { p := ToNixPath(path) - for _, pp := range append(fsPaths, logrotatePaths...) { - if p == pp { - return true - } - } - return false + return slices.Contains(append(fsPaths, logrotatePaths...), p) } // yum install yum-utils diff --git a/internal/glob/glob.go b/internal/glob/glob.go index b78fb05..08d08b9 100644 --- a/internal/glob/glob.go +++ b/internal/glob/glob.go @@ -29,18 +29,13 @@ func longestCommonPrefix(strs []string) string { } func strlcp(a, b string) string { - var min int - if len(a) > len(b) { - min = len(b) - } else { - min = len(a) - } - for i := 0; i < min; i++ { + minlen := min(len(a), len(b)) + for i := range minlen { if a[i] != b[i] { return a[0:i] } } - return a[0:min] + return a[0:minlen] } // ErrGlobNoMatch happens when no files matched the given glob. diff --git a/internal/glob/glob_test.go b/internal/glob/glob_test.go index e06fd9d..a1062cd 100644 --- a/internal/glob/glob_test.go +++ b/internal/glob/glob_test.go @@ -22,7 +22,7 @@ func TestLongestCommonPrefix(t *testing.T) { empty := []string{} lcp2 := longestCommonPrefix(empty) - require.Equal(t, "", lcp2) + require.Empty(t, lcp2) unique := []string{ "every", @@ -35,7 +35,7 @@ func TestLongestCommonPrefix(t *testing.T) { } lcp3 := longestCommonPrefix(unique) - require.Equal(t, "", lcp3) + require.Empty(t, lcp3) } func TestGlob(t *testing.T) { diff --git a/internal/sign/pgp.go b/internal/sign/pgp.go index c30183c..6be282e 100644 --- a/internal/sign/pgp.go +++ b/internal/sign/pgp.go @@ -254,7 +254,7 @@ func readSigningKey(keyFile, passphrase string) (*openpgp.Entity, error) { } func isASCII(s []byte) bool { - for i := 0; i < len(s); i++ { + for i := range s { if s[i] > unicode.MaxASCII { return false } diff --git a/ipk/ipk_test.go b/ipk/ipk_test.go index f56262d..7350918 100644 --- a/ipk/ipk_test.go +++ b/ipk/ipk_test.go @@ -1034,7 +1034,7 @@ func Test_stripDisallowedFields(t *testing.T) { stripDisallowedFields(tc.info) - assert.Equal(tc.expect, tc.info.Overridables.IPK.Fields) + assert.Equal(tc.expect, tc.info.IPK.Fields) }) } } diff --git a/nfpm.go b/nfpm.go index 6e223d3..0d55797 100644 --- a/nfpm.go +++ b/nfpm.go @@ -8,6 +8,7 @@ import ( "io" "io/fs" "os" + "slices" "sort" "strings" "sync" @@ -182,7 +183,7 @@ func (c *Config) expandEnvVarsStringSlice(items []string) []string { } for i := 0; i < len(items); i++ { if items[i] == "" { - items = append(items[:i], items[i+1:]...) + items = slices.Delete(items, i, i+1) i-- // Since we just deleted items[i], we must redo that index } } @@ -204,11 +205,11 @@ func (c *Config) expandEnvVarsContents(contents files.Contents) files.Contents { func (c *Config) expandEnvVars() { // Version related fields - c.Info.Release = os.Expand(c.Info.Release, c.envMappingFunc) - c.Info.Version = os.Expand(c.Info.Version, c.envMappingFunc) - c.Info.Prerelease = os.Expand(c.Info.Prerelease, c.envMappingFunc) - c.Info.Platform = os.Expand(c.Info.Platform, c.envMappingFunc) - c.Info.Arch = os.Expand(c.Info.Arch, c.envMappingFunc) + c.Release = os.Expand(c.Release, c.envMappingFunc) + c.Version = os.Expand(c.Version, c.envMappingFunc) + c.Prerelease = os.Expand(c.Prerelease, c.envMappingFunc) + c.Platform = os.Expand(c.Platform, c.envMappingFunc) + c.Arch = os.Expand(c.Arch, c.envMappingFunc) for or := range c.Overrides { c.Overrides[or].Conflicts = c.expandEnvVarsStringSlice(c.Overrides[or].Conflicts) c.Overrides[or].Depends = c.expandEnvVarsStringSlice(c.Overrides[or].Depends) @@ -218,67 +219,67 @@ func (c *Config) expandEnvVars() { c.Overrides[or].Suggests = c.expandEnvVarsStringSlice(c.Overrides[or].Suggests) c.Overrides[or].Contents = c.expandEnvVarsContents(c.Overrides[or].Contents) } - c.Info.Conflicts = c.expandEnvVarsStringSlice(c.Info.Conflicts) - c.Info.Depends = c.expandEnvVarsStringSlice(c.Info.Depends) - c.Info.Replaces = c.expandEnvVarsStringSlice(c.Info.Replaces) - c.Info.Recommends = c.expandEnvVarsStringSlice(c.Info.Recommends) - c.Info.Provides = c.expandEnvVarsStringSlice(c.Info.Provides) - c.Info.Suggests = c.expandEnvVarsStringSlice(c.Info.Suggests) - c.Info.Contents = c.expandEnvVarsContents(c.Info.Contents) + c.Conflicts = c.expandEnvVarsStringSlice(c.Conflicts) + c.Depends = c.expandEnvVarsStringSlice(c.Depends) + c.Replaces = c.expandEnvVarsStringSlice(c.Replaces) + c.Recommends = c.expandEnvVarsStringSlice(c.Recommends) + c.Provides = c.expandEnvVarsStringSlice(c.Provides) + c.Suggests = c.expandEnvVarsStringSlice(c.Suggests) + c.Contents = c.expandEnvVarsContents(c.Contents) // Basic metadata fields - c.Info.Name = os.Expand(c.Info.Name, c.envMappingFunc) - c.Info.Homepage = os.Expand(c.Info.Homepage, c.envMappingFunc) - c.Info.Maintainer = os.Expand(c.Info.Maintainer, c.envMappingFunc) - c.Info.Vendor = os.Expand(c.Info.Vendor, c.envMappingFunc) - c.Info.Description = os.Expand(c.Info.Description, c.envMappingFunc) + c.Name = os.Expand(c.Name, c.envMappingFunc) + c.Homepage = os.Expand(c.Homepage, c.envMappingFunc) + c.Maintainer = os.Expand(c.Maintainer, c.envMappingFunc) + c.Vendor = os.Expand(c.Vendor, c.envMappingFunc) + c.Description = os.Expand(c.Description, c.envMappingFunc) // Package signing related fields - c.Info.Deb.Signature.KeyFile = os.Expand(c.Deb.Signature.KeyFile, c.envMappingFunc) - c.Info.RPM.Signature.KeyFile = os.Expand(c.RPM.Signature.KeyFile, c.envMappingFunc) - c.Info.APK.Signature.KeyFile = os.Expand(c.APK.Signature.KeyFile, c.envMappingFunc) - c.Info.Deb.Signature.KeyID = pointer.ToString(os.Expand(pointer.GetString(c.Deb.Signature.KeyID), c.envMappingFunc)) - c.Info.RPM.Signature.KeyID = pointer.ToString(os.Expand(pointer.GetString(c.RPM.Signature.KeyID), c.envMappingFunc)) - c.Info.APK.Signature.KeyID = pointer.ToString(os.Expand(pointer.GetString(c.APK.Signature.KeyID), c.envMappingFunc)) + c.Deb.Signature.KeyFile = os.Expand(c.Deb.Signature.KeyFile, c.envMappingFunc) + c.RPM.Signature.KeyFile = os.Expand(c.RPM.Signature.KeyFile, c.envMappingFunc) + c.APK.Signature.KeyFile = os.Expand(c.APK.Signature.KeyFile, c.envMappingFunc) + c.Deb.Signature.KeyID = pointer.ToString(os.Expand(pointer.GetString(c.Deb.Signature.KeyID), c.envMappingFunc)) + c.RPM.Signature.KeyID = pointer.ToString(os.Expand(pointer.GetString(c.RPM.Signature.KeyID), c.envMappingFunc)) + c.APK.Signature.KeyID = pointer.ToString(os.Expand(pointer.GetString(c.APK.Signature.KeyID), c.envMappingFunc)) // Package signing passphrase generalPassphrase := os.Expand("$NFPM_PASSPHRASE", c.envMappingFunc) - c.Info.Deb.Signature.KeyPassphrase = generalPassphrase - c.Info.RPM.Signature.KeyPassphrase = generalPassphrase - c.Info.APK.Signature.KeyPassphrase = generalPassphrase + c.Deb.Signature.KeyPassphrase = generalPassphrase + c.RPM.Signature.KeyPassphrase = generalPassphrase + c.APK.Signature.KeyPassphrase = generalPassphrase debPassphrase := os.Expand("$NFPM_DEB_PASSPHRASE", c.envMappingFunc) if debPassphrase != "" { - c.Info.Deb.Signature.KeyPassphrase = debPassphrase + c.Deb.Signature.KeyPassphrase = debPassphrase } rpmPassphrase := os.Expand("$NFPM_RPM_PASSPHRASE", c.envMappingFunc) if rpmPassphrase != "" { - c.Info.RPM.Signature.KeyPassphrase = rpmPassphrase + c.RPM.Signature.KeyPassphrase = rpmPassphrase } apkPassphrase := os.Expand("$NFPM_APK_PASSPHRASE", c.envMappingFunc) if apkPassphrase != "" { - c.Info.APK.Signature.KeyPassphrase = apkPassphrase + c.APK.Signature.KeyPassphrase = apkPassphrase } // RPM specific - c.Info.RPM.Packager = os.Expand(c.RPM.Packager, c.envMappingFunc) + c.RPM.Packager = os.Expand(c.RPM.Packager, c.envMappingFunc) // Deb specific - for k, v := range c.Info.Deb.Fields { - c.Info.Deb.Fields[k] = os.Expand(v, c.envMappingFunc) + for k, v := range c.Deb.Fields { + c.Deb.Fields[k] = os.Expand(v, c.envMappingFunc) } - c.Info.Deb.Predepends = c.expandEnvVarsStringSlice(c.Info.Deb.Predepends) + c.Deb.Predepends = c.expandEnvVarsStringSlice(c.Deb.Predepends) // IPK specific - for k, v := range c.Info.IPK.Fields { - c.Info.IPK.Fields[k] = os.Expand(v, c.envMappingFunc) + for k, v := range c.IPK.Fields { + c.IPK.Fields[k] = os.Expand(v, c.envMappingFunc) } - c.Info.IPK.Predepends = c.expandEnvVarsStringSlice(c.Info.IPK.Predepends) + c.IPK.Predepends = c.expandEnvVarsStringSlice(c.IPK.Predepends) // RPM specific - c.Info.RPM.Packager = os.Expand(c.RPM.Packager, c.envMappingFunc) + c.RPM.Packager = os.Expand(c.RPM.Packager, c.envMappingFunc) } // Info contains information about a single package. diff --git a/nfpm_test.go b/nfpm_test.go index 44371ee..6edfa5f 100644 --- a/nfpm_test.go +++ b/nfpm_test.go @@ -52,21 +52,21 @@ func TestDefaultsVersion(t *testing.T) { }) require.NotEmpty(t, info.Platform) require.Equal(t, "1.0.0", info.Version) - require.Equal(t, "", info.Release) - require.Equal(t, "", info.Prerelease) + require.Empty(t, info.Release) + require.Empty(t, info.Prerelease) info = nfpm.WithDefaults(&nfpm.Info{ Version: "v1.0.0-rc1", }) require.Equal(t, "1.0.0", info.Version) - require.Equal(t, "", info.Release) + require.Empty(t, info.Release) require.Equal(t, "rc1", info.Prerelease) info = nfpm.WithDefaults(&nfpm.Info{ Version: "v1.0.0-beta1", }) require.Equal(t, "1.0.0", info.Version) - require.Equal(t, "", info.Release) + require.Empty(t, info.Release) require.Equal(t, "beta1", info.Prerelease) info = nfpm.WithDefaults(&nfpm.Info{ @@ -180,20 +180,20 @@ func TestPrepareForPackager(t *testing.T) { }, }) require.NoError(t, nfpm.PrepareForPackager(info, "")) - require.Len(t, info.Overridables.Contents, 5) - asdFile := info.Overridables.Contents[0] + require.Len(t, info.Contents, 5) + asdFile := info.Contents[0] require.Equal(t, "/asd", asdFile.Destination) require.Equal(t, files.TypeFile, asdFile.Type) require.Equal(t, "-rw-r--r--", asdFile.FileInfo.Mode.String()) require.Equal(t, "root", asdFile.FileInfo.Owner) require.Equal(t, "root", asdFile.FileInfo.Group) - usrDir := info.Overridables.Contents[1] + usrDir := info.Contents[1] require.Equal(t, "/usr/", usrDir.Destination) require.Equal(t, files.TypeImplicitDir, usrDir.Type) require.Equal(t, "-rwxr-xr-x", usrDir.FileInfo.Mode.String()) require.Equal(t, "root", usrDir.FileInfo.Owner) require.Equal(t, "root", usrDir.FileInfo.Group) - aDir := info.Overridables.Contents[2] + aDir := info.Contents[2] require.Equal(t, "/usr/a/", aDir.Destination) require.Equal(t, files.TypeDir, aDir.Type) require.Equal(t, "-rwxr-xr-x", aDir.FileInfo.Mode.String()) @@ -239,7 +239,7 @@ func TestValidate(t *testing.T) { }, } require.NoError(t, nfpm.Validate(&info)) - require.Len(t, info.Overridables.Contents, 2) + require.Len(t, info.Contents, 2) }) t.Run("config", func(t *testing.T) { @@ -322,7 +322,7 @@ func TestParseFile(t *testing.T) { require.Equal(t, "My description", config.Description) require.Equal(t, "my/rpm/key/file", config.RPM.Signature.KeyFile) require.Equal(t, "hard/coded/file", config.Deb.Signature.KeyFile) - require.Equal(t, "", config.APK.Signature.KeyFile) + require.Empty(t, config.APK.Signature.KeyFile) } func TestParseEnhancedFile(t *testing.T) { diff --git a/rpm/rpm_test.go b/rpm/rpm_test.go index aa16527..370eeaa 100644 --- a/rpm/rpm_test.go +++ b/rpm/rpm_test.go @@ -736,19 +736,19 @@ func TestRPMChangelog(t *testing.T) { require.NoError(t, err) times, ok := _times.([]uint32) require.True(t, ok) - require.Equal(t, len(changelog), len(times)) + require.Len(t, changelog, len(times)) _titles, err := rpm.Header.Get(tagChangelogName) require.NoError(t, err) titles, ok := _titles.([]string) require.True(t, ok) - require.Equal(t, len(changelog), len(titles)) + require.Len(t, changelog, len(titles)) _notes, err := rpm.Header.Get(tagChangelogText) require.NoError(t, err) allNotes, ok := _notes.([]string) require.True(t, ok) - require.Equal(t, len(changelog), len(allNotes)) + require.Len(t, changelog, len(allNotes)) for i, entry := range changelog { timestamp := time.Unix(int64(times[i]), 0).UTC() @@ -758,7 +758,7 @@ func TestRPMChangelog(t *testing.T) { require.Equal(t, entry.Date, timestamp) require.Contains(t, title, entry.Packager) require.Contains(t, title, entry.Semver) - require.Equal(t, len(entry.Changes), len(notes)) + require.Len(t, entry.Changes, len(notes)) for j, change := range entry.Changes { require.Contains(t, notes[j], change.Note)