Skip to content

Commit 97483c8

Browse files
committed
lint cleanup of code base
1 parent a103b70 commit 97483c8

File tree

10 files changed

+819
-441
lines changed

10 files changed

+819
-441
lines changed

.golangci.yml

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

7+
# Version information for builds
8+
GIT_VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
9+
GIT_COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
10+
BUILD_DATE := $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
11+
LDFLAGS := -X main.version=$(GIT_VERSION) -X main.commit=$(GIT_COMMIT) -X main.date=$(BUILD_DATE)
12+
713
.PHONY: build clean deps run app-bundle install install-darwin install-unix install-windows
814

915
# Install dependencies
@@ -24,28 +30,28 @@ endif
2430
# Build for current platform
2531
build:
2632
ifeq ($(OS),Windows_NT)
27-
CGO_ENABLED=1 go build -ldflags -H=windowsgui -o $(APP_NAME).exe .
33+
CGO_ENABLED=1 go build -ldflags "-H=windowsgui $(LDFLAGS)" -o $(APP_NAME).exe .
2834
else
29-
CGO_ENABLED=1 go build -o $(APP_NAME) .
35+
CGO_ENABLED=1 go build -ldflags "$(LDFLAGS)" -o $(APP_NAME) .
3036
endif
3137

3238
# Build for all platforms
3339
build-all: build-darwin build-linux build-windows
3440

3541
# Build for macOS
3642
build-darwin:
37-
CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 go build -o out/$(APP_NAME)-darwin-amd64 .
38-
CGO_ENABLED=1 GOOS=darwin GOARCH=arm64 go build -o out/$(APP_NAME)-darwin-arm64 .
43+
CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o out/$(APP_NAME)-darwin-amd64 .
44+
CGO_ENABLED=1 GOOS=darwin GOARCH=arm64 go build -ldflags "$(LDFLAGS)" -o out/$(APP_NAME)-darwin-arm64 .
3945

4046
# Build for Linux
4147
build-linux:
42-
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -o out/$(APP_NAME)-linux-amd64 .
43-
CGO_ENABLED=1 GOOS=linux GOARCH=arm64 go build -o out/$(APP_NAME)-linux-arm64 .
48+
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o out/$(APP_NAME)-linux-amd64 .
49+
CGO_ENABLED=1 GOOS=linux GOARCH=arm64 go build -ldflags "$(LDFLAGS)" -o out/$(APP_NAME)-linux-arm64 .
4450

4551
# Build for Windows
4652
build-windows:
47-
CGO_ENABLED=1 GOOS=windows GOARCH=amd64 go build -ldflags -H=windowsgui -o out/$(APP_NAME)-windows-amd64.exe .
48-
CGO_ENABLED=1 GOOS=windows GOARCH=arm64 go build -ldflags -H=windowsgui -o out/$(APP_NAME)-windows-arm64.exe .
53+
CGO_ENABLED=1 GOOS=windows GOARCH=amd64 go build -ldflags "-H=windowsgui $(LDFLAGS)" -o out/$(APP_NAME)-windows-amd64.exe .
54+
CGO_ENABLED=1 GOOS=windows GOARCH=arm64 go build -ldflags "-H=windowsgui $(LDFLAGS)" -o out/$(APP_NAME)-windows-arm64.exe .
4955

5056
# Clean build artifacts
5157
clean:
@@ -166,4 +172,62 @@ install-windows: build
166172
@echo "Copying executable..."
167173
@copy /Y "$(APP_NAME).exe" "%LOCALAPPDATA%\Programs\$(APP_NAME)\"
168174
@echo "Installation complete! $(APP_NAME) has been installed to %LOCALAPPDATA%\Programs\$(APP_NAME)"
169-
@echo "You may want to add %LOCALAPPDATA%\Programs\$(APP_NAME) to your PATH environment variable."
175+
@echo "You may want to add %LOCALAPPDATA%\Programs\$(APP_NAME) to your PATH environment variable."
176+
# BEGIN: lint-install .
177+
# http://github.com/codeGROOVE-dev/lint-install
178+
179+
.PHONY: lint
180+
lint: _lint
181+
182+
LINT_ARCH := $(shell uname -m)
183+
LINT_OS := $(shell uname)
184+
LINT_OS_LOWER := $(shell echo $(LINT_OS) | tr '[:upper:]' '[:lower:]')
185+
LINT_ROOT := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
186+
187+
# shellcheck and hadolint lack arm64 native binaries: rely on x86-64 emulation
188+
ifeq ($(LINT_OS),Darwin)
189+
ifeq ($(LINT_ARCH),arm64)
190+
LINT_ARCH=x86_64
191+
endif
192+
endif
193+
194+
LINTERS :=
195+
FIXERS :=
196+
197+
GOLANGCI_LINT_CONFIG := $(LINT_ROOT)/.golangci.yml
198+
GOLANGCI_LINT_VERSION ?= v2.3.1
199+
GOLANGCI_LINT_BIN := $(LINT_ROOT)/out/linters/golangci-lint-$(GOLANGCI_LINT_VERSION)-$(LINT_ARCH)
200+
$(GOLANGCI_LINT_BIN):
201+
mkdir -p $(LINT_ROOT)/out/linters
202+
rm -rf $(LINT_ROOT)/out/linters/golangci-lint-*
203+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(LINT_ROOT)/out/linters $(GOLANGCI_LINT_VERSION)
204+
mv $(LINT_ROOT)/out/linters/golangci-lint $@
205+
206+
LINTERS += golangci-lint-lint
207+
golangci-lint-lint: $(GOLANGCI_LINT_BIN)
208+
find . -name go.mod -execdir "$(GOLANGCI_LINT_BIN)" run -c "$(GOLANGCI_LINT_CONFIG)" \;
209+
210+
FIXERS += golangci-lint-fix
211+
golangci-lint-fix: $(GOLANGCI_LINT_BIN)
212+
find . -name go.mod -execdir "$(GOLANGCI_LINT_BIN)" run -c "$(GOLANGCI_LINT_CONFIG)" --fix \;
213+
214+
YAMLLINT_VERSION ?= 1.37.1
215+
YAMLLINT_ROOT := $(LINT_ROOT)/out/linters/yamllint-$(YAMLLINT_VERSION)
216+
YAMLLINT_BIN := $(YAMLLINT_ROOT)/dist/bin/yamllint
217+
$(YAMLLINT_BIN):
218+
mkdir -p $(LINT_ROOT)/out/linters
219+
rm -rf $(LINT_ROOT)/out/linters/yamllint-*
220+
curl -sSfL https://github.com/adrienverge/yamllint/archive/refs/tags/v$(YAMLLINT_VERSION).tar.gz | tar -C $(LINT_ROOT)/out/linters -zxf -
221+
cd $(YAMLLINT_ROOT) && pip3 install --target dist . || pip install --target dist .
222+
223+
LINTERS += yamllint-lint
224+
yamllint-lint: $(YAMLLINT_BIN)
225+
PYTHONPATH=$(YAMLLINT_ROOT)/dist $(YAMLLINT_ROOT)/dist/bin/yamllint .
226+
227+
.PHONY: _lint $(LINTERS)
228+
_lint: $(LINTERS)
229+
230+
.PHONY: fix $(FIXERS)
231+
fix: $(FIXERS)
232+
233+
# END: lint-install .

0 commit comments

Comments
 (0)