From aa9c584fac3bf6f219c202e24bd0a17db8fcf23a Mon Sep 17 00:00:00 2001 From: Dmitrii Stepanov Date: Wed, 9 Oct 2024 16:04:30 +0300 Subject: [PATCH] [#11] makefile: Add static code analyzers Signed-off-by: Dmitrii Stepanov --- .golangci.yml | 75 ++++++++++++++++++++++++++++++++++++++++++++++ Makefile | 83 +++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 153 insertions(+), 5 deletions(-) create mode 100644 .golangci.yml diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..548696b --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,75 @@ +# This file contains all available configuration options +# with their default values. + +# options for analysis running +run: + # timeout for analysis, e.g. 30s, 5m, default is 1m + timeout: 20m + + # include test files or not, default is true + tests: false + +# output configuration options +output: + # colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number" + formats: + - format: tab + +# all available settings of specific linters +linters-settings: + exhaustive: + # indicates that switch statements are to be considered exhaustive if a + # 'default' case is present, even if all enum members aren't listed in the + # switch + default-signifies-exhaustive: true + govet: + # report about shadowed variables + check-shadowing: false + staticcheck: + checks: ["all", "-SA1019"] # TODO Enable SA1019 after deprecated warning are fixed. + funlen: + lines: 80 # default 60 + statements: 60 # default 40 + gocognit: + min-complexity: 40 # default 30 + unused: + field-writes-are-uses: false + exported-fields-are-used: false + local-variables-are-used: false + +linters: + enable: + # mandatory linters + - govet + - revive + + # some default golangci-lint linters + - errcheck + - gosimple + - godot + - ineffassign + - staticcheck + - typecheck + - unused + + # extra linters + - bidichk + - durationcheck + - exhaustive + - copyloopvar + - gofmt + - goimports + - misspell + - predeclared + - reassign + - whitespace + - containedctx + - funlen + - gocognit + - contextcheck + - importas + - perfsprint + - testifylint + - protogetter + disable-all: true + fast: false diff --git a/Makefile b/Makefile index b9bf237..2518fb5 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,82 @@ -integration-test: - # TODO figure out needed capabilities - sudo go test -count=1 -v ./... -tags=integration +#!/usr/bin/make -f + +STATICCHECK_VERSION ?= 2024.1.1 +LINT_VERSION ?= 1.60.3 + +BIN = bin +OUTPUT_LINT_DIR ?= $(abspath $(BIN))/linters +LINT_DIR = $(OUTPUT_LINT_DIR)/golangci-lint-$(LINT_VERSION) +TMP_DIR := .cache + +STATICCHECK_DIR ?= $(abspath $(BIN))/staticcheck +STATICCHECK_VERSION_DIR ?= $(STATICCHECK_DIR)/$(STATICCHECK_VERSION) + +GOFUMPT_VERSION ?= v0.7.0 +GOFUMPT_DIR ?= $(abspath $(BIN))/gofumpt +GOFUMPT_VERSION_DIR ?= $(GOFUMPT_DIR)/$(GOFUMPT_VERSION) + +GOPLS_VERSION ?= v0.16.2 +GOPLS_DIR ?= $(abspath $(BIN))/gopls +GOPLS_VERSION_DIR ?= $(GOPLS_DIR)/$(GOPLS_VERSION) +GOPLS_TEMP_FILE := $(shell mktemp) test: go test -count=1 -v ./... -patch-example: - gopatch -d -p ./multinet.patch ./testdata/patch* \ No newline at end of file +# Install linters +lint-install: + @rm -rf $(OUTPUT_LINT_DIR) + @mkdir -p $(OUTPUT_LINT_DIR) + @CGO_ENABLED=1 GOBIN=$(LINT_DIR) go install -trimpath github.com/golangci/golangci-lint/cmd/golangci-lint@v$(LINT_VERSION) + +# Run linters +lint: + @if [ ! -d "$(LINT_DIR)" ]; then \ + make lint-install; \ + fi + $(LINT_DIR)/golangci-lint run + +# Install staticcheck +staticcheck-install: + @rm -rf $(STATICCHECK_DIR) + @mkdir $(STATICCHECK_DIR) + @GOBIN=$(STATICCHECK_VERSION_DIR) go install honnef.co/go/tools/cmd/staticcheck@$(STATICCHECK_VERSION) + +# Run staticcheck +staticcheck-run: + @if [ ! -d "$(STATICCHECK_VERSION_DIR)" ]; then \ + make staticcheck-install; \ + fi + @$(STATICCHECK_VERSION_DIR)/staticcheck ./... + +# Install gopls +gopls-install: + @rm -rf $(GOPLS_DIR) + @mkdir $(GOPLS_DIR) + @GOBIN=$(GOPLS_VERSION_DIR) go install golang.org/x/tools/gopls@$(GOPLS_VERSION) + +# Run gopls +gopls-run: + @if [ ! -d "$(GOPLS_VERSION_DIR)" ]; then \ + make gopls-install; \ + fi + $(GOPLS_VERSION_DIR)/gopls check $(SOURCES) 2>&1 >$(GOPLS_TEMP_FILE) + @if [[ $$(wc -l < $(GOPLS_TEMP_FILE)) -ne 0 ]]; then \ + cat $(GOPLS_TEMP_FILE); \ + exit 1; \ + fi + rm $(GOPLS_TEMP_FILE) + +# Install gofumpt +fumpt-install: + @rm -rf $(GOFUMPT_DIR) + @mkdir $(GOFUMPT_DIR) + @GOBIN=$(GOFUMPT_VERSION_DIR) go install mvdan.cc/gofumpt@$(GOFUMPT_VERSION) + +# Run gofumpt +fumpt: + @if [ ! -d "$(GOFUMPT_VERSION_DIR)" ]; then \ + make fumpt-install; \ + fi + @echo "⇒ Processing gofumpt check" + $(GOFUMPT_VERSION_DIR)/gofumpt -l -w . \ No newline at end of file