From 6931bbd44c2c80c60f3c2e437a90725ef639386f Mon Sep 17 00:00:00 2001 From: thxCode Date: Sun, 4 Feb 2024 11:35:52 +0800 Subject: [PATCH] ci: adjust lint Signed-off-by: thxCode --- .golangci.yaml | 126 +++++++++++-------- hack/lib/style.sh | 245 ++++++++----------------------------- hack/lint.sh | 14 ++- pkg/plugins/azure/token.go | 3 +- 4 files changed, 136 insertions(+), 252 deletions(-) diff --git a/.golangci.yaml b/.golangci.yaml index 01a503e..3e266c2 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -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,22 +76,20 @@ linters: # - funlen linters-settings: - staticcheck: - checks: ["all", "-SA1019", "-SA2002", "-SA5008"] - stylecheck: - checks: ["all", "-ST1003"] - gosec: - severity: "low" - confidence: "low" - excludes: - - G101 - - G112 - 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 @@ -99,32 +98,74 @@ linters-settings: # Default: [] exclude: # Exclude todo and fixme comments. - - "^fixme:" - - "^todo:" + - "(?i)^ FIXME:" + - "(?i)^ TODO:" + - "(?i)^ SPDX\\-License\\-Identifier:" # Check that each sentence ends with a period. # Default: true period: true # 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: @@ -153,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.19" - checks: ["all"] - nakedret: - max-func-lines: 60 usestdlibvars: # Suggest the use of http.MethodXX # Default: true @@ -186,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: @@ -204,4 +233,3 @@ issues: - makezero - lll - funlen - - wsl diff --git a/hack/lib/style.sh b/hack/lib/style.sh index 9d63f3a..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,183 +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" - "${path}" - ) - - seal::log::debug "gofmt ${gofmt_opts[*]}" - gofmt "${gofmt_opts[@]}" - - # 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 b3bc5fb..e0468de 100755 --- a/hack/lint.sh +++ b/hack/lint.sh @@ -27,12 +27,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/plugins/azure/token.go b/pkg/plugins/azure/token.go index e89b565..abf1b41 100644 --- a/pkg/plugins/azure/token.go +++ b/pkg/plugins/azure/token.go @@ -10,9 +10,8 @@ import ( "github.com/Azure/azure-sdk-for-go/sdk/azcore/log" "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" - "k8s.io/klog/v2" - "github.com/Azure/azure-sdk-for-go/sdk/azidentity" + "k8s.io/klog/v2" "github.com/seal-io/kubecia/pkg/cache" "github.com/seal-io/kubecia/pkg/token"