aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.golangci.yaml118
-rw-r--r--Makefile96
-rw-r--r--Makefile.maker.yaml17
3 files changed, 200 insertions, 31 deletions
diff --git a/.golangci.yaml b/.golangci.yaml
new file mode 100644
index 0000000..08a5f85
--- /dev/null
+++ b/.golangci.yaml
@@ -0,0 +1,118 @@
+################################################################################
+# This file is AUTOGENERATED with <https://github.com/sapcc/go-makefile-maker> #
+# Edit Makefile.maker.yaml instead. #
+################################################################################
+
+run:
+ deadline: 3m # 1m by default
+ modules-download-mode: readonly
+
+output:
+ # Do not print lines of code with issue.
+ print-issued-lines: false
+
+issues:
+ exclude:
+ # It is idiomatic Go to reuse the name 'err' with ':=' for subsequent errors.
+ # Ref: https://go.dev/doc/effective_go#redeclaration
+ - 'declaration of "err" shadows declaration at'
+ exclude-rules:
+ - path: _test\.go
+ linters:
+ - bodyclose
+ # '0' disables the following options.
+ max-issues-per-linter: 0
+ max-same-issues: 0
+
+linters-settings:
+ dupl:
+ # Tokens count to trigger issue, 150 by default.
+ threshold: 100
+ errcheck:
+ # Report about assignment of errors to blank identifier.
+ check-blank: true
+ # Report about not checking of errors in type assertions.
+ check-type-assertions: true
+ forbidigo:
+ forbid:
+ # ioutil package has been deprecated: https://github.com/golang/go/issues/42026
+ - ^ioutil\..*$
+ gocritic:
+ enabled-checks:
+ - boolExprSimplify
+ - builtinShadow
+ - emptyStringTest
+ - evalOrder
+ - httpNoBody
+ - importShadow
+ - initClause
+ - methodExprCall
+ - paramTypeCombine
+ - preferFilepathJoin
+ - ptrToRefParam
+ - redundantSprint
+ - returnAfterHttpError
+ - stringConcatSimplify
+ - timeExprSimplify
+ - truncateCmp
+ - typeAssertChain
+ - typeUnparen
+ - unnamedResult
+ - unnecessaryBlock
+ - unnecessaryDefer
+ - weakCond
+ - yodaStyleExpr
+ goimports:
+ # Put local imports after 3rd-party packages.
+ local-prefixes: github.com/majewsky/schwift
+ gosec:
+ excludes:
+ # gosec wants us to set a short ReadHeaderTimeout to avoid Slowloris attacks, but doing so would expose us to Keep-Alive race conditions (see https://iximiuz.com/en/posts/reverse-proxy-http-keep-alive-and-502s/)
+ - G112
+ # created file permissions are restricted by umask if necessary
+ - G306
+ govet:
+ # Report about shadowed variables.
+ check-shadowing: true
+ nolintlint:
+ require-specific: true
+ usestdlibvars:
+ http-method: true
+ http-status-code: true
+ time-weekday: true
+ time-month: true
+ time-layout: true
+ crypto-hash: true
+ default-rpc-path: true
+ whitespace:
+ # Enforce newlines (or comments) after multi-line function signatures.
+ multi-func: true
+
+linters:
+ # We use 'disable-all' and enable linters explicitly so that a newer version
+ # does not introduce new linters unexpectedly.
+ disable-all: true
+ enable:
+ - dupl
+ - errcheck
+ - exportloopref
+ - forbidigo
+ - gocritic
+ - gofmt
+ - goimports
+ - gosec
+ - gosimple
+ - govet
+ - ineffassign
+ - misspell
+ - nolintlint
+ - rowserrcheck
+ - sqlclosecheck
+ - staticcheck
+ - stylecheck
+ - typecheck
+ - unconvert
+ - unparam
+ - unused
+ - usestdlibvars
+ - whitespace
diff --git a/Makefile b/Makefile
index b955ad6..27b8766 100644
--- a/Makefile
+++ b/Makefile
@@ -1,13 +1,19 @@
-help:
- @echo 'Available targets:'
- @echo ' make generate'
- @echo ' make test'
+################################################################################
+# This file is AUTOGENERATED with <https://github.com/sapcc/go-makefile-maker> #
+# Edit Makefile.maker.yaml instead. #
+################################################################################
-GO_BUILDFLAGS =
-GO_LDFLAGS =
-GO_TESTENV =
+MAKEFLAGS=--warn-undefined-variables
+# /bin/sh is dash on Debian which does not support all features of ash/bash
+# to fix that we use /bin/bash only on Debian to not break Alpine
+ifneq (,$(wildcard /etc/os-release)) # check file existence
+ ifneq ($(shell grep -c debian /etc/os-release),0)
+ SHELL := /bin/bash
+ endif
+endif
-################################################################################
+default: FORCE
+ @echo 'There is nothing to build, use `make check` for running the test suite or `make help` for a list of available targets.'
generate: generated.go
@@ -15,40 +21,68 @@ generate: generated.go
@echo ./util/render_template.go < $< > $@
@./util/render_template.go < $< > $@.new && mv $@.new $@ || (rm $@.new; false)
-################################################################################
-
-test: static-tests cover.html
- @printf "\e[1;32m>> All tests successful.\e[0m\n"
+GO_BUILDFLAGS =
+GO_LDFLAGS =
+GO_TESTENV =
-# which packages to test with static checkers
-GO_ALLPKGS := $(shell go list ./... | grep -v '/util')
-# which files to test with static checkers (this contains a list of globs)
-GO_ALLFILES := $(addsuffix /*.go,$(patsubst $(shell go list .),.,$(GO_ALLPKGS)))
# which packages to test with "go test"
-GO_TESTPKGS := $(shell go list -f '{{if .TestGoFiles}}{{.ImportPath}}{{end}}' ./... | grep -v '/util')
+GO_TESTPKGS := $(shell go list -f '{{if or .TestGoFiles .XTestGoFiles}}{{.ImportPath}}{{end}}' ./...)
# which packages to measure coverage for
GO_COVERPKGS := $(shell go list ./... | grep -Ev '/util')
# to get around weird Makefile syntax restrictions, we need variables containing a space and comma
space := $(null) $(null)
comma := ,
-static-tests: FORCE
- @if ! hash golint 2>/dev/null; then printf "\e[1;36m>> Installing golint...\e[0m\n"; GO111MODULE=off go get -u golang.org/x/lint/golint; fi
- @printf "\e[1;36m>> gofmt\e[0m\n"
- @if s="$$(gofmt -s -d $(GO_ALLFILES) 2>/dev/null)" && test -n "$$s"; then echo "$$s"; false; fi
- @printf "\e[1;36m>> golint\e[0m\n"
- @if s="$$(golint $(GO_ALLPKGS) 2>/dev/null)" && test -n "$$s"; then echo "$$s"; false; fi
- @printf "\e[1;36m>> go vet\e[0m\n"
- @go vet $(GO_BUILDFLAGS) $(GO_ALLPKGS)
+check: FORCE static-check build/cover.html
+ @printf "\e[1;32m>> All checks successful.\e[0m\n"
+
+prepare-static-check: FORCE
+ @if ! hash golangci-lint 2>/dev/null; then printf "\e[1;36m>> Installing golangci-lint (this may take a while)...\e[0m\n"; go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest; fi
-cover.out: FORCE
+static-check: FORCE prepare-static-check
+ @printf "\e[1;36m>> golangci-lint\e[0m\n"
+ @golangci-lint run
+
+build/cover.out: FORCE | build
@printf "\e[1;36m>> go test\e[0m\n"
- @env $(GO_TESTENV) go test $(GO_BUILDFLAGS) -ldflags '-s -w $(GO_LDFLAGS)' -p 1 -coverprofile=$@ -covermode=count -coverpkg=$(subst $(space),$(comma),$(GO_COVERPKGS)) $(GO_TESTPKGS)
+ @env $(GO_TESTENV) go test $(GO_BUILDFLAGS) -ldflags '-s -w $(GO_LDFLAGS)' -shuffle=on -p 1 -coverprofile=$@ -covermode=count -coverpkg=$(subst $(space),$(comma),$(GO_COVERPKGS)) $(GO_TESTPKGS)
-cover.html: cover.out
- @printf "\e[1;36m>> go tool cover > $@\e[0m\n"
- @go tool cover -html=$< -o $@
+build/cover.html: build/cover.out
+ @printf "\e[1;36m>> go tool cover > build/cover.html\e[0m\n"
+ @go tool cover -html $< -o $@
-################################################################################
+build:
+ @mkdir $@
+
+tidy-deps: FORCE
+ go mod tidy
+ go mod verify
+
+license-headers: FORCE
+ @if ! hash addlicense 2>/dev/null; then printf "\e[1;36m>> Installing addlicense...\e[0m\n"; go install github.com/google/addlicense@latest; fi
+ find * \( -name vendor -type d -prune \) -o \( -name \*.go -exec addlicense -c "SAP SE" -- {} + \)
+
+clean: FORCE
+ git clean -dxf build
+
+help: FORCE
+ @printf "\n"
+ @printf "\e[1mUsage:\e[0m\n"
+ @printf " make \e[36m<target>\e[0m\n"
+ @printf "\n"
+ @printf "\e[1mGeneral\e[0m\n"
+ @printf " \e[36mhelp\e[0m Display this help.\n"
+ @printf "\n"
+ @printf "\e[1mTest\e[0m\n"
+ @printf " \e[36mcheck\e[0m Run the test suite (unit tests and golangci-lint).\n"
+ @printf " \e[36mprepare-static-check\e[0m Install golangci-lint. This is used in CI, you should probably install golangci-lint using your package manager.\n"
+ @printf " \e[36mstatic-check\e[0m Run golangci-lint.\n"
+ @printf " \e[36mbuild/cover.out\e[0m Run tests and generate coverage report.\n"
+ @printf " \e[36mbuild/cover.html\e[0m Generate an HTML file with source code annotations from the coverage report.\n"
+ @printf "\n"
+ @printf "\e[1mDevelopment\e[0m\n"
+ @printf " \e[36mtidy-deps\e[0m Run go mod tidy and go mod verify.\n"
+ @printf " \e[36mlicense-headers\e[0m Add license headers to all .go files excluding the vendor directory.\n"
+ @printf " \e[36mclean\e[0m Run git clean.\n"
.PHONY: FORCE
diff --git a/Makefile.maker.yaml b/Makefile.maker.yaml
new file mode 100644
index 0000000..1875a35
--- /dev/null
+++ b/Makefile.maker.yaml
@@ -0,0 +1,17 @@
+# Configuration file for <https://github.com/sapcc/go-makefile-maker>
+
+metadata:
+ url: https://github.com/majewsky/schwift
+
+coverageTest:
+ except: '/util'
+
+golangciLint:
+ createConfig: true
+
+verbatim: |
+ generate: generated.go
+
+ %: %.in | util/render_template.go
+ @echo ./util/render_template.go < $< > $@
+ @./util/render_template.go < $< > $@.new && mv $@.new $@ || (rm $@.new; false)