Skip to content

Commit da42685

Browse files
andaaronlfrancke
andauthored
chore: update golangci-lint and fix all issues (#3575)
* chore: Update golangci-lint Signed-off-by: Lars Francke <git@lars-francke.de> * chore: fix all golangci-lint issues - Remove deprecated `// +build` tags - Fix godoclint, modernize, wsl_v5, govet, lll, gci, noctx issues - Update linter configuration - Modernize code to use Go 1.22+ features (for range N, slices.Contains, etc.) - Update make check lint the privileged tests Signed-off-by: Andrei Aaron <andreifdaaron@gmail.com> --------- Signed-off-by: Lars Francke <git@lars-francke.de> Signed-off-by: Andrei Aaron <andreifdaaron@gmail.com> Co-authored-by: Lars Francke <git@lars-francke.de>
1 parent 566286a commit da42685

242 files changed

Lines changed: 808 additions & 1007 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/golangci-lint.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ jobs:
2424
uses: golangci/golangci-lint-action@v9
2525
with:
2626
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
27-
version: v2.1.0
27+
version: v2.6.2
2828

2929
# Optional: working directory, useful for monorepos
3030
# working-directory: somedir
3131

3232
# Optional: golangci-lint command line arguments.
3333
# args: --issues-exit-code=0
34-
args: --config ./golangcilint.yaml --build-tags debug,needprivileges,sync,scrub,search,userprefs,metrics,lint,mgmt,imagetrust ./cmd/... ./pkg/...
34+
args: --build-tags debug,needprivileges,sync,scrub,search,userprefs,metrics,lint,mgmt,imagetrust ./cmd/... ./pkg/...
3535

3636
# Optional: show only new issues if it's a pull request. The default value is `false`.
3737
# only-new-issues: true

golangcilint.yaml renamed to .golangci.yaml

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ linters:
1919
- maintidx
2020
- mnd
2121
- musttag
22+
- noinlineerr
2223
- paralleltest
2324
- promlinter
2425
- protogetter
@@ -29,6 +30,7 @@ linters:
2930
- staticcheck
3031
- usetesting
3132
- wrapcheck
33+
- wsl
3234
settings:
3335
cyclop:
3436
max-complexity: 40
@@ -68,9 +70,20 @@ linters:
6870
- l *ldap.Conn
6971
- w http.ResponseWriter
7072
- r *http.Request
71-
wsl:
72-
allow-assign-and-anything: true
73-
force-err-cuddling: true
73+
wsl_v5:
74+
allow-first-in-block: true
75+
allow-whole-block: false
76+
branch-max-lines: 2
77+
enable:
78+
- err
79+
disable:
80+
- assign
81+
- defer
82+
- if
83+
- send
84+
- expr
85+
- select
86+
- switch
7487
exclusions:
7588
generated: lax
7689
presets:

Makefile

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ TOOLSDIR := $(shell pwd)/hack/tools
1111
PATH := bin:$(TOOLSDIR)/bin:$(PATH)
1212
STACKER := $(shell which stacker)
1313
GOLINTER := $(TOOLSDIR)/bin/golangci-lint
14-
GOLINTER_VERSION := v2.1.0
14+
GOLINTER_VERSION := v2.6.2
1515
NOTATION := $(TOOLSDIR)/bin/notation
1616
NOTATION_VERSION := 1.3.2
1717
COSIGN := $(TOOLSDIR)/bin/cosign
@@ -331,24 +331,29 @@ covhtml:
331331
gocovmerge coverage*.txt > coverage.txt
332332
go tool cover -html=coverage.txt -o coverage.html
333333

334-
$(GOLINTER):
334+
$(GOLINTER): $(TOOLSDIR)/.golangci-lint-$(GOLINTER_VERSION)
335+
336+
$(TOOLSDIR)/.golangci-lint-$(GOLINTER_VERSION):
335337
mkdir -p $(TOOLSDIR)/bin
338+
rm -f $(TOOLSDIR)/.golangci-lint-*
336339
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(TOOLSDIR)/bin $(GOLINTER_VERSION)
337340
$(GOLINTER) version
341+
touch $@
338342

339343
.PHONY: check-logs
340344
check-logs:
341345
@./scripts/check_logs.sh
342346

343347
.PHONY: check
344348
check: $(if $(findstring ui,$(BUILD_LABELS)), ui)
345-
check: ./golangcilint.yaml $(GOLINTER)
349+
check: ./.golangci.yaml $(GOLINTER)
346350
mkdir -p pkg/extensions/build; touch pkg/extensions/build/.empty
347-
$(GOLINTER) --config ./golangcilint.yaml run --output.text.colors --build-tags ./...
348-
$(GOLINTER) --config ./golangcilint.yaml run --output.text.colors --build-tags $(BUILD_LABELS) ./...
349-
$(GOLINTER) --config ./golangcilint.yaml run --output.text.colors --build-tags debug ./pkg/debug/swagger/ ./pkg/debug/gqlplayground
350-
$(GOLINTER) --config ./golangcilint.yaml run --output.text.colors --build-tags dev ./pkg/test/inject/
351-
$(GOLINTER) --config ./golangcilint.yaml run --output.text.colors --build-tags stress ./pkg/cli/server/
351+
$(GOLINTER) run --output.text.colors --build-tags ./...
352+
$(GOLINTER) run --output.text.colors --build-tags $(BUILD_LABELS) ./...
353+
$(GOLINTER) run --output.text.colors --build-tags debug ./pkg/debug/swagger/ ./pkg/debug/gqlplayground
354+
$(GOLINTER) run --output.text.colors --build-tags dev ./pkg/test/inject/
355+
$(GOLINTER) run --output.text.colors --build-tags stress ./pkg/cli/server/
356+
$(GOLINTER) run --output.text.colors --build-tags needprivileges,$(BUILD_LABELS) ./pkg/cli/client/ ./pkg/storage/local/ ./pkg/api/config/
352357
rm pkg/extensions/build/.empty
353358

354359
.PHONY: swagger

cmd/zb/helper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"zotregistry.dev/zot/v2/pkg/common"
2626
)
2727

28-
func makeHTTPGetRequest(url string, resultPtr interface{}, client *resty.Client) (http.Header, error) {
28+
func makeHTTPGetRequest(url string, resultPtr any, client *resty.Client) (http.Header, error) {
2929
resp, err := client.R().Get(url)
3030
if err != nil {
3131
return http.Header{}, err

cmd/zb/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"zotregistry.dev/zot/v2/pkg/log"
1111
)
1212

13-
// "zb" - performance benchmark and stress.
13+
// NewPerfRootCmd creates the root command for "zb" - performance benchmark and stress.
1414
func NewPerfRootCmd() *cobra.Command {
1515
showVersion := false
1616

cmd/zb/perf.go

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,10 @@ func updateStats(summary *statsSummary, record statsRecord) {
196196
}
197197

198198
type cicdTestSummary struct {
199-
Name string `json:"name"`
200-
Unit string `json:"unit"`
201-
Value interface{} `json:"value"`
202-
Range string `json:"range,omitempty"`
199+
Name string `json:"name"`
200+
Unit string `json:"unit"`
201+
Value any `json:"value"`
202+
Range string `json:"range,omitempty"`
203203
}
204204

205205
type manifestStruct struct {
@@ -299,15 +299,15 @@ func GetCatalog(
299299

300300
statusRequests = sync.Map{}
301301

302-
for count := 0; count < requests; count++ {
302+
for range requests {
303303
// Push random blob
304304
_, repos, err = pushMonolithImage(workdir, url, repo, repos, config, client)
305305
if err != nil {
306306
return err
307307
}
308308
}
309309

310-
for count := 0; count < requests; count++ {
310+
for range requests {
311311
func() {
312312
start := time.Now()
313313

@@ -376,7 +376,7 @@ func PushMonolithStreamed(
376376
statusRequests = sync.Map{}
377377
}
378378

379-
for count := 0; count < requests; count++ {
379+
for count := range requests {
380380
repos = pushMonolithAndCollect(workdir, url, trepo, count,
381381
repos, config, client, statsCh)
382382
}
@@ -406,7 +406,7 @@ func PushChunkStreamed(
406406
statusRequests = sync.Map{}
407407
}
408408

409-
for count := 0; count < requests; count++ {
409+
for count := range requests {
410410
repos = pushChunkAndCollect(workdir, url, trepo, count,
411411
repos, config, client, statsCh)
412412
}
@@ -493,7 +493,7 @@ func Pull(
493493
}
494494

495495
// download image
496-
for count := 0; count < requests; count++ {
496+
for range requests {
497497
repos = pullAndCollect(url, repos, manifestItem, config, client, statsCh)
498498
}
499499

@@ -530,7 +530,7 @@ func MixedPullAndPush(
530530
manifestHash: manifestHash,
531531
}
532532

533-
for count := 0; count < requests; count++ {
533+
for count := range requests {
534534
idx := flipFunc(config.probabilityRange)
535535

536536
readTestIdx := 0
@@ -667,6 +667,16 @@ func Perf(
667667
outFmt string, srcIPs string, srcCIDR string, skipCleanup bool,
668668
) {
669669
json := jsoniter.ConfigCompatibleWithStandardLibrary
670+
671+
// fatalWithCleanup calls teardown then logs fatal, ensuring cleanup happens before exit.
672+
// Uses sync.Once to ensure teardown is only called once, even from goroutines.
673+
var teardownOnce sync.Once
674+
fatalWithCleanup := func(err error) {
675+
teardownOnce.Do(func() {
676+
teardown(workdir)
677+
})
678+
log.Fatal(err)
679+
}
670680
// logging
671681
log.SetFlags(0)
672682
log.SetOutput(tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.TabIndent))
@@ -694,7 +704,6 @@ func Perf(
694704
log.Printf("Preparing test data ...\n")
695705

696706
setup(workdir)
697-
defer teardown(workdir)
698707

699708
log.Printf("Starting tests ...\n")
700709

@@ -709,7 +718,7 @@ func Perf(
709718
} else if len(srcCIDR) > 0 {
710719
ips, err = getIPsFromCIDR(srcCIDR, maxSourceIPs)
711720
if err != nil {
712-
log.Fatal(err) //nolint: gocritic
721+
fatalWithCleanup(err)
713722
}
714723
}
715724

@@ -722,7 +731,7 @@ func Perf(
722731

723732
start := time.Now()
724733

725-
for c := 0; c < concurrency; c++ {
734+
for range concurrency {
726735
// parallelize with clients
727736
wg.Add(1)
728737

@@ -731,12 +740,12 @@ func Perf(
731740

732741
httpClient, err := getRandomClientIPs(auth, url, ips)
733742
if err != nil {
734-
log.Fatal(err)
743+
fatalWithCleanup(err)
735744
}
736745

737746
err = tconfig.tfunc(workdir, url, repo, requests/concurrency, tconfig, statsCh, httpClient, skipCleanup)
738747
if err != nil {
739-
log.Fatal(err)
748+
fatalWithCleanup(err)
740749
}
741750
}()
742751
}
@@ -754,7 +763,7 @@ func Perf(
754763
summary.mixedType = true
755764
}
756765

757-
for count := 0; count < requests; count++ {
766+
for range requests {
758767
record := <-statsCh
759768
updateStats(&summary, record)
760769
}
@@ -771,14 +780,19 @@ func Perf(
771780
if outFmt == cicdFmt {
772781
jsonOut, err := json.Marshal(cicdSummary)
773782
if err != nil {
774-
log.Fatal(err) // file closed on exit
783+
fatalWithCleanup(err)
775784
}
776785

777786
if err := os.WriteFile(outFmt+".json", jsonOut, defaultFilePerms); err != nil {
778-
log.Fatal(err)
787+
fatalWithCleanup(err)
779788
}
780789
}
781790

791+
// Cleanup before exit (sync.Once ensures it only runs once, even if fatalWithCleanup was called)
792+
teardownOnce.Do(func() {
793+
teardown(workdir)
794+
})
795+
782796
if zbError {
783797
os.Exit(1)
784798
}

cmd/zli/main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//go:build search
2-
// +build search
32

43
package main
54

cmd/zli/main_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//go:build search
2-
// +build search
32

43
package main_test
54

cmd/zxp/main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//go:build !metrics
2-
// +build !metrics
32

43
package main
54

pkg/api/authn.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"net"
1414
"net/http"
1515
"os"
16+
"slices"
1617
"strconv"
1718
"strings"
1819
"time"
@@ -657,7 +658,7 @@ func getRelyingPartyArgs(cfg *config.Config, provider string, hashKey, encryptKe
657658

658659
scopes := providerConfig.Scopes
659660
// openid scope must be the first one in list
660-
if !zcommon.Contains(scopes, oidc.ScopeOpenID) && config.IsOpenIDSupported(provider) {
661+
if !slices.Contains(scopes, oidc.ScopeOpenID) && config.IsOpenIDSupported(provider) {
661662
scopes = append([]string{oidc.ScopeOpenID}, scopes...)
662663
}
663664

0 commit comments

Comments
 (0)