From 04d9e0ed8417b1eb6c08e742adec70227d75cfd9 Mon Sep 17 00:00:00 2001 From: Philip Laine Date: Wed, 15 May 2024 19:03:38 +0200 Subject: [PATCH] ci: run revive using golang-lint-ci (#2499) ## Description This replaces revive with golang-ci-lint, and instead runs revive through the aforementioned tool. Additionally it replaces the header check script with the goheader linter. The benefit of this change is that we can enable more linters along with revive in the future. ## Related Issue Part of #2503 ## Checklist before merging - [x] Test, docs, adr added or updated as needed - [x] [Contributor Guide Steps](https://github.com/defenseunicorns/zarf/blob/main/.github/CONTRIBUTING.md#developer-workflow) followed --------- Co-authored-by: Austin Abro <37223396+AustinAbro321@users.noreply.github.com> Co-authored-by: razzle --- .github/CONTRIBUTING.md | 7 ++-- .github/workflows/scan-lint.yml | 11 ++---- .golangci.yaml | 62 ++++++++++++++++++++++++++++++++ .pre-commit-config.yaml | 24 +++++-------- Makefile | 5 ++- hack/check-spdx-go.sh | 63 --------------------------------- hack/revive.toml | 31 ---------------- src/pkg/utils/bytes.go | 1 + 8 files changed, 77 insertions(+), 127 deletions(-) create mode 100644 .golangci.yaml delete mode 100755 hack/check-spdx-go.sh delete mode 100644 hack/revive.toml diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index a931273d47..4dda9e1668 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -23,11 +23,8 @@ We use [pre-commit](https://pre-commit.com/) to manage our pre-commit hooks. Thi # install hooks pre-commit install -# install goimports -go install golang.org/x/tools/cmd/goimports@latest - -# install revive -go install github.com/mgechev/revive@latest +# install golang-ci-lint +go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest ``` Now every time you commit, the hooks will run and format your code, linting can be called via `make lint-go`. diff --git a/.github/workflows/scan-lint.yml b/.github/workflows/scan-lint.yml index fb826ae737..e35d4f7330 100644 --- a/.github/workflows/scan-lint.yml +++ b/.github/workflows/scan-lint.yml @@ -10,12 +10,5 @@ jobs: steps: - name: Checkout uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 - - - name: Run Revive Action by pulling pre-built image - uses: docker://morphy/revive-action:v2.5.7@sha256:087d4e61077087755711ab7e9fae3cc899b7bb07ff8f6a30c3dfb240b1620ae8 - with: - config: hack/revive.toml - # Exclude patterns, separated by semicolons (optional) - exclude: "src/cmd/viper.go" - # Path pattern (default: ./...) - path: "./src/..." + - name: Run golangci-lint + uses: golangci/golangci-lint-action@a4f60bb28d35aeee14e6880718e0c85ff1882e64 # v6.0.1 diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 0000000000..274226cc10 --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,62 @@ +run: + timeout: 5m +linters: + disable-all: true + enable: + - gosimple + - govet + - staticcheck + - unused + - revive + - goheader + - goimports + - nolintlint +linters-settings: + govet: + enable-all: true + disable: + - shadow + - fieldalignment + - unusedwrite + nolintlint: + require-specific: true + goheader: + template: |- + SPDX-License-Identifier: Apache-2.0 + SPDX-FileCopyrightText: 2021-Present The Zarf Authors + revive: + rules: + - name: blank-imports + - name: context-as-argument + - name: context-keys-type + - name: dot-imports + - name: error-return + - name: error-strings + - name: error-naming + - name: exported + - name: if-return + - name: increment-decrement + - name: var-naming + - name: var-declaration + - name: package-comments + - name: range + - name: receiver-naming + - name: time-naming + - name: unexported-return + - name: indent-error-flow + - name: errorf + - name: empty-block + - name: superfluous-else + - name: unused-parameter + - name: unreachable-code + - name: redefines-builtin-id +issues: + # Revive rules that are disabled by default. + include: + - EXC0012 + - EXC0013 + - EXC0014 + - EXC0015 + # Exclude linting code copied from Helm. + exclude-dirs: + - "src/cmd/tools/helm" diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 38ae99b9bd..93759bdb25 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -30,24 +30,16 @@ repos: language: script - id: goimports name: goimports - entry: goimports - files: .go$ - args: - - -l - - -w - language: system + entry: golangci-lint run --enable-only goimports --fix + types: [go] + language: golang pass_filenames: true - id: lint - name: revive go lint - entry: revive - args: - - "-config" - - "hack/revive.toml" - - "-exclude" - - "src/cmd/viper.go" - files: .go$ - language: system - pass_filenames: true + name: golangci-lint go lint + entry: golangci-lint run + types: [go] + language: golang + pass_filenames: false - repo: https://github.com/python-jsonschema/check-jsonschema rev: 0.14.0 hooks: diff --git a/Makefile b/Makefile index cc82f9d56e..5683962afc 100644 --- a/Makefile +++ b/Makefile @@ -225,6 +225,5 @@ cve-report: ## Create a CVE report for the current project (must `brew install g @test -d ./build || mkdir ./build go run main.go tools sbom scan . -o json --exclude './site' --exclude './examples' | grype -o template -t hack/grype.tmpl > build/zarf-known-cves.csv -lint-go: ## Run revive to lint the go code (must `brew install revive` first) - revive -config hack/revive.toml -exclude src/cmd/viper.go -formatter stylish ./src/... - @hack/check-spdx-go.sh src >/dev/null || (echo "SPDX check for go failed, please run 'hack/check-spdx-go.sh src' to see the errors" && exit 1) +lint-go: ## Run golang-ci-lint to lint the go code (must `brew install golang-ci-lint` first) + golangci-lint run diff --git a/hack/check-spdx-go.sh b/hack/check-spdx-go.sh deleted file mode 100755 index 7db63e2ff1..0000000000 --- a/hack/check-spdx-go.sh +++ /dev/null @@ -1,63 +0,0 @@ -#!/usr/bin/env bash - -set -euo pipefail - -# Directory containing the Go files -DIRECTORY="$1" - -# Array of paths to exclude from the check -EXCLUDE_PATHS=( - "src/cmd/tools/helm/repo_update.go" - "src/cmd/tools/helm/repo_remove.go" - "src/cmd/tools/helm/load_plugins.go" - "src/cmd/tools/helm/repo_list.go" - "src/cmd/tools/helm/flags.go" - "src/cmd/tools/helm/repo_add.go" - "src/cmd/tools/helm/dependency.go" - "src/cmd/tools/helm/repo_index.go" - "src/cmd/tools/helm/repo.go" - "src/cmd/tools/helm/dependency_build.go" - "src/cmd/tools/helm/dependency_update.go" - "src/cmd/tools/helm/root.go" -) - -BLACK='\033[0;30m' -RED='\033[0;31m' -RESET='\033[0m' - -# Function to check if a path is in the EXCLUDE_PATHS array -is_excluded() { - local path="$1" - for exclude in "${EXCLUDE_PATHS[@]}"; do - if [[ "$path" == "$exclude"* ]]; then - return 0 # 0 means true/success in shell script - fi - done - return 1 # 1 means false/failure in shell script -} - -# Flag to track if any file meets the condition -found=0 - -# Use process substitution to avoid subshell issue with the 'found' variable -while IFS= read -r file; do - if is_excluded "$file"; then - echo -e "$BLACK$file$RESET" - continue - fi - - # Use `head` to grab the first two lines and compare them directly - firstLine=$(head -n 1 "$file") - secondLine=$(head -n 2 "$file" | tail -n 1) - - # Check if the lines do not match the specified strings - if [[ "$firstLine" != "// SPDX-License-Identifier: Apache-2.0" || "$secondLine" != "// SPDX-FileCopyrightText: 2021-Present The Zarf Authors" ]]; then - echo -e "$RED$file$RESET" - found=1 - fi -done < <(find "$DIRECTORY" -type f -name "*.go") - -# If any file met the condition, exit with status 1 -if [ "$found" -eq 1 ]; then - exit 1 -fi diff --git a/hack/revive.toml b/hack/revive.toml deleted file mode 100644 index 222a780640..0000000000 --- a/hack/revive.toml +++ /dev/null @@ -1,31 +0,0 @@ -ignoreGeneratedHeader = false -severity = "warning" -confidence = 0.8 -errorCode = 1 -warningCode = 1 -formatter = "stylish" - -[rule.blank-imports] -[rule.context-as-argument] -[rule.context-keys-type] -[rule.dot-imports] -[rule.error-return] -[rule.error-strings] -[rule.error-naming] -[rule.exported] -[rule.if-return] -[rule.increment-decrement] -[rule.var-naming] -[rule.var-declaration] -[rule.package-comments] -[rule.range] -[rule.receiver-naming] -[rule.time-naming] -[rule.unexported-return] -[rule.indent-error-flow] -[rule.errorf] -[rule.empty-block] -[rule.superfluous-else] -[rule.unused-parameter] -[rule.unreachable-code] -[rule.redefines-builtin-id] diff --git a/src/pkg/utils/bytes.go b/src/pkg/utils/bytes.go index d5fd24e5c6..7ef3ef5c8c 100644 --- a/src/pkg/utils/bytes.go +++ b/src/pkg/utils/bytes.go @@ -1,5 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-FileCopyrightText: 2021-Present The Zarf Authors + // forked from https://www.socketloop.com/tutorials/golang-byte-format-example // Package utils provides generic utility functions.