fix: lint issues, modernize codebase (#922)

This commit is contained in:
Carlos Alexandro Becker
2025-03-29 08:57:48 -03:00
committed by GitHub
parent a543547e69
commit c94b6a1433
14 changed files with 119 additions and 110 deletions
+41 -21
View File
@@ -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
rules:
yaml: snake
json: snake
settings:
depguard:
rules:
main:
deny:
- pkg: "github.com/pkg/errors"
desc: "use stdlib instead"
- 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$
+2 -6
View File
@@ -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.
+3 -3
View File
@@ -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,
},
}
+1 -1
View File
@@ -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...)
}
+1 -1
View File
@@ -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
}
+2 -2
View File
@@ -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)
}
}
+3 -6
View File
@@ -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
+3 -8
View File
@@ -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.
+2 -2
View File
@@ -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) {
+1 -1
View File
@@ -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
}
+1 -1
View File
@@ -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)
})
}
}
+39 -38
View File
@@ -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.
+10 -10
View File
@@ -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) {
+4 -4
View File
@@ -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)