From 911aa346a1c547bc5713bd1bc8b6dcf4f60f3285 Mon Sep 17 00:00:00 2001 From: Daniel Hiller Date: Thu, 12 Dec 2024 17:00:19 +0100 Subject: [PATCH] lint: add make target lint Adds the lint make target and the initial .golangci-lint configuration. Also it adds //nolint directive to some of the files Signed-off-by: Daniel Hiller --- .golangci.yaml | 14 +++++++++ Makefile | 27 +++++++++++++++-- hack/lint.sh | 30 +++++++++++++++++++ releng/release-tool/release-tool.go | 4 +-- robots/pkg/kubevirt/cmd/get/support-matrix.go | 2 +- 5 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 .golangci.yaml create mode 100755 hack/lint.sh diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 0000000000..7191115291 --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,14 @@ +linters: + # initially we disable all linters, since this is an old code base with quite a lot of issues + disable-all: true + enable: + - gocyclo +# - staticcheck +# - unused +# - ineffassign +# - govet +# - gosimple +# - errcheck +#linters-settings: +# gocyclo: +# min-complexity: 10 diff --git a/Makefile b/Makefile index 89f4ad4281..eddc4f9aa1 100644 --- a/Makefile +++ b/Makefile @@ -5,11 +5,28 @@ querier := robots/cmd/release-querier kubevirtci := robots/cmd/kubevirtci-bumper bazelbin := bazelisk -.PHONY: all clean deps-update gazelle-update-repos update-labels $(limiter) $(flake-report-writer) $(querier) $(kubevirtci) $(flake-issue-creator) +ifndef GOLANGCI_LINT_VERSION + GOLANGCI_LINT_VERSION=v1.62.2 + export GOLANGCI_LINT_VERSION +endif +ifndef ARTIFACTS + ARTIFACTS=/tmp/artifacts + export ARTIFACTS +endif +ifndef COVERAGE_OUTPUT_PATH + COVERAGE_OUTPUT_PATH=${ARTIFACTS}/coverage.html + export COVERAGE_OUTPUT_PATH +endif + +.PHONY: all make-artifacts-dir clean deps-update gazelle-update-repos update-labels install-metrics-binaries lint $(limiter) $(flake-report-writer) $(querier) $(kubevirtci) $(flake-issue-creator) all: deps-update $(limiter) $(flake-report-writer) $(querier) $(kubevirtci) $(flake-issue-creator) -clean: +make-artifacts-dir: + mkdir -p ${ARTIFACTS} + +clean: install-metrics-binaries $(bazelbin) clean + golangci-lint cache clean $(limiter) $(flake-report-writer) $(querier) $(kubevirtci) $(flake-issue-creator): deps-update $(MAKE) --directory=$@ @@ -40,3 +57,9 @@ test: update-labels: ./hack/labels/update.sh + +install-metrics-binaries: + if ! command -V golangci-lint; then curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b ${GOPATH}/bin ${GOLANGCI_LINT_VERSION} ; fi + +lint: install-metrics-binaries + ./hack/lint.sh diff --git a/hack/lint.sh b/hack/lint.sh new file mode 100755 index 0000000000..aac370994f --- /dev/null +++ b/hack/lint.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash +# +# This file is part of the KubeVirt project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Copyright the KubeVirt Authors. +# +# + +set -e +set -u +set -o pipefail + +golangci-lint --version +golangci-lint run + +if [ $(git grep '//nolint' -- '**/*.go' | wc -l) -gt 0 ]; then + echo 'WARNING: nolint directive detected'; +fi diff --git a/releng/release-tool/release-tool.go b/releng/release-tool/release-tool.go index aa207ab1dd..5ceb52ac93 100644 --- a/releng/release-tool/release-tool.go +++ b/releng/release-tool/release-tool.go @@ -772,7 +772,7 @@ func (r *releaseData) getReleases() ([]*github.RepositoryRelease, error) { return r.allReleases, nil } -func (r *releaseData) autoDetectData(autoReleaseCadance string, autoPromoteAfterDays int) error { +func (r *releaseData) autoDetectData(autoReleaseCadance string, autoPromoteAfterDays int) error { //nolint:gocyclo log.Printf("Attempting to auto detect release for %s/%s", r.org, r.repo) @@ -975,7 +975,7 @@ func (r *releaseData) autoDetectData(autoReleaseCadance string, autoPromoteAfter return nil } -func (r *releaseData) verifyTag() error { +func (r *releaseData) verifyTag() error { //nolint:gocyclo // must be a valid semver version tagSemver, err := semver.NewVersion(r.tag) if err != nil { diff --git a/robots/pkg/kubevirt/cmd/get/support-matrix.go b/robots/pkg/kubevirt/cmd/get/support-matrix.go index 89d9661a30..613ce3dd0a 100644 --- a/robots/pkg/kubevirt/cmd/get/support-matrix.go +++ b/robots/pkg/kubevirt/cmd/get/support-matrix.go @@ -108,7 +108,7 @@ type SupportMatrixTemplateData struct { MapK6tToK8sVersions map[string]map[string]bool } -func GenerateMarkdownForSupportMatrix(_ *cobra.Command, _ []string) error { +func GenerateMarkdownForSupportMatrix(_ *cobra.Command, _ []string) error { //nolint:gocyclo if err := getSupportMatrixOpts.validateOptions(); err != nil { return fmt.Errorf("failed to validate options: %v", err) }