From 478d7b26f690a039e24f7c464fcf05f7a5a07ad9 Mon Sep 17 00:00:00 2001 From: Philip Laine Date: Tue, 14 May 2024 11:38:49 +0200 Subject: [PATCH] Run revive using golang-lint-ci --- .github/CONTRIBUTING.md | 4 +-- .github/workflows/scan-lint.yml | 11 ++---- .golangci.yaml | 22 ++++++++++++ .pre-commit-config.yaml | 9 ++--- Makefile | 5 ++- hack/check-spdx-go.sh | 63 --------------------------------- hack/revive.toml | 31 ---------------- src/pkg/utils/bytes.go | 1 + 8 files changed, 33 insertions(+), 113 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..d9c64f5644 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -26,8 +26,8 @@ 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..df749c591d 100644 --- a/.github/workflows/scan-lint.yml +++ b/.github/workflows/scan-lint.yml @@ -10,12 +10,7 @@ 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 + - name: Run golangci-lint + uses: golangci/golangci-lint-action@v6 with: - config: hack/revive.toml - # Exclude patterns, separated by semicolons (optional) - exclude: "src/cmd/viper.go" - # Path pattern (default: ./...) - path: "./src/..." + version: v1.58.1 diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 0000000000..75778c0888 --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,22 @@ +run: + timeout: 5m +linters: + disable-all: true + enable: + - gosimple + - govet + - staticcheck + - unused + - revive + - goheader + - goimports +linters-settings: + goheader: + template: |- + SPDX-License-Identifier: Apache-2.0 + SPDX-FileCopyrightText: 2021-Present The Zarf Authors +issues: + exclude-rules: + - path: "src/cmd/tools/helm/(.*).go" + linters: + - goheader diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 5e96cba653..1b30fa4dc6 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -37,13 +37,10 @@ repos: language: system pass_filenames: true - id: lint - name: revive go lint - entry: revive + name: golang-ci-lint go lint + entry: golang-ci-lint args: - - "-config" - - "hack/revive.toml" - - "-exclude" - - "src/cmd/viper.go" + - "run" files: .go$ language: system pass_filenames: true 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.