Skip to content

Commit 7c5e82f

Browse files
authored
Merge pull request #4 from codeGROOVE-dev/lint
Lint it
2 parents 24ab1b0 + 930bd26 commit 7c5e82f

File tree

6 files changed

+698
-247
lines changed

6 files changed

+698
-247
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@ go.work.sum
3030
# Editor/IDE
3131
# .idea/
3232
# .vscode/
33+
34+
# added by lint-install
35+
out/

.golangci.yml

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
## Golden config for golangci-lint - strict, but within the realm of what Go authors might use.
2+
#
3+
# This is tied to the version of golangci-lint listed in the Makefile, usage with other
4+
# versions of golangci-lint will yield errors and/or false positives.
5+
#
6+
# Docs: https://github.com/golangci/golangci-lint/blob/HEAD/.golangci.reference.yml
7+
# Based heavily on https://gist.github.com/maratori/47a4d00457a92aa426dbd48a18776322
8+
9+
version: "2"
10+
11+
issues:
12+
# Maximum issues count per one linter. Set to 0 to disable. Default is 50.
13+
max-issues-per-linter: 0
14+
# Maximum count of issues with the same text. Set to 0 to disable. Default is 3.
15+
max-same-issues: 0
16+
17+
formatters:
18+
enable:
19+
# - gci
20+
# - gofmt
21+
- gofumpt
22+
# - goimports
23+
# - golines
24+
- swaggo
25+
26+
settings:
27+
golines:
28+
# Default: 100
29+
max-len: 120
30+
31+
linters:
32+
default: all
33+
disable:
34+
# linters that give advice contrary to what the Go authors advise
35+
- decorder # checks declaration order and count of types, constants, variables and functions
36+
- dupword # [useless without config] checks for duplicate words in the source code
37+
- exhaustruct # [highly recommend to enable] checks if all structure fields are initialized
38+
- forcetypeassert # [replaced by errcheck] finds forced type assertions
39+
- ginkgolinter # [if you use ginkgo/gomega] enforces standards of using ginkgo and gomega
40+
- gochecknoglobals # checks that no global variables exist
41+
- cyclop # replaced by revive
42+
- gocyclo # replaced by revive
43+
- forbidigo # needs configuration to be useful
44+
- funlen # replaced by revive
45+
- godox # TODO's are OK
46+
- ireturn # It's OK
47+
- musttag
48+
- nonamedreturns
49+
- goconst # finds repeated strings that could be replaced by a constant
50+
- goheader # checks is file header matches to pattern
51+
- gomodguard # [use more powerful depguard] allow and block lists linter for direct Go module dependencies
52+
- gomoddirectives
53+
- err113 # bad advice about dynamic errors
54+
- lll # [replaced by golines] reports long lines
55+
- mnd # detects magic numbers, duplicated by revive
56+
- nlreturn # [too strict and mostly code is not more readable] checks for a new line before return and branch statements to increase code clarity
57+
- noinlineerr # disallows inline error handling `if err := ...; err != nil {`
58+
- prealloc # [premature optimization, but can be used in some cases] finds slice declarations that could potentially be preallocated
59+
- tagliatelle # needs configuration
60+
- testableexamples # checks if examples are testable (have an expected output)
61+
- testpackage # makes you use a separate _test package
62+
- paralleltest # not every test should be in parallel
63+
- wrapcheck # not required
64+
- wsl # [too strict and mostly code is not more readable] whitespace linter forces you to use empty lines
65+
- wsl_v5 # [too strict and mostly code is not more readable] add or remove empty lines
66+
- zerologlint # detects the wrong usage of zerolog that a user forgets to dispatch zerolog.Event
67+
68+
# All settings can be found here https://github.com/golangci/golangci-lint/blob/HEAD/.golangci.reference.yml
69+
settings:
70+
depguard:
71+
rules:
72+
"deprecated":
73+
files:
74+
- "$all"
75+
deny:
76+
- pkg: github.com/golang/protobuf
77+
desc: Use google.golang.org/protobuf instead, see https://developers.google.com/protocol-buffers/docs/reference/go/faq#modules
78+
- pkg: github.com/satori/go.uuid
79+
desc: Use github.com/google/uuid instead, satori's package is not maintained
80+
- pkg: github.com/gofrs/uuid$
81+
desc: Use github.com/gofrs/uuid/v5 or later, it was not a go module before v5
82+
"non-test files":
83+
files:
84+
- "!$test"
85+
deny:
86+
- pkg: math/rand$
87+
desc: Use math/rand/v2 instead, see https://go.dev/blog/randv2
88+
- pkg: "github.com/sirupsen/logrus"
89+
desc: not allowed
90+
- pkg: "github.com/pkg/errors"
91+
desc: Should be replaced by standard lib errors package
92+
93+
dupl:
94+
# token count (default: 150)
95+
threshold: 300
96+
97+
embeddedstructfieldcheck:
98+
# Checks that sync.Mutex and sync.RWMutex are not used as embedded fields.
99+
forbid-mutex: true
100+
101+
errcheck:
102+
# Report about not checking of errors in type assertions: `a := b.(MyStruct)`.
103+
check-type-assertions: true
104+
check-blank: true
105+
106+
exhaustive:
107+
# Program elements to check for exhaustiveness.
108+
# Default: [ switch ]
109+
check:
110+
- switch
111+
- map
112+
default-signifies-exhaustive: true
113+
114+
fatcontext:
115+
# Check for potential fat contexts in struct pointers.
116+
# May generate false positives.
117+
# Default: false
118+
check-struct-pointers: true
119+
120+
funcorder:
121+
# Checks if the exported methods of a structure are placed before the non-exported ones.
122+
struct-method: false
123+
124+
gocognit:
125+
min-complexity: 55
126+
127+
gocritic:
128+
enable-all: true
129+
disabled-checks:
130+
- paramTypeCombine
131+
# The list of supported checkers can be found at https://go-critic.com/overview.
132+
settings:
133+
captLocal:
134+
# Whether to restrict checker to params only.
135+
paramsOnly: false
136+
underef:
137+
# Whether to skip (*x).method() calls where x is a pointer receiver.
138+
skipRecvDeref: false
139+
hugeParam:
140+
# Default: 80
141+
sizeThreshold: 200
142+
143+
govet:
144+
enable-all: true
145+
146+
godot:
147+
scope: toplevel
148+
149+
inamedparam:
150+
# Skips check for interface methods with only a single parameter.
151+
skip-single-param: true
152+
153+
nakedret:
154+
# Default: 30
155+
max-func-lines: 4
156+
157+
nestif:
158+
min-complexity: 12
159+
160+
nolintlint:
161+
# Exclude following linters from requiring an explanation.
162+
# Default: []
163+
allow-no-explanation: [funlen, gocognit, golines]
164+
# Enable to require an explanation of nonzero length after each nolint directive.
165+
require-explanation: true
166+
# Enable to require nolint directives to mention the specific linter being suppressed.
167+
require-specific: true
168+
169+
revive:
170+
enable-all-rules: true
171+
rules:
172+
- name: add-constant
173+
severity: warning
174+
disabled: false
175+
exclude: [""]
176+
arguments:
177+
- max-lit-count: "5"
178+
allow-strs: '"","\n"'
179+
allow-ints: "0,1,2,3,24,30,60,100,365,0o600,0o700,0o750,0o755"
180+
allow-floats: "0.0,0.,1.0,1.,2.0,2."
181+
- name: cognitive-complexity
182+
arguments: [55]
183+
- name: cyclomatic
184+
arguments: [60]
185+
- name: function-length
186+
arguments: [150, 225]
187+
- name: line-length-limit
188+
arguments: [150]
189+
- name: nested-structs
190+
disabled: true
191+
- name: max-public-structs
192+
arguments: [10]
193+
- name: flag-parameter # fixes are difficult
194+
disabled: true
195+
196+
rowserrcheck:
197+
# database/sql is always checked.
198+
# Default: []
199+
packages:
200+
- github.com/jmoiron/sqlx
201+
202+
perfsprint:
203+
# optimize fmt.Sprintf("x: %s", y) into "x: " + y
204+
strconcat: false
205+
206+
staticcheck:
207+
checks:
208+
- all
209+
210+
usetesting:
211+
# Enable/disable `os.TempDir()` detections.
212+
# Default: false
213+
os-temp-dir: true
214+
215+
varnamelen:
216+
max-distance: 40
217+
min-name-length: 2
218+
219+
exclusions:
220+
# Default: []
221+
presets:
222+
- common-false-positives
223+
rules:
224+
# Allow "err" and "ok" vars to shadow existing declarations, otherwise we get too many false positives.
225+
- text: '^shadow: declaration of "(err|ok)" shadows declaration'
226+
linters:
227+
- govet
228+
- text: "parameter 'ctx' seems to be unused, consider removing or renaming it as _"
229+
linters:
230+
- revive
231+
- path: _test\.go
232+
linters:
233+
- dupl
234+
- gosec
235+
- godot
236+
- govet # alignment
237+
- noctx
238+
- perfsprint
239+
- revive
240+
- varnamelen

.yamllint

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
extends: default
3+
4+
rules:
5+
braces:
6+
max-spaces-inside: 1
7+
brackets:
8+
max-spaces-inside: 1
9+
comments: disable
10+
comments-indentation: disable
11+
document-start: disable
12+
line-length:
13+
level: warning
14+
max: 160
15+
allow-non-breakable-inline-mappings: true
16+
truthy: disable

Makefile

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
# BEGIN: lint-install .
3+
# http://github.com/codeGROOVE-dev/lint-install
4+
5+
.PHONY: lint
6+
lint: _lint
7+
8+
LINT_ARCH := $(shell uname -m)
9+
LINT_OS := $(shell uname)
10+
LINT_OS_LOWER := $(shell echo $(LINT_OS) | tr '[:upper:]' '[:lower:]')
11+
LINT_ROOT := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
12+
13+
# shellcheck and hadolint lack arm64 native binaries: rely on x86-64 emulation
14+
ifeq ($(LINT_OS),Darwin)
15+
ifeq ($(LINT_ARCH),arm64)
16+
LINT_ARCH=x86_64
17+
endif
18+
endif
19+
20+
LINTERS :=
21+
FIXERS :=
22+
23+
GOLANGCI_LINT_CONFIG := $(LINT_ROOT)/.golangci.yml
24+
GOLANGCI_LINT_VERSION ?= v2.4.0
25+
GOLANGCI_LINT_BIN := $(LINT_ROOT)/out/linters/golangci-lint-$(GOLANGCI_LINT_VERSION)-$(LINT_ARCH)
26+
$(GOLANGCI_LINT_BIN):
27+
mkdir -p $(LINT_ROOT)/out/linters
28+
rm -rf $(LINT_ROOT)/out/linters/golangci-lint-*
29+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(LINT_ROOT)/out/linters $(GOLANGCI_LINT_VERSION)
30+
mv $(LINT_ROOT)/out/linters/golangci-lint $@
31+
32+
LINTERS += golangci-lint-lint
33+
golangci-lint-lint: $(GOLANGCI_LINT_BIN)
34+
find . -name go.mod -execdir "$(GOLANGCI_LINT_BIN)" run -c "$(GOLANGCI_LINT_CONFIG)" \;
35+
36+
FIXERS += golangci-lint-fix
37+
golangci-lint-fix: $(GOLANGCI_LINT_BIN)
38+
find . -name go.mod -execdir "$(GOLANGCI_LINT_BIN)" run -c "$(GOLANGCI_LINT_CONFIG)" --fix \;
39+
40+
YAMLLINT_VERSION ?= 1.37.1
41+
YAMLLINT_ROOT := $(LINT_ROOT)/out/linters/yamllint-$(YAMLLINT_VERSION)
42+
YAMLLINT_BIN := $(YAMLLINT_ROOT)/dist/bin/yamllint
43+
$(YAMLLINT_BIN):
44+
mkdir -p $(LINT_ROOT)/out/linters
45+
rm -rf $(LINT_ROOT)/out/linters/yamllint-*
46+
curl -sSfL https://github.com/adrienverge/yamllint/archive/refs/tags/v$(YAMLLINT_VERSION).tar.gz | tar -C $(LINT_ROOT)/out/linters -zxf -
47+
cd $(YAMLLINT_ROOT) && pip3 install --target dist . || pip install --target dist .
48+
49+
LINTERS += yamllint-lint
50+
yamllint-lint: $(YAMLLINT_BIN)
51+
PYTHONPATH=$(YAMLLINT_ROOT)/dist $(YAMLLINT_ROOT)/dist/bin/yamllint .
52+
53+
.PHONY: _lint $(LINTERS)
54+
_lint: $(LINTERS)
55+
56+
.PHONY: fix $(FIXERS)
57+
fix: $(FIXERS)
58+
59+
# END: lint-install .

0 commit comments

Comments
 (0)