Skip to content

Commit

Permalink
update lint config
Browse files Browse the repository at this point in the history
  • Loading branch information
k15r committed Oct 7, 2024
1 parent fbc4398 commit 61ecf70
Show file tree
Hide file tree
Showing 9 changed files with 1,447 additions and 27 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,4 @@ vendor/

# Lint files
lint-report.json
.golangci.yaml
.act
264 changes: 264 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
linters:
enable-all: true
disable:
- contextcheck # too many false positives
- depguard # checks if package imports are whitelisted
- exhaustruct # TODO enable and fix, use regex to exclude certain cases
- nlreturn # too strict and mostly code is not more readable
- sqlclosecheck # not needed for this project
- wsl # too strict and mostly code is not more readable
- execinquery # deprecated since 1.58.0
- exportloopref # deprecated since 1.60.2
- gomnd
### disabled for now... will be enabled 1 by 1
- ginkgolinter
- lll
- dupl
- ireturn
- maintidx
- prealloc
- testpackage
- wrapcheck
- paralleltest
- perfsprint
- importas
- varnamelen
- tagliatelle
- noctx
- stylecheck
- bodyclose
- gochecknoglobals
- errorlint
- promlinter
- funlen
- forcetypeassert
- gosec
- gomoddirectives
- gocognit
- nestif
- inamedparam
- unparam
- gocritic
- err113

linters-settings:
stylecheck:
dot-import-whitelist:
- github.com/onsi/ginkgo/v2
- github.com/onsi/gomega
revive:
enable-all-rules: false
severity: error
rules:
- name: comment-spacings
disabled: true
- name: dot-imports
severity: warning
disabled: true
- name: line-length-limit
severity: warning
disabled: true
arguments: [ 120 ]
funlen:
lines: 80
cyclop:
max-complexity: 20
nestif:
min-complexity: 6
gci:
sections:
- standard # Standard packages.
- default # Imports that could not be matched to another section type.
- prefix(github.com/kyma-project/kyma-metrics-collector) # Imports with the specified prefix.
- blank # Blank imports.
- dot # Dot imports.
custom-order: true
skip-generated: true
importas:
no-unaliased: false
no-extra-aliases: true
## kubernetes APIs get the prefix `k`. As an example check `kcorev1`

alias:
- pkg: k8s.io/api/core/v1
alias: kcorev1
- pkg: k8s.io/api/apps/v1
alias: kappsv1
- pkg: k8s.io/api/rbac/v1
alias: krbacv1
- pkg: k8s.io/apimachinery/pkg/runtime/schema
alias: kschema
- pkg: k8s.io/apimachinery/pkg/labels
alias: klabels
- pkg: k8s.io/apimachinery/pkg/runtime
alias: kruntime
- pkg: k8s.io/apimachinery/pkg/types
alias: ktypes
- pkg: k8s.io/apimachinery/pkg/apis/meta/v1/unstructured
alias: kunstructured
- pkg: k8s.io/apimachinery/pkg/apis/meta/v1
alias: kmetav1
- pkg: github.com/kyma-project/eventing-manager/api/eventing/v1alpha2
alias: eventingv1alpha2
- pkg: github.com/kyma-project/eventing-manager/api/operator/v1alpha1
alias: operatorv1alpha1
- pkg: github.com/kyma-project/api-gateway/apis/gateway/v1beta1
alias: apigatewayv1beta1
- pkg: k8s.io/client-go/dynamic/fake
alias: kdynamicfake
- pkg: k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset
alias: kapixclientset
- pkg: k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake
alias: kapixclientsetfake
- pkg: k8s.io/apimachinery/pkg/api/errors
alias: kerrors
- pkg: k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1
alias: kapiextensionsv1
- pkg: github.com/kyma-project/eventing-manager/testing
alias: eventingtesting
- pkg: sigs.k8s.io/controller-runtime
alias: kctrl
- pkg: sigs.k8s.io/controller-runtime/pkg/log
alias: kctrllog
- pkg: sigs.k8s.io/controller-runtime/pkg/client
alias: kctrlclient
- pkg: k8s.io/api/autoscaling/v1
alias: kautoscalingv1
- pkg: k8s.io/api/autoscaling/v2
alias: kautoscalingv2
- pkg: k8s.io/api/admissionregistration/v1
alias: kadmissionregistrationv1
- pkg: github.com/cloudevents/sdk-go/v2
alias: cloudevents
- pkg: github.com/cloudevents/sdk-go/protocol/nats/v2
alias: cenats
- pkg: github.com/cloudevents/sdk-go/v2/binding
alias: cebinding
- pkg: github.com/cloudevents/sdk-go/v2/client
alias: ceclient
- pkg: github.com/cloudevents/sdk-go/v2/event
alias: ceevent
- pkg: github.com/cloudevents/sdk-go/v2/protocol
alias: ceprotocol
- pkg: github.com/cloudevents/sdk-go/v2/protocol/http
alias: cehttp
- pkg: github.com/kyma-project/eventing-manager/pkg/backend/(\w+)$
alias: backend$1
- pkg: github.com/kyma-project/eventing-manager/pkg/backend/(\w+)/(\w+)$
alias: backend$1$2
- pkg: github.com/kyma-project/eventing-manager/test/(\w+)$
alias: test$1
- pkg: github.com/kyma-project/eventing-manager/test/(\w+)/(\w+)$
alias: test$1$2
- pkg: istio.io/client-go/pkg/apis/security/v1beta1
alias: istiopkgsecurityv1beta1
- pkg: istio.io/api/(\w+)/(v\d+\w*\d*)$
alias: istio$1$2
- pkg: github.com/nats-io/nats-server/v2/(\w+)$
alias: natsio$1
- pkg: github.com/nats-io/nats.go
alias: natsio
- pkg: github.com/kyma-project/eventing-manager/internal/controller/(\w+)$
alias: controller$1
- pkg: github.com/kyma-project/kyma/common/logging/logger
alias: kymalogger
- pkg: github.com/kyma-project/nats-manager/api/v1alpha1
alias: natsv1alpha1
- pkg: github.com/kyma-project/nats-manager/testutils
alias: natstestutils
- pkg: github.com/kyma-project/eventing-manager/internal/connection/nats
alias: natsconnection
- pkg: github.com/kyma-project/eventing-manager/internal/connection/nats/errors
alias: natsconnectionerrors
- pkg: github.com/kyma-project/eventing-manager/internal/connection/nats/mocks
alias: natsconnectionmocks
- pkg: github.com/kyma-project/eventing-manager/internal/controller/eventing/subscription/(\w+)$
alias: subscriptioncontroller$1
- pkg: github.com/kyma-project/eventing-manager/internal/controller/operator/eventing
alias: eventingcontroller
- pkg: github.com/kyma-project/eventing-manager/internal/controller/operator/eventing/mocks
alias: eventingcontrollermocks
- pkg: github.com/kyma-project/eventing-manager/pkg/ems/api/events/client/mocks
alias: emsclientmocks
- pkg: github.com/kyma-project/eventing-manager/pkg/ems/api/events/types
alias: emstypes
- pkg: github.com/onsi/gomega/types
alias: gomegatypes
- pkg: k8s.io/apimachinery/pkg/util/runtime
alias: kutilruntime
- pkg: k8s.io/client-go/kubernetes/scheme
alias: kkubernetesscheme
- pkg: github.com/kyma-project/eventing-manager/pkg/subscriptionmanager/manager
alias: submgrmanager
- pkg: github.com/kyma-project/eventing-manager/pkg/subscriptionmanager/manager/mocks
alias: submgrmanagermocks
- pkg: github.com/kyma-project/eventing-manager/pkg/subscriptionmanager/mocks
alias: submgrmocks
- pkg: github.com/kyma-project/eventing-manager/pkg/watcher/mocks
alias: watchermocks
- pkg: github.com/kyma-project/eventing-manager/pkg/k8s/mocks
alias: k8smocks
- pkg: github.com/kyma-project/eventing-manager/pkg/eventing/mocks
alias: eventingmocks
- pkg: github.com/kyma-project/eventing-manager/pkg/errors
alias: emerrors
- pkg: github.com/pkg/errors
alias: pkgerrors
- pkg: github.com/kyma-project/eventing-manager/testing/eventmeshsub
alias: eventmeshsubmatchers

ireturn:
allow:
- anon
- error
- empty
- stdlib
- Client
- client.Object
- (or|er)$
wrapcheck:
ignorePackageGlobs:
varnamelen:
ignore-names:
- ok # Ignore "ok" variables as return values of functions
- g # Ignore "g" as it is commonly used for gomega
- a # Ignore as it is used in comparison functions
- b # Ignore as it is used in comparison functions
- tc # Ignore as it is commonly used in table tests
# Ignore "ok" variables that hold the bool return value of a type assertion. (defaults to false)
ignore-type-assert-ok: true
# Ignore "ok" variables that hold the bool return value of a map index. (defaults to false)
ignore-map-index-ok: true
# Ignore "ok" variables that hold the bool return value of a channel receive. (defaults to false)
ignore-chan-recv-ok: true
ignore-decls:
- i int
- g *gomega.WithT
- w http.ResponseWriter
- r *http.Request
- cm *v1.ConfigMap
- cm *kcorev1.ConfigMap
- js JetStream
- js *JetStream
issues:
exclude-rules:
- path: "_test\\.go"
linters:
- wrapcheck
- gochecknoglobals
- funlen # Table driven unit and integration tests exceed function length by design
- maintidx # Table driven unit and integration tests exceed maintainability index by design
- linters:
- importas
text: has alias "" which is not part of config # Ignore false positives that emerged due to https://github.com/julz/importas/issues/15.
- linters:
- lll
source: "^// +kubebuilder: " # Exclude lll issues for long lines starting with kubebuilder marker prefix
max-issues-per-linter: 0
max-same-issues: 0
output:
sort-results: true
run:
timeout: 15m
exclude-files:
- zz_generated.deepcopy.go
50 changes: 31 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,31 @@ else
GOBIN=$(shell go env GOBIN)
endif

##@ Build Dependencies
# Setting SHELL to bash allows bash commands to be executed by recipes.
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
SHELL = /usr/bin/env bash -o pipefail
.SHELLFLAGS = -ec

## Location to install dependencies to
LOCALBIN ?= $(shell pwd)/bin
$(LOCALBIN):
mkdir -p $(LOCALBIN)
SRC_ROOT := $(shell git rev-parse --show-toplevel)
TOOLS_MOD_DIR := $(SRC_ROOT)/internal/tools
TOOLS_MOD_REGEX := "\s+_\s+\".*\""
TOOLS_PKG_NAMES := $(shell grep -E $(TOOLS_MOD_REGEX) < $(TOOLS_MOD_DIR)/tools.go | tr -d " _\"")
TOOLS_BIN_DIR := $(SRC_ROOT)/bin
# Strip off versions (e.g. /v2) from pkg names
TOOLS_PKG_NAMES_CLEAN := $(shell grep -E $(TOOLS_MOD_REGEX) < $(TOOLS_MOD_DIR)/tools.go | tr -d " _\"" | sed "s/\/v[0-9].*$$//")
TOOLS_BIN_NAMES := $(addprefix $(TOOLS_BIN_DIR)/, $(notdir $(TOOLS_PKG_NAMES_CLEAN)))

.PHONY: install-tools
install-tools: $(TOOLS_BIN_NAMES)

$(TOOLS_BIN_DIR):
mkdir -p $@

$(TOOLS_BIN_NAMES): $(TOOLS_BIN_DIR) $(TOOLS_MOD_DIR)/go.mod
cd $(TOOLS_MOD_DIR) && go build -o $@ -trimpath $(filter $(filter %/$(notdir $@),$(TOOLS_PKG_NAMES_CLEAN))%,$(TOOLS_PKG_NAMES))

## Tools
GOLANGCI_LINT := $(TOOLS_BIN_DIR)/golangci-lint

##@ General

Expand All @@ -59,20 +77,20 @@ help: ## Display this help.
##@ Development

.PHONY: lint
lint: golangci-lint ## Check lint issues using `golangci-lint`
$(LOCALBIN)/golangci-lint run
lint: $(GOLANGCI_LINT) ## Check lint issues using `golangci-lint`
$(TOOLS_BIN_DIR)/golangci-lint run

.PHONY: lint-compact
lint-compact: golangci-lint ## Check lint issues using `golangci-lint` in compact result format
$(LOCALBIN)/golangci-lint run --print-issued-lines=false
lint-compact: $(GOLANGCI_LINT) ## Check lint issues using `golangci-lint` in compact result format
$(TOOLS_BIN_DIR)/golangci-lint run --print-issued-lines=false

.PHONY: lint-fix
lint-fix: golangci-lint ## Check and fix lint issues using `golangci-lint`
$(LOCALBIN)/golangci-lint run --fix
lint-fix: $(GOLANGCI_LINT) ## Check and fix lint issues using `golangci-lint`
$(TOOLS_BIN_DIR)/golangci-lint run --fix

.PHONY: lint-report
lint-report: golangci-lint ## Check lint issues using `golangci-lint` then export them to a file, then print the list of linters used
$(LOCALBIN)/golangci-lint run --issues-exit-code 0 --out-format json > ./lint-report.json
lint-report: $(GOLANGCI_LINT)golangci-lint ## Check lint issues using `golangci-lint` then export them to a file, then print the list of linters used
$(TOOLS_BIN_DIR)/golangci-lint run --issues-exit-code 0 --out-format json > ./lint-report.json

.PHONY: lint-report-issue-category
lint-report-issue-category: ## Get lint issues categories
Expand All @@ -94,12 +112,6 @@ lint-report-count-issue: ## Count lint issues
lint-report-clean: ## Clean lint report
rm -f ./lint-report.json

GOLANG_CI_LINT_VERSION ?= v1.61
.PHONY: golangci-lint
golangci-lint:
test -s $(LOCALBIN)/golangci-lint && $(LOCALBIN)/golangci-lint version | grep -q $(GOLANG_CI_LINT_VERSION) || \
GOBIN=$(LOCALBIN) go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANG_CI_LINT_VERSION)

.PHONY: fmt
fmt: ## Reformat files using `go fmt`
go fmt $$($(DIRS_TO_CHECK))
Expand Down
Loading

0 comments on commit 61ecf70

Please sign in to comment.