Skip to content

Commit bef74d6

Browse files
authored
refactor: modernize code (#350)
* chore: enable `modernize` linter * refactor: modernize code
1 parent 5a486d5 commit bef74d6

File tree

9 files changed

+14
-37
lines changed

9 files changed

+14
-37
lines changed

.golangci.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ linters:
2424
- wrapcheck # not (yet) convinced
2525
- wsl # disagree with, for now
2626
- wsl_v5 # disagree with, for now
27-
28-
- modernize
2927
settings:
3028
depguard:
3129
rules:

main.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"os"
88
"path/filepath"
99
"slices"
10-
"sort"
1110
"strings"
1211

1312
"github.com/fatih/color"
@@ -269,9 +268,7 @@ func findLockfiles(r *reporter.Reporter, pathToLockOrDirectory string, parseAs s
269268
r.PrintErrorf("Error reading %s: %v\n", pathToLockOrDirectory, err)
270269
}
271270

272-
sort.Slice(lockfiles, func(i, j int) bool {
273-
return lockfiles[i] < lockfiles[j]
274-
})
271+
slices.Sort(lockfiles)
275272

276273
return lockfiles, err != nil
277274
}
@@ -734,9 +731,7 @@ func writeUpdatedConfigs(r *reporter.Reporter, vulnsPerConfig map[string]map[str
734731
for id := range vulns {
735732
ignores = append(ignores, id)
736733
}
737-
sort.Slice(ignores, func(i, j int) bool {
738-
return ignores[i] < ignores[j]
739-
})
734+
slices.Sort(ignores)
740735

741736
err := configer.UpdateWithIgnores(configPath, ignores)
742737

@@ -752,9 +747,7 @@ func writeUpdatedConfigs(r *reporter.Reporter, vulnsPerConfig map[string]map[str
752747
}
753748
}
754749

755-
sort.Slice(lines, func(i, j int) bool {
756-
return lines[i] < lines[j]
757-
})
750+
slices.Sort(lines)
758751

759752
for _, line := range lines {
760753
if strings.HasPrefix(line, "Error updating") {

pkg/database/api-check.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,7 @@ func batchPkgs(pkgs []internal.PackageDetails, batchSize int) [][]internal.Packa
149149
)
150150

151151
for i := 0; i < len(pkgs); i += batchSize {
152-
end := i + batchSize
153-
154-
if end > len(pkgs) {
155-
end = len(pkgs)
156-
}
152+
end := min(i+batchSize, len(pkgs))
157153

158154
batches = append(batches, pkgs[i:end])
159155
}

pkg/database/api-check_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type apiPackage struct {
2121
type apiQuery struct {
2222
Commit string `json:"commit,omitempty"`
2323
Version string `json:"version,omitempty"`
24-
Package apiPackage `json:"package,omitempty"`
24+
Package apiPackage `json:"package"`
2525
}
2626

2727
func jsonMarshalQueryBatchResponse(t *testing.T, vulns []objectsWithIDs) []byte {

pkg/database/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type apiQuery struct {
2424
Package struct {
2525
Name string `json:"name"`
2626
Ecosystem internal.Ecosystem `json:"ecosystem"`
27-
} `json:"package,omitempty"`
27+
} `json:"package"`
2828
}
2929

3030
var ErrOfflineDatabaseNotSupported = errors.New("API database does not support being used offline")

pkg/lockfile/helpers_test.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package lockfile_test
22

33
import (
44
"fmt"
5+
"slices"
56
"strings"
67
"testing"
78

@@ -34,13 +35,7 @@ func packageToString(pkg lockfile.PackageDetails) string {
3435
func hasPackage(t *testing.T, packages []lockfile.PackageDetails, pkg lockfile.PackageDetails) bool {
3536
t.Helper()
3637

37-
for _, details := range packages {
38-
if details == pkg {
39-
return true
40-
}
41-
}
42-
43-
return false
38+
return slices.Contains(packages, pkg)
4439
}
4540

4641
func expectPackage(t *testing.T, packages []lockfile.PackageDetails, pkg lockfile.PackageDetails) {

pkg/lockfile/parse-npm-lock.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package lockfile
33
import (
44
"encoding/json"
55
"fmt"
6+
"maps"
67
"os"
78
"path"
89
"strings"
@@ -43,13 +44,8 @@ func pkgDetailsMapToSlice(m map[string]PackageDetails) []PackageDetails {
4344
func mergePkgDetailsMap(m1 map[string]PackageDetails, m2 map[string]PackageDetails) map[string]PackageDetails {
4445
details := map[string]PackageDetails{}
4546

46-
for name, detail := range m1 {
47-
details[name] = detail
48-
}
49-
50-
for name, detail := range m2 {
51-
details[name] = detail
52-
}
47+
maps.Copy(details, m1)
48+
maps.Copy(details, m2)
5349

5450
return details
5551
}

pkg/lockfile/parse-requirements-txt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func parseRequirementsTxt(pathToLockfile string, requiredAlready map[string]stru
128128

129129
line = removeComments(line)
130130

131-
if ar := strings.TrimPrefix(line, "-r "); ar != line {
131+
if ar, ok := strings.CutPrefix(line, "-r "); ok {
132132
ar = filepath.Join(filepath.Dir(pathToLockfile), ar)
133133

134134
if _, ok := requiredAlready[ar]; ok {

pkg/lockfile/parse.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"path/filepath"
7+
"slices"
78
"sort"
89
"strings"
910
)
@@ -81,9 +82,7 @@ func (ps Packages) Ecosystems() []Ecosystem {
8182

8283
slicedEcosystems := toSliceOfEcosystems(ecosystems)
8384

84-
sort.Slice(slicedEcosystems, func(i, j int) bool {
85-
return slicedEcosystems[i] < slicedEcosystems[j]
86-
})
85+
slices.Sort(slicedEcosystems)
8786

8887
return slicedEcosystems
8988
}

0 commit comments

Comments
 (0)