From fd8523aa08a22aa36165f83329d687494d2d125e Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Tue, 28 Feb 2023 15:19:27 +0100 Subject: [PATCH] golangci-lint: enable running specific linters To run just a specific linter based on command line flags, the default configuration needs to be disabled (because it would enable additional ones) and then command line flags must be passed through to "golangci-lint run". For example, to lint with just "go vet" in verbose mode, use: verify-golangci-lint.sh -c none -- --disable-all --enable=govet -v --- hack/verify-golangci-lint.sh | 52 +++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/hack/verify-golangci-lint.sh b/hack/verify-golangci-lint.sh index 11ab9ec53d7..d2b785da1a3 100755 --- a/hack/verify-golangci-lint.sh +++ b/hack/verify-golangci-lint.sh @@ -16,7 +16,16 @@ # This script checks coding style for go language files in each # Kubernetes package by golint. -# Usage: `hack/verify-golangci-lint.sh`. + +usage () { + cat <&2 +Usage: $0 [-- ] [packages]" + -c : use the specified configuration or none instead of the default hack/golangci.yaml + [packages]: check specific packages or directories instead of everything +EOF + exit 1 +} + set -o errexit set -o nounset @@ -41,7 +50,35 @@ invocation=(./hack/verify-golangci-lint.sh "$@") # _output/local/bin/golangci-lint cache clean golangci=(env LOGCHECK_CONFIG="${KUBE_ROOT}/hack/logcheck.conf" "${GOBIN}/golangci-lint" run) golangci_config="${KUBE_ROOT}/hack/golangci.yaml" -golangci+=(--config="${golangci_config}") +while getopts "c:" o; do + case "${o}" in + c) + if [ "${OPTARG}" = "none" ]; then + golangci_config="" + else + golangci_config="${OPTARG}" + fi + ;; + *) + usage + ;; + esac +done + +if [ "${golangci_config}" ]; then + golangci+=(--config="${golangci_config}") +fi + +# Filter out arguments that start with "-" and move them to the run flags. +shift $((OPTIND-1)) +targets=() +for arg; do + if [[ "${arg}" == -* ]]; then + golangci+=("${arg}") + else + targets+=("${arg}") + fi +done kube::golang::verify_go_version @@ -52,15 +89,18 @@ export GO111MODULE=on echo "installing golangci-lint and logcheck plugin from hack/tools into ${GOBIN}" pushd "${KUBE_ROOT}/hack/tools" >/dev/null go install github.com/golangci/golangci-lint/cmd/golangci-lint - go build -o "${GOBIN}/logcheck.so" -buildmode=plugin sigs.k8s.io/logtools/logcheck/plugin + if [ "${golangci_config}" ]; then + # This cannot be used without a config. + go build -o "${GOBIN}/logcheck.so" -buildmode=plugin sigs.k8s.io/logtools/logcheck/plugin + fi popd >/dev/null cd "${KUBE_ROOT}" res=0 -if [[ "$#" -gt 0 ]]; then - echo "running ${golangci[*]} $*" >&2 - "${golangci[@]}" "$@" >&2 || res=$? +if [[ "${#targets[@]}" -gt 0 ]]; then + echo "running ${golangci[*]} ${targets[*]}" >&2 + "${golangci[@]}" "${targets[@]}" >&2 || res=$? else echo "running ${golangci[*]} ./..." >&2 "${golangci[@]}" ./... >&2 || res=$?