From 7d2d3f29ebcb842a09e8e0e86a669b811f6e85f1 Mon Sep 17 00:00:00 2001 From: thxCode Date: Sun, 4 Feb 2024 09:54:45 +0800 Subject: [PATCH] ci: adjust lint Signed-off-by: thxCode --- .golangci.yml | 123 +++++++++------- hack/lib/style.sh | 252 ++++++-------------------------- hack/lint.sh | 14 +- pkg/tap/apply.go | 3 +- pkg/tap/load.go | 3 +- pkg/terraform/load.go | 3 +- pkg/terraform/load_overrider.go | 1 - pkg/terraform/load_walker.go | 2 - pkg/terraform/write.go | 3 +- 9 files changed, 136 insertions(+), 268 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 30f8a22..3e266c2 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -44,16 +44,16 @@ linters: - exportloopref - godot - goconst - - gofumpt - gocritic - gosimple - gosec - govet + - gofumpt + - gofmt - ineffassign - lll - makezero - misspell - - misspell - nakedret - nilerr - prealloc @@ -67,6 +67,7 @@ linters: - unused - usestdlibvars - whitespace + - wsl # enable complexity linters # - dupl @@ -75,24 +76,20 @@ linters: # - funlen linters-settings: - staticcheck: - checks: ["all", "-SA1019", "-SA2002"] - stylecheck: - checks: ["all", "-ST1003"] - gosec: - severity: "low" - confidence: "low" - excludes: - - G101 - - G107 - - G112 - - G404 - revive: - rules: - - name: var-naming - disabled: true - arguments: - - ["HTTP", "ID", "TLS", "TCP", "UDP", "API", "CA", "URL", "DNS"] + decorder: + dec-order: + - const + - var + - func + disable-init-func-first-check: false + disable-dec-order-check: true + errorlint: + # Check whether fmt.Errorf uses the %w verb for formatting errors. See the readme for caveats + errorf: true + # Check for plain type assertions and type switches + asserts: true + # Check for plain error comparisons + comparison: true godot: # Comments to be checked: `declarations`, `toplevel`, or `all`. # Default: declarations @@ -110,24 +107,65 @@ linters-settings: # Check that each sentence starts with a capital letter. # Default: false capital: true - lll: - # max line length, lines longer will be reported. Default is 120. - # '\t' is counted as 1 character by default, and can be changed with the tab-width option - line-length: 150 - # tab width in spaces. Default to 1. - tab-width: 1 goconst: # Minimal length of string constant. # Default: 3 min-len: 3 # Minimum occurrences of constant string count to trigger issue. # Default: 3 - min-occurrences: 3 + min-occurrences: 10 + gosimple: + go: "1.21" + checks: [ "all" ] + gosec: + severity: "low" + confidence: "low" + excludes: + - G101 + - G107 + - G112 + - G404 + gofumpt: + # Choose whether to use the extra rules. + # Default: false + extra-rules: true + gofmt: + # Simplify code: gofmt with `-s` option. + # Default: true + simplify: true + # Apply the rewrite rules to the source before reformatting. + # https://pkg.go.dev/cmd/gofmt + # Default: [] + rewrite-rules: + - pattern: 'interface{}' + replacement: 'any' + - pattern: 'a[b:len(a)]' + replacement: 'a[b:]' + lll: + # max line length, lines longer will be reported. Default is 120. + # '\t' is counted as 1 character by default, and can be changed with the tab-width option + line-length: 150 + # tab width in spaces. Default to 1. + tab-width: 1 + makezero: + always: false misspell: # Correct spellings using locale preferences for US or UK. # Default is to use a neutral variety of English. # Setting locale to US will correct the British spelling of 'colour' to 'color'. locale: US + nakedret: + max-func-lines: 60 + revive: + rules: + - name: var-naming + disabled: true + arguments: + - [ "HTTP", "ID", "TLS", "TCP", "UDP", "API", "CA", "URL", "DNS" ] + staticcheck: + checks: [ "all", "-SA1019", "-SA2002", "-SA5008" ] + stylecheck: + checks: [ "all", "-ST1003" ] unparam: # Inspect exported functions, default is false. Set to true if no external program/library imports your code. # XXX: if you enable this setting, unparam will report a lot of false-positives in text editors: @@ -156,20 +194,6 @@ linters-settings: # Mark all identifiers inside generated files as used. # Default: true generated-is-used: true - errorlint: - # Check whether fmt.Errorf uses the %w verb for formatting errors. See the readme for caveats - errorf: true - # Check for plain type assertions and type switches - asserts: true - # Check for plain error comparisons - comparison: true - makezero: - always: false - gosimple: - go: "1.20" - checks: ["all"] - nakedret: - max-func-lines: 60 usestdlibvars: # Suggest the use of http.MethodXX # Default: true @@ -189,13 +213,15 @@ linters-settings: # Suggest the use of crypto.Hash # Default: false crypto-hash: true - decorder: - dec-order: - - const - - var - - func - disable-init-func-first-check: false - disable-dec-order-check: true + wsl: + # Allows assignments to be cuddled with anything. + allow-assign-and-anything: true + # Allow blocks to end with comments. + allow-trailing-comment: true + # Causes an error if a short declaration (:=) cuddles with anything other than + # another short declaration. + # This logic overrides force-err-cuddling among others. + force-short-decl-cuddling: false issues: exclude-rules: @@ -207,4 +233,3 @@ issues: - makezero - lll - funlen - - wsl diff --git a/hack/lib/style.sh b/hack/lib/style.sh index a7f2082..754bb40 100644 --- a/hack/lib/style.sh +++ b/hack/lib/style.sh @@ -4,9 +4,11 @@ # Lint variables helpers. These functions need the # following variables: # +# GOIMPORT_REVISER_VERSION - The Goimports-reviser version, default is v3.6.4. # GOLANGCI_LINT_VERSION - The Golangci-lint version, default is v1.55.2. # COMMITSAR_VERSION - The Commitsar version, default is v0.20.2. +goimports_reviser_version=${GOIMPORT_REVISER_VERSION:-"v3.6.4"} golangci_lint_version=${GOLANGCI_LINT_VERSION:-"v1.55.2"} commitsar_version=${COMMITSAR_VERSION:-"v0.20.2"} @@ -24,7 +26,7 @@ function seal::lint::golangci_lint::validate() { seal::log::info "installing golangci-lint ${golangci_lint_version}" if seal::lint::golangci_lint::install; then - seal::log::info "golangci_lint $($(seal::lint::golangci_lint::bin) --version 2>&1 | cut -d " " -f 4 2>&1 | head -n 1)" + seal::log::info "golangci-lint $($(seal::lint::golangci_lint::bin) --version 2>&1 | cut -d " " -f 4 2>&1 | head -n 1)" return 0 fi seal::log::error "no golangci-lint available" @@ -39,51 +41,40 @@ function seal::lint::golangci_lint::bin() { echo -n "${bin}" } -function seal::lint::run() { - if ! seal::lint::golangci_lint::validate; then - seal::log::warn "using go fmt/vet instead golangci-lint" - shift 1 - local fmt_args=() - local vet_args=() - for arg in "$@"; do - if [[ "${arg}" == "--build-tags="* ]]; then - arg="${arg//--build-/-}" - vet_args+=("${arg}") - continue - fi - fmt_args+=("${arg}") - vet_args+=("${arg}") - done - seal::log::debug "go fmt ${fmt_args[*]}" - go fmt "${fmt_args[@]}" - seal::log::debug "go vet ${vet_args[*]}" - go vet "${vet_args[@]}" - return 0 - fi - - seal::log::debug "golangci-lint run --fix $*" - $(seal::lint::golangci_lint::bin) run --fix "$@" -} - -function seal::format::goimports::install() { - GOBIN="${ROOT_DIR}/.sbin" go install github.com/incu6us/goimports-reviser/v3@latest +function seal::lint::goimports_reviser::install() { + local os + os="$(seal::util::get_raw_os)" + local arch + arch="$(seal::util::get_raw_arch)" + curl --retry 3 --retry-all-errors --retry-delay 3 \ + -o /tmp/commitsar.tar.gz \ + -sSfL "https://github.com/incu6us/goimports-reviser/releases/download/${goimports_reviser_version}/goimports-reviser_${goimports_reviser_version#v}_${os}_${arch}.tar.gz" + tar -zxvf /tmp/commitsar.tar.gz \ + --directory "${ROOT_DIR}/.sbin" \ + --no-same-owner \ + --exclude ./LICENSE \ + --exclude ./README.md + chmod a+x "${ROOT_DIR}/.sbin/goimports-reviser" } -function seal::format::goimports::validate() { +function seal::lint::goimports_reviser::validate() { # shellcheck disable=SC2046 - if [[ -n "$(command -v $(seal::format::goimports::bin))" ]]; then - return 0 + if [[ -n "$(command -v $(seal::lint::goimports_reviser::bin))" ]]; then + if [[ $($(seal::lint::goimports_reviser::bin) -version | grep tag | cut -d " " -f 2 2>&1 | head -n 1) == "${goimports_reviser_version}" ]]; then + return 0 + fi fi - seal::log::info "installing goimports" - if seal::format::goimports::install; then + seal::log::info "installing goimports-reviser" + if seal::lint::goimports_reviser::install; then + seal::log::info "goimports-reviser $($(seal::lint::goimports_reviser::bin) -version | grep tag | cut -d " " -f 2 2>&1 | head -n 1)" return 0 fi seal::log::error "no goimports-reviser available" return 1 } -function seal::format::goimports::bin() { +function seal::lint::goimports_reviser::bin() { local bin="goimports-reviser" if [[ -f "${ROOT_DIR}/.sbin/goimports-reviser" ]]; then bin="${ROOT_DIR}/.sbin/goimports-reviser" @@ -91,190 +82,43 @@ function seal::format::goimports::bin() { echo -n "${bin}" } -function seal::format::gofumpt::install() { - GOBIN="${ROOT_DIR}/.sbin" go install mvdan.cc/gofumpt@latest -} - -function seal::format::gofumpt::validate() { - # shellcheck disable=SC2046 - if [[ -n "$(command -v $(seal::format::gofumpt::bin))" ]]; then - return 0 - fi - - seal::log::info "installing gofumpt" - if seal::format::gofumpt::install; then - return 0 - fi - seal::log::error "no gofumpt available" - return 1 -} - -function seal::format::gofumpt::bin() { - local bin="gofumpt" - if [[ -f "${ROOT_DIR}/.sbin/gofumpt" ]]; then - bin="${ROOT_DIR}/.sbin/gofumpt" - fi - echo -n "${bin}" -} - -# install golines -function seal::format::golines::install() { - GOBIN="${ROOT_DIR}/.sbin" go install github.com/segmentio/golines@latest -} - -function seal::format::golines::validate() { - # shellcheck disable=SC2046 - if [[ -n "$(command -v $(seal::format::golines::bin))" ]]; then - return 0 - fi - - seal::log::info "installing golines" - if seal::format::golines::install; then - return 0 - fi - seal::log::error "no golines available" - return 1 -} - -function seal::format::golines::bin() { - local bin="golines" - if [[ -f "${ROOT_DIR}/.sbin/golines" ]]; then - bin="${ROOT_DIR}/.sbin/golines" - fi - echo -n "${bin}" -} - -# install wsl(Whitespace Linter) -function seal::format::wsl::install() { - GOBIN="${ROOT_DIR}/.sbin" go install github.com/bombsimon/wsl/v4/cmd...@master -} - -function seal::format::wsl::validate() { - # shellcheck disable=SC2046 - if [[ -n "$(command -v $(seal::format::wsl::bin))" ]]; then - return 0 - fi - - seal::log::info "installing wsl" - if seal::format::wsl::install; then - return 0 - fi - seal::log::error "no wsl available" - return 1 -} - -function seal::format::wsl::bin() { - local bin="wsl" - if [[ -f "${ROOT_DIR}/.sbin/wsl" ]]; then - bin="${ROOT_DIR}/.sbin/wsl" - fi - echo -n "${bin}" -} - -function seal::format::run() { - local path=$1 - shift 1 - # shellcheck disable=SC2206 - local path_ignored=(${*}) - - # goimports - if ! seal::format::goimports::validate; then - seal::log::fatal "cannot execute goimports as client is not found" +function seal::lint::run() { + if ! seal::lint::goimports_reviser::validate; then + seal::log::fatal "cannot execute goimports-reviser as client is not found" fi - # shellcheck disable=SC2155 local goimports_opts=( "-rm-unused" - "-set-alias" "-use-cache" + "-recursive" "-imports-order=std,general,company,project,blanked,dotted" "-output=file" ) - set +e - if [[ ${#path_ignored[@]} -gt 0 ]]; then - seal::log::debug "pushd ${path}; go list -f \"{{.Dir}}\" ./... | grep -v -E \"$(seal::util::join_array "|" "${path_ignored[@]}")\" | xargs goimports-reviser ${goimports_opts[*]}; popd" - [[ "${path}" == "${ROOT_DIR}" ]] || pushd "${path}" >/dev/null 2>&1 - go list -f "{{.Dir}}" ./... | grep -v -E "$(seal::util::join_array "|" "${path_ignored[@]}")" | xargs "$(seal::format::goimports::bin)" "${goimports_opts[@]}" - [[ "${path}" == "${ROOT_DIR}" ]] || popd >/dev/null 2>&1 - else - seal::log::debug "pushd ${path}; go list -f \"{{.Dir}}\" ./... | xargs goimports-reviser ${goimports_opts[*]}; popd" - [[ "${path}" == "${ROOT_DIR}" ]] || pushd "${path}" >/dev/null 2>&1 - go list -f "{{.Dir}}" ./... | xargs "$(seal::format::goimports::bin)" "${goimports_opts[@]}" - [[ "${path}" == "${ROOT_DIR}" ]] || popd >/dev/null 2>&1 - fi - set -e - - # gofmt interface{} -> any - local gofmt_opts=( - "-w" - "-r" - "interface{} -> any" - ) - if [[ ${#path_ignored[@]} -gt 0 ]]; then - seal::log::debug "pushd ${path}; go list -f \"{{.Dir}}\" ./... | grep -v -E \"$(seal::util::join_array "|" "${path_ignored[@]}")\" | xargs gofmt ${gofmt_opts[*]}; popd" - [[ "${path}" == "${ROOT_DIR}" ]] || pushd "${path}" >/dev/null 2>&1 - go list -f "{{.Dir}}" ./... | grep -v -E "$(seal::util::join_array "|" "${path_ignored[@]}")" | xargs gofmt "${gofmt_opts[@]}" - [[ "${path}" == "${ROOT_DIR}" ]] || popd >/dev/null 2>&1 - else - seal::log::debug "pushd ${path}; go list -f \"{{.Dir}}\" ./... | xargs gofmt ${gofmt_opts[*]}; popd" - [[ "${path}" == "${ROOT_DIR}" ]] || pushd "${path}" >/dev/null 2>&1 - go list -f "{{.Dir}}" ./... | xargs gofmt "${gofmt_opts[@]}" - [[ "${path}" == "${ROOT_DIR}" ]] || popd >/dev/null 2>&1 - fi - - # golines - if ! seal::format::golines::validate; then - seal::log::fatal "cannot execute golines as client is not found" - fi - - # gofumpt for golines base-formatter - if ! seal::format::gofumpt::validate; then - seal::log::fatal "cannot execute gofumpt as client is not found" + local goimports_excludes=() + for arg in "$@"; do + if [[ "${arg}" == "--skip-dirs="* ]]; then + arg="${arg//--skip-dirs=/}" + goimports_excludes+=("${arg}") + fi + done + if [[ -n "${goimports_excludes[*]}" ]]; then + goimports_opts+=("-excludes=$(seal::util::join_array "," "${goimports_excludes[*]}")") fi + goimport_target="${*:$#}" + goimports_opts+=("${goimport_target//\/.../}") - local golines_opts=( - "-w" - "--max-len=120" - "--no-reformat-tags" - "--ignore-generated" # file start with generated_ - "--ignored-dirs=.git" - "--ignored-dirs=node_modules" - "--ignored-dirs=vendor" - ) - for ig in "${path_ignored[@]}"; do - golines_opts+=("--ignored-dirs=${ig}") - done - golines_opts+=( - "--base-formatter=$(seal::format::gofumpt::bin) -extra" # format by gofumpt - "${path}" - ) - seal::log::debug "golines ${golines_opts[*]}" - $(seal::format::golines::bin) "${golines_opts[@]}" + seal::log::debug "goimports-reviser ${goimports_opts[*]}" + "$(seal::lint::goimports_reviser::bin)" "${goimports_opts[@]}" - # wsl - if ! seal::format::wsl::validate; then - seal::log::fatal "cannot execute wsl as client is not found" + if ! seal::lint::golangci_lint::validate; then + seal::log::fatal "cannot execute golangci-lint as client is not found" fi - local wsl_opts=( - "--allow-assign-and-anything" - "--allow-trailing-comment" - "--force-short-decl-cuddling=false" + local golangci_lint_opts=( "--fix" ) - set +e - if [[ ${#path_ignored[@]} -gt 0 ]]; then - seal::log::debug "pushd ${path}; go list ./... | grep -v -E \"$(seal::util::join_array "|" "${path_ignored[@]}")\" | xargs wsl ${wsl_opts[*]}; popd" - [[ "${path}" == "${ROOT_DIR}" ]] || pushd "${path}" >/dev/null 2>&1 - go list ./... | grep -v -E "$(seal::util::join_array "|" "${path_ignored[@]}")" | xargs "$(seal::format::wsl::bin)" "${wsl_opts[@]}" >/dev/null 2>&1 - [[ "${path}" == "${ROOT_DIR}" ]] || popd >/dev/null 2>&1 - else - seal::log::debug "pushd ${path}; go list ./... | xargs wsl ${wsl_opts[*]}; popd" - [[ "${path}" == "${ROOT_DIR}" ]] || pushd "${path}" >/dev/null 2>&1 - go list ./... | xargs "$(seal::format::wsl::bin)" "${wsl_opts[@]}" - [[ "${path}" == "${ROOT_DIR}" ]] || popd >/dev/null 2>&1 - fi - set -e + seal::log::debug "golangci-lint run ${golangci_lint_opts[*]} $*" + $(seal::lint::golangci_lint::bin) run "${golangci_lint_opts[@]}" "$@" } function seal::commit::commitsar::install() { diff --git a/hack/lint.sh b/hack/lint.sh index 4068187..d4b97b4 100755 --- a/hack/lint.sh +++ b/hack/lint.sh @@ -26,12 +26,18 @@ function lint() { [[ "${path}" == "${ROOT_DIR}" ]] || pushd "${path}" >/dev/null 2>&1 - seal::format::run "${path}" "${path_ignored}" + local golangci_lint_opts=() if [[ ${#build_tags[@]} -gt 0 ]]; then - GOLANGCI_LINT_CACHE="$(go env GOCACHE)/golangci-lint" seal::lint::run --build-tags="\"${build_tags[*]}\"" "${path}/..." - else - GOLANGCI_LINT_CACHE="$(go env GOCACHE)/golangci-lint" seal::lint::run "${path}/..." + golangci_lint_opts+=("--build-tags=\"${build_tags[*]}\"") + fi + if [[ -n "${path_ignored}" ]]; then + IFS=" " read -r -a ignored_path <<<"${path_ignored}" + for ig in "${ignored_path[@]}"; do + golangci_lint_opts+=("--skip-dirs=${ig}") + done fi + golangci_lint_opts+=("${path}/...") + GOLANGCI_LINT_CACHE="$(go env GOCACHE)/golangci-lint" seal::lint::run "${golangci_lint_opts[@]}" [[ "${path}" == "${ROOT_DIR}" ]] || popd >/dev/null 2>&1 } diff --git a/pkg/tap/apply.go b/pkg/tap/apply.go index cb572ea..60f8fc0 100644 --- a/pkg/tap/apply.go +++ b/pkg/tap/apply.go @@ -3,9 +3,8 @@ package tap import ( "fmt" - "golang.org/x/exp/slices" - "github.com/hashicorp/terraform/configs" + "golang.org/x/exp/slices" ) // Apply applies the tap configuration to the Terraform configuration. diff --git a/pkg/tap/load.go b/pkg/tap/load.go index 412ac94..b7bc71d 100644 --- a/pkg/tap/load.go +++ b/pkg/tap/load.go @@ -5,11 +5,10 @@ import ( "os" "strings" - "github.com/spf13/afero" - "github.com/hashicorp/hcl/v2" "github.com/hashicorp/hcl/v2/gohcl" "github.com/hashicorp/hcl/v2/hclparse" + "github.com/spf13/afero" "github.com/seal-io/tap/utils/pointer" ) diff --git a/pkg/terraform/load.go b/pkg/terraform/load.go index 8cf7d37..5953847 100644 --- a/pkg/terraform/load.go +++ b/pkg/terraform/load.go @@ -6,10 +6,9 @@ import ( "os" "path/filepath" - "github.com/spf13/afero" - "github.com/hashicorp/terraform/configs" "github.com/hashicorp/terraform/modsdir" + "github.com/spf13/afero" ) type Config = configs.Config diff --git a/pkg/terraform/load_overrider.go b/pkg/terraform/load_overrider.go index 5e3d023..51e706d 100644 --- a/pkg/terraform/load_overrider.go +++ b/pkg/terraform/load_overrider.go @@ -5,7 +5,6 @@ import ( "github.com/hashicorp/hcl/v2" "github.com/hashicorp/hcl/v2/hclsyntax" - "github.com/hashicorp/terraform/configs" ) diff --git a/pkg/terraform/load_walker.go b/pkg/terraform/load_walker.go index 6849727..47d4fea 100644 --- a/pkg/terraform/load_walker.go +++ b/pkg/terraform/load_walker.go @@ -4,9 +4,7 @@ import ( "fmt" "github.com/hashicorp/go-version" - "github.com/hashicorp/hcl/v2" - "github.com/hashicorp/terraform/configs" "github.com/hashicorp/terraform/modsdir" ) diff --git a/pkg/terraform/write.go b/pkg/terraform/write.go index 8a32994..07d2ccb 100644 --- a/pkg/terraform/write.go +++ b/pkg/terraform/write.go @@ -4,11 +4,10 @@ import ( "io" "sort" - "github.com/zclconf/go-cty/cty" - "github.com/hashicorp/hcl/v2/hclsyntax" "github.com/hashicorp/hcl/v2/hclwrite" "github.com/hashicorp/terraform/configs" + "github.com/zclconf/go-cty/cty" ) // Write writes the given Config to the given writer.