Skip to content

Commit 5e7d1dd

Browse files
authored
[chore][go lint] Add make targets for linting go code (#1816)
* [chore][go make targets] Add go linting make targets * Parallelize lint * Add tools changes
1 parent fd08bd1 commit 5e7d1dd

File tree

9 files changed

+1575
-10
lines changed

9 files changed

+1575
-10
lines changed

.golangci.yml

Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
formatters:
2+
enable:
3+
- gci
4+
- gofumpt
5+
6+
settings:
7+
gci:
8+
sections:
9+
- standard
10+
- default
11+
- prefix(github.com/signalfx/splunk-otel-collector-chart)
12+
13+
issues:
14+
# Maximum issues count per one linter.
15+
max-issues-per-linter: 0
16+
17+
# Maximum count of issues with the same text.
18+
max-same-issues: 0
19+
20+
linters:
21+
enable:
22+
- copyloopvar
23+
- decorder
24+
- depguard
25+
- errcheck
26+
- errorlint
27+
- exhaustive
28+
- gocritic
29+
- gosec
30+
- govet
31+
- misspell
32+
- nolintlint
33+
- predeclared
34+
- reassign
35+
- revive
36+
- staticcheck
37+
- testifylint
38+
- thelper
39+
- unconvert
40+
- unparam
41+
- unused
42+
- usestdlibvars
43+
- usetesting
44+
- wastedassign
45+
- whitespace
46+
47+
exclusions:
48+
49+
presets:
50+
- comments
51+
- common-false-positives
52+
- legacy
53+
- std-error-handling
54+
55+
# Excluding configuration per-path, per-linter, per-text and per-source
56+
rules:
57+
- linters:
58+
- unused
59+
path: pagefile.go # This exclusion is required for Windows only
60+
text: cachedBytes
61+
62+
# Log a warning if an exclusion rule is unused.
63+
warn-unused: true
64+
65+
# all available settings of specific linters
66+
settings:
67+
depguard:
68+
rules:
69+
denied-deps:
70+
deny:
71+
- pkg: github.com/azure/go-autorest
72+
desc: "This package is deprecated (EOL). Please use an alternative Azure SDK."
73+
74+
- pkg: go.uber.org/atomic
75+
desc: Use 'sync/atomic' instead of go.uber.org/atomic
76+
77+
- pkg: github.com/pkg/errors
78+
desc: Use 'errors' or 'fmt' instead of github.com/pkg/errors
79+
80+
- pkg: github.com/hashicorp/go-multierror
81+
desc: Use go.uber.org/multierr instead of github.com/hashicorp/go-multierror
82+
83+
- pkg: math/rand$
84+
desc: Use math/rand/v2 instead of math/rand
85+
86+
# Add a different guard rule so that we can ignore tests.
87+
ignore-in-test:
88+
deny:
89+
- pkg: go.opentelemetry.io/proto
90+
desc: Use go.opentelemetry.io/collector/pdata instead
91+
92+
# Allow in tests for testing pdata or other receivers/exporters that expect OTLP.
93+
files:
94+
- '!**/*_test.go'
95+
96+
exhaustive:
97+
explicit-exhaustive-switch: true
98+
99+
ignore-enum-members: pmetric.MetricTypeEmpty
100+
101+
gosec:
102+
excludes:
103+
- G115
104+
- G402
105+
- G404
106+
107+
govet:
108+
# TODO: Enable this and fix the alignment issues.
109+
disable:
110+
- fieldalignment
111+
112+
enable-all: true
113+
114+
settings:
115+
printf: # analyzer name, run `go tool vet help` to see all analyzers
116+
funcs: # run `go tool vet help printf` to see available settings for `printf` analyzer
117+
- Infof
118+
- Warnf
119+
- Errorf
120+
- Fatalf
121+
122+
misspell:
123+
ignore-rules:
124+
- cancelled
125+
126+
# Correct spellings using locale preferences for US or UK.
127+
# Default is to use a neutral variety of English.
128+
# Setting locale to US will correct the British spelling of 'colour' to 'color'.
129+
locale: US
130+
131+
nolintlint:
132+
require-specific: true
133+
134+
predeclared:
135+
ignore:
136+
- copy
137+
138+
revive:
139+
# minimal confidence for issues, default is 0.8
140+
confidence: 0.8
141+
142+
rules:
143+
# Blank import should be only in a main or test package, or have a comment justifying it.
144+
- name: blank-imports
145+
# context.Context() should be the first parameter of a function when provided as argument.
146+
- name: context-as-argument
147+
# Basic types should not be used as a key in `context.WithValue`
148+
- name: context-keys-type
149+
# Importing with `.` makes the programs much harder to understand
150+
- name: dot-imports
151+
- name: early-return
152+
arguments:
153+
- preserveScope
154+
# Empty blocks make code less readable and could be a symptom of a bug or unfinished refactoring.
155+
- name: empty-block
156+
# for better readability, variables of type `error` must be named with the prefix `err`.
157+
- name: error-naming
158+
# for better readability, the errors should be last in the list of returned values by a function.
159+
- name: error-return
160+
# for better readability, error messages should not be capitalized or end with punctuation or a newline.
161+
- name: error-strings
162+
# report when replacing `errors.New(fmt.Sprintf())` with `fmt.Errorf()` is possible
163+
- name: errorf
164+
# incrementing an integer variable by 1 is recommended to be done using the `++` operator
165+
- name: increment-decrement
166+
# highlights redundant else-blocks that can be eliminated from the code
167+
- name: indent-error-flow
168+
# This rule suggests a shorter way of writing ranges that do not use the second value.
169+
- name: range
170+
# receiver names in a method should reflect the struct name (p for Person, for example)
171+
- name: receiver-naming
172+
# redefining built in names (true, false, append, make) can lead to bugs very difficult to detect.
173+
- name: redefines-builtin-id
174+
# redundant else-blocks that can be eliminated from the code.
175+
- name: superfluous-else
176+
arguments:
177+
- preserveScope
178+
# prevent confusing name for variables when using `time` package
179+
- name: time-naming
180+
# warns when an exported function or method returns a value of an un-exported type.
181+
- name: unexported-return
182+
# spots and proposes to remove unreachable code. also helps to spot errors
183+
- name: unreachable-code
184+
# Functions or methods with unused parameters can be a symptom of an unfinished refactoring or a bug.
185+
- name: unused-parameter
186+
# Since Go 1.18, interface{} has an alias: any. This rule proposes to replace instances of interface{} with any.
187+
- name: use-any
188+
# identifies calls to fmt.Errorf that can be safely replaced by, the more efficient, errors.New.
189+
- name: use-errors-new
190+
# report when a variable declaration can be simplified
191+
- name: var-declaration
192+
# warns when initialism, variable or package naming conventions are not followed.
193+
- name: var-naming
194+
195+
staticcheck:
196+
checks:
197+
- all
198+
- -ST1003 # FIXME: Poorly chosen identifier
199+
- -ST1005 # FIXME: Incorrectly formatted error string
200+
- -ST1011 # FIXME: Poorly chosen name for variable of type time.Duration
201+
202+
testifylint:
203+
disable:
204+
- encoded-compare # has false positives that cannot be fixed with testifylint-fix
205+
- float-compare
206+
- require-error
207+
- suite-subtest-run
208+
209+
enable-all: true
210+
211+
thelper:
212+
benchmark:
213+
begin: false
214+
215+
fuzz:
216+
begin: false
217+
218+
tb:
219+
begin: false
220+
221+
test:
222+
begin: false
223+
224+
output:
225+
# The formats used to render issues.
226+
formats:
227+
# Prints issues in a text format with colors, line number, and linter name.
228+
text:
229+
# Output path can be either `stdout`, `stderr` or path to the file to write to.
230+
path: stdout
231+
# Print linter name in the end of issue text.
232+
print-linter-name: true
233+
# Print lines of code with issue.
234+
print-issued-lines: true
235+
236+
# Show statistics per linter.
237+
show-stats: false
238+
239+
# options for analysis running
240+
run:
241+
# Allow multiple parallel golangci-lint instances running.
242+
# If false, golangci-lint acquires file lock on start.
243+
allow-parallel-runners: true
244+
245+
# List of build tags, all linters use it.
246+
build-tags:
247+
- integration
248+
249+
# default concurrency is a available CPU number
250+
concurrency: 4
251+
252+
# by default isn't set. If set we pass it to "go list -mod={option}". From "go help modules":
253+
# If invoked with -mod=readonly, the go command is disallowed from the implicit
254+
# automatic updating of go.mod described above. Instead, it fails when any changes
255+
# to go.mod are needed. This setting is most useful to check that go.mod does
256+
# not need updates, such as in a continuous integration and testing system.
257+
# If invoked with -mod=vendor, the go command assumes that the vendor
258+
# directory holds the correct copies of dependencies and ignores
259+
# the dependency descriptions in go.mod.
260+
modules-download-mode: readonly
261+
262+
# exit code when at least one issue was found, default is 1
263+
issues-exit-code: 1
264+
265+
# include test files or not, default is true
266+
tests: true
267+
268+
# timeout for analysis, e.g. 30s, 5m, default is 0 (no timeout)
269+
timeout: 30m
270+
271+
version: "2"

Makefile

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
include ./Makefile.common
2+
23
##@ General
34
# The general settings and variables for the project
45

@@ -21,6 +22,10 @@ CHLOGGEN ?= $(LOCALBIN)/chloggen
2122

2223
CERTMANAGER_VERSION ?= $(shell yq eval ".dependencies[] | select(.name == \"cert-manager\") | .version" helm-charts/splunk-otel-collector/Chart.yaml)
2324

25+
FIND_MOD_ARGS=-type f -name "go.mod"
26+
TO_MOD_DIR=dirname {} \; | sort | egrep '^./'
27+
ALL_MODS := $(shell find . $(FIND_MOD_ARGS) -exec $(TO_MOD_DIR))
28+
2429
# The help target as provided
2530
.PHONY: help
2631
help: ## Display Makefile help information for all actions
@@ -212,18 +217,37 @@ update-chart-dep: dep-update ## Updates the dependency version in the Chart.yaml
212217
prepare-release: ## Prepares for a new release of the helm chart. Optionally specify CHART_VERSION and APP_VERSION.
213218
ci_scripts/prepare-release.sh CREATE_BRANCH=${CREATE_BRANCH} CHART_VERSION=${CHART_VERSION} APP_VERSION=${APP_VERSION}
214219

220+
.PHONY: update-operator-crds
221+
update-operator-crds: ## Update CRDs in the opentelemetry-operator-crds subchart
222+
ci_scripts/update-crds.sh
223+
224+
.PHONY: for-all
225+
for-all:
226+
@set -e; for dir in $(ALL_MODS); do \
227+
(cd "$${dir}" && \
228+
echo "running $${CMD} in $${dir}" && \
229+
$${CMD} ); \
230+
done
231+
215232
.PHONY: tidy-all
216233
tidy-all:
217234
@for dir in $$(find . -type f -name "go.mod" -exec dirname {} \; | sort | egrep '^./'); do \
218235
echo "Running go mod tidy in $$dir"; \
219236
(cd "$$dir" && rm -f go.sum && go mod tidy); \
220237
done
221238

222-
.PHONY: update-operator-crds
223-
update-operator-crds: ## Update CRDs in the opentelemetry-operator-crds subchart
224-
ci_scripts/update-crds.sh
239+
.PHONY: gofmt-all
240+
gofmt-all:
241+
@$(MAKE) for-all gofmt
242+
243+
.PHONY: govulncheck-all
244+
govulncheck-all:
245+
@$(MAKE) for-all govulncheck
246+
247+
.PHONY: golint-all
248+
golint-all:
249+
@$(MAKE) for-all golint
225250

226-
.PHONY: misspell
227-
misspell: $(TOOLS_BIN_DIR)/misspell
228-
@echo "running $(MISSPELL)"
229-
@$(MISSPELL) $$($(ALL_SRC_AND_DOC_CMD))
251+
.PHONY: gogci-all
252+
gogci-all:
253+
@$(MAKE) for-all gogci

0 commit comments

Comments
 (0)