From 559c0f30b16b305cc6b79832b747fc8568af61ee Mon Sep 17 00:00:00 2001 From: DmitriyLewen <91113035+DmitriyLewen@users.noreply.github.com> Date: Tue, 26 Sep 2023 12:20:54 +0600 Subject: [PATCH] chore: enable more linters (#5228) * chore: enable more linters * fix typos * ci: add `verbose` option in linter action * ci: remove `verbose` option in linter action --- .github/workflows/test.yaml | 2 +- .golangci.yaml | 16 +++++++++++++++- docs/community/contribute/pr.md | 7 ++++++- magefiles/magefile.go | 14 +++++++++++--- pkg/fanal/image/registry/google/google.go | 2 +- 5 files changed, 34 insertions(+), 7 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 6cfc3a4bbe3e..2970bcb9846f 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -43,7 +43,7 @@ jobs: - name: Lint uses: golangci/golangci-lint-action@v3.7.0 with: - version: v1.52 + version: v1.54 args: --deadline=30m skip-cache: true # https://github.com/golangci/golangci-lint-action/issues/244#issuecomment-1052197778 if: matrix.operating-system == 'ubuntu-latest' diff --git a/.golangci.yaml b/.golangci.yaml index 9c574718fb44..8c2914b18411 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -32,6 +32,17 @@ linters-settings: - prefix(github.com/aquasecurity/) - blank - dot + gomodguard: + blocked: + modules: + - github.com/hashicorp/go-version: + recommendations: + - github.com/aquasecurity/go-version + reason: "`aquasecurity/go-version` is designed for our use-cases" + - github.com/Masterminds/semver: + recommendations: + - github.com/aquasecurity/go-version + reason: "`aquasecurity/go-version` is designed for our use-cases" linters: disable-all: true @@ -47,12 +58,15 @@ linters: - gocyclo - gofmt - misspell + - bodyclose - gci + - gomodguard + - tenv run: go: '1.20' skip-files: - - ".*._mock.go$" + - ".*_mock.go$" - ".*_test.go$" - "integration/*" - "examples/*" diff --git a/docs/community/contribute/pr.md b/docs/community/contribute/pr.md index c6b915b198a9..2982e3cecd72 100644 --- a/docs/community/contribute/pr.md +++ b/docs/community/contribute/pr.md @@ -27,7 +27,7 @@ $ ./trivy -h You must pass the linter checks: ```shell -$ mage lint +$ mage lint:run ``` Additionally, you need to have run `go mod tidy`, so execute the following command as well: @@ -36,6 +36,11 @@ Additionally, you need to have run `go mod tidy`, so execute the following comma $ mage tidy ``` +To autofix linters use the following command: +```shell +$ mage lint:fix +``` + ### Unit tests Your PR must pass all the unit tests. You can test it as below. diff --git a/magefiles/magefile.go b/magefiles/magefile.go index ada94c878b31..f3f921d19b34 100644 --- a/magefiles/magefile.go +++ b/magefiles/magefile.go @@ -60,7 +60,7 @@ func (Tool) Wire() error { // GolangciLint installs golangci-lint func (Tool) GolangciLint() error { - const version = "v1.52.2" + const version = "v1.54.2" if exists(filepath.Join(GOBIN, "golangci-lint")) { return nil } @@ -277,12 +277,20 @@ func (Test) UpdateVMGolden() error { return sh.RunWithV(ENV, "go", "test", "-v", "-tags=vm_integration", "./integration/...", "-update") } -// Lint runs linters -func Lint() error { +type Lint mg.Namespace + +// Run runs linters +func (Lint) Run() error { mg.Deps(Tool{}.GolangciLint) return sh.RunV("golangci-lint", "run", "--timeout", "5m") } +// Fix auto fixes linters +func (Lint) Fix() error { + mg.Deps(Tool{}.GolangciLint) + return sh.RunV("golangci-lint", "run", "--timeout", "5m", "--fix") +} + // Fmt formats Go code and proto files func Fmt() error { // Check if clang-format is installed diff --git a/pkg/fanal/image/registry/google/google.go b/pkg/fanal/image/registry/google/google.go index 37182f44fecd..f4e7a7414260 100644 --- a/pkg/fanal/image/registry/google/google.go +++ b/pkg/fanal/image/registry/google/google.go @@ -34,7 +34,7 @@ func (g *Registry) CheckOptions(domain string, option types.RegistryOptions) err return nil } -func (g *Registry) GetCredential(ctx context.Context) (username, password string, err error) { +func (g *Registry) GetCredential(_ context.Context) (username, password string, err error) { var credStore store.GCRCredStore if g.Store == nil { credStore, err = store.DefaultGCRCredStore()