Skip to content

Commit b198910

Browse files
committed
chore: add golangci-lint
1 parent fe28dd6 commit b198910

File tree

10 files changed

+304
-18
lines changed

10 files changed

+304
-18
lines changed

.golangci.yaml

Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
1+
# See https://golangci-lint.run/usage/configuration/
2+
# Over time we should try tightening some of these.
3+
4+
linters-settings:
5+
dupl:
6+
# goal: 100
7+
threshold: 412
8+
9+
exhaustruct:
10+
include:
11+
# Gradually extend to cover more of the codebase.
12+
- 'httpmw\.\w+'
13+
# We want to enforce all values are specified when inserting or updating
14+
# a database row. Ref: #9936
15+
- 'github.com/coder/coder/v2/coderd/database\.[^G][^e][^t]\w+Params'
16+
gocognit:
17+
min-complexity: 300
18+
19+
goconst:
20+
min-len: 4 # Min length of string consts (def 3).
21+
min-occurrences: 3 # Min number of const occurrences (def 3).
22+
23+
gocritic:
24+
enabled-checks:
25+
# - appendAssign
26+
# - appendCombine
27+
# - assignOp
28+
# - badCall
29+
- badLock
30+
- badRegexp
31+
- boolExprSimplify
32+
# - builtinShadow
33+
- builtinShadowDecl
34+
# - commentedOutCode
35+
- commentedOutImport
36+
- deferUnlambda
37+
# - deprecatedComment
38+
# - docStub
39+
- dupImport
40+
# - elseif
41+
- emptyFallthrough
42+
# - emptyStringTest
43+
# - equalFold
44+
# - evalOrder
45+
# - exitAfterDefer
46+
# - exposedSyncMutex
47+
# - filepathJoin
48+
- hexLiteral
49+
# - httpNoBody
50+
# - hugeParam
51+
# - ifElseChain
52+
# - importShadow
53+
- indexAlloc
54+
- initClause
55+
- methodExprCall
56+
# - nestingReduce
57+
- nilValReturn
58+
# - octalLiteral
59+
# - paramTypeCombine
60+
# - preferStringWriter
61+
# - preferWriteByte
62+
# - ptrToRefParam
63+
# - rangeExprCopy
64+
# - rangeValCopy
65+
- regexpPattern
66+
# - regexpSimplify
67+
- ruleguard
68+
# - sloppyReassign
69+
- sortSlice
70+
- sprintfQuotedString
71+
- sqlQuery
72+
# - stringConcatSimplify
73+
# - stringXbytes
74+
# - suspiciousSorting
75+
- truncateCmp
76+
- typeAssertChain
77+
# - typeDefFirst
78+
# - typeUnparen
79+
# - unlabelStmt
80+
# - unlambda
81+
# - unnamedResult
82+
# - unnecessaryBlock
83+
# - unnecessaryDefer
84+
# - unslice
85+
- weakCond
86+
# - whyNoLint
87+
# - wrapperFunc
88+
# - yodaStyleExpr
89+
settings:
90+
ruleguard:
91+
failOn: all
92+
rules: "${configDir}/scripts/rules.go"
93+
94+
staticcheck:
95+
# https://staticcheck.io/docs/options#checks
96+
# We disable SA1019 because it gets angry about our usage of xerrors. We
97+
# intentionally xerrors because stack frame support didn't make it into the
98+
# stdlib port.
99+
checks: ["all", "-SA1019"]
100+
101+
goimports:
102+
local-prefixes: coder.com,cdr.dev,go.coder.com,github.com/cdr,github.com/coder
103+
104+
importas:
105+
no-unaliased: true
106+
107+
misspell:
108+
locale: US
109+
ignore-words:
110+
- trialer
111+
112+
nestif:
113+
# goal: 10
114+
min-complexity: 20
115+
116+
revive:
117+
# see https://github.com/mgechev/revive#available-rules for details.
118+
ignore-generated-header: true
119+
severity: warning
120+
rules:
121+
- name: atomic
122+
- name: bare-return
123+
- name: blank-imports
124+
- name: bool-literal-in-expr
125+
- name: call-to-gc
126+
- name: confusing-naming
127+
- name: confusing-results
128+
- name: constant-logical-expr
129+
- name: context-as-argument
130+
- name: context-keys-type
131+
- name: deep-exit
132+
- name: defer
133+
- name: dot-imports
134+
- name: duplicated-imports
135+
- name: early-return
136+
- name: empty-block
137+
- name: empty-lines
138+
- name: error-naming
139+
- name: error-return
140+
- name: error-strings
141+
- name: errorf
142+
- name: exported
143+
- name: flag-parameter
144+
- name: get-return
145+
- name: identical-branches
146+
- name: if-return
147+
- name: import-shadowing
148+
- name: increment-decrement
149+
- name: indent-error-flow
150+
# - name: modifies-parameter
151+
- name: modifies-value-receiver
152+
- name: package-comments
153+
- name: range
154+
- name: receiver-naming
155+
- name: redefines-builtin-id
156+
- name: string-of-int
157+
- name: struct-tag
158+
- name: superfluous-else
159+
- name: time-naming
160+
- name: unconditional-recursion
161+
- name: unexported-naming
162+
- name: unexported-return
163+
- name: unhandled-error
164+
- name: unnecessary-stmt
165+
- name: unreachable-code
166+
- name: unused-parameter
167+
exclude: "**/*_test.go"
168+
- name: unused-receiver
169+
- name: var-declaration
170+
- name: var-naming
171+
- name: waitgroup-by-value
172+
173+
# irrelevant as of Go v1.22: https://go.dev/blog/loopvar-preview
174+
govet:
175+
disable:
176+
- loopclosure
177+
gosec:
178+
excludes:
179+
# Implicit memory aliasing of items from a range statement (irrelevant as of Go v1.22)
180+
- G601
181+
182+
issues:
183+
exclude-dirs:
184+
- coderd/database/dbmem
185+
- node_modules
186+
- .git
187+
188+
exclude-files:
189+
- scripts/rules.go
190+
191+
# Rules listed here: https://github.com/securego/gosec#available-rules
192+
exclude-rules:
193+
- path: _test\.go
194+
linters:
195+
# We use assertions rather than explicitly checking errors in tests
196+
- errcheck
197+
- forcetypeassert
198+
- exhaustruct # This is unhelpful in tests.
199+
- path: scripts/*
200+
linters:
201+
- exhaustruct
202+
- path: scripts/rules.go
203+
linters:
204+
- ALL
205+
206+
fix: true
207+
max-issues-per-linter: 0
208+
max-same-issues: 0
209+
210+
run:
211+
timeout: 10m
212+
213+
# Over time, add more and more linters from
214+
# https://golangci-lint.run/usage/linters/ as the code improves.
215+
linters:
216+
disable-all: true
217+
enable:
218+
- asciicheck
219+
- bidichk
220+
- bodyclose
221+
- dogsled
222+
- errcheck
223+
- errname
224+
- errorlint
225+
- exhaustruct
226+
- forcetypeassert
227+
- gocritic
228+
# gocyclo is may be useful in the future when we start caring
229+
# about testing complexity, but for the time being we should
230+
# create a good culture around cognitive complexity.
231+
# - gocyclo
232+
- gocognit
233+
- nestif
234+
- goimports
235+
- gomodguard
236+
- gosec
237+
- gosimple
238+
- govet
239+
- importas
240+
- ineffassign
241+
- makezero
242+
- misspell
243+
- nilnil
244+
- noctx
245+
- paralleltest
246+
- revive
247+
248+
# These don't work until the following issue is solved.
249+
# https://github.com/golangci/golangci-lint/issues/2649
250+
# - rowserrcheck
251+
# - sqlclosecheck
252+
# - structcheck
253+
# - wastedassign
254+
255+
- staticcheck
256+
- tenv
257+
# In Go, it's possible for a package to test it's internal functionality
258+
# without testing any exported functions. This is enabled to promote
259+
# decomposing a package before testing it's internals. A function caller
260+
# should be able to test most of the functionality from exported functions.
261+
#
262+
# There are edge-cases to this rule, but they should be carefully considered
263+
# to avoid structural inconsistency.
264+
- testpackage
265+
- tparallel
266+
- typecheck
267+
- unconvert
268+
- unused
269+
- dupl

cli/clidisplay/resources.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func WorkspaceTags(writer io.Writer, tags types.TagBlocks) hcl.Diagnostics {
3030
k, v := tag.AsStrings()
3131
tableWriter.AppendRow(table.Row{k, v, ""})
3232
continue
33-
//diags = diags.Extend(tDiags)
33+
// diags = diags.Extend(tDiags)
3434
//if !diags.HasErrors() {
3535
// tableWriter.AppendRow(table.Row{k, v, ""})
3636
// continue
@@ -41,7 +41,7 @@ func WorkspaceTags(writer io.Writer, tags types.TagBlocks) hcl.Diagnostics {
4141
refs := tag.References()
4242
tableWriter.AppendRow(table.Row{k, "??", strings.Join(refs, "\n")})
4343

44-
//refs := tb.AllReferences()
44+
// refs := tb.AllReferences()
4545
//refsStr := make([]string, 0, len(refs))
4646
//for _, ref := range refs {
4747
// refsStr = append(refsStr, ref.String())
@@ -55,7 +55,7 @@ func WorkspaceTags(writer io.Writer, tags types.TagBlocks) hcl.Diagnostics {
5555

5656
func Parameters(writer io.Writer, params []types.Parameter, files map[string]*hcl.File) {
5757
tableWriter := table.NewWriter()
58-
//tableWriter.SetTitle("Parameters")
58+
// tableWriter.SetTitle("Parameters")
5959
tableWriter.SetStyle(table.StyleLight)
6060
tableWriter.Style().Options.SeparateColumns = false
6161
row := table.Row{"Parameter"}
@@ -66,7 +66,7 @@ func Parameters(writer io.Writer, params []types.Parameter, files map[string]*hc
6666
if p.FormType == provider.ParameterFormTypeMultiSelect {
6767
_ = json.Unmarshal([]byte(strVal), &selections)
6868
}
69-
//value := p.Value.Value
69+
// value := p.Value.Value
7070
//
7171
//if value.IsNull() {
7272
// strVal = "null"

cmd/preview/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import (
55
"log"
66
"os"
77

8-
"github.com/coder/preview/cli"
98
"github.com/hashicorp/hcl/v2"
9+
10+
"github.com/coder/preview/cli"
1011
)
1112

1213
func main() {

log.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ func init() {
1111
Level: tlog.LevelDebug,
1212
}))
1313
var _ = ll
14-
//tlog.SetDefault(ll)
14+
// tlog.SetDefault(ll)
1515
}

paramhook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func isForEachKey(key cty.Value) bool {
156156
func evaluateCoderParameterDefault(b *terraform.Block) (cty.Value, bool) {
157157
attributes := b.Attributes()
158158

159-
//typeAttr, exists := attributes["type"]
159+
// typeAttr, exists := attributes["type"]
160160
//valueType := cty.String // TODO: Default to string?
161161
//if exists {
162162
// typeVal := typeAttr.Value()

plan.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func planJSONHook(dfs fs.FS, input Input) (func(ctx *tfcontext.Context, blocks t
2323
var contents io.Reader = bytes.NewReader(input.PlanJSON)
2424
// Also accept `{}` as an empty plan. If this is stored in postgres or another json
2525
// type, then `{}` is the "empty" value.
26-
if len(input.PlanJSON) == 0 || bytes.Compare(input.PlanJSON, []byte("{}")) == 0 {
26+
if len(input.PlanJSON) == 0 || bytes.Equal(input.PlanJSON, []byte("{}")) {
2727
if input.PlanJSONPath == "" {
2828
return func(ctx *tfcontext.Context, blocks terraform.Blocks, inputVars map[string]cty.Value) {}, nil
2929
}

preview.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func Preview(ctx context.Context, input Input, dir fs.FS) (output *Output, diagn
5858

5959
// TODO: Fix logging. There is no way to pass in an instanced logger to
6060
// the parser.
61-
//slog.SetLogLoggerLevel(slog.LevelDebug)
61+
// slog.SetLogLoggerLevel(slog.LevelDebug)
6262
//slog.SetDefault(slog.New(log.NewHandler(os.Stderr, nil)))
6363

6464
varFiles, err := tfVarFiles("", dir)
@@ -127,7 +127,7 @@ func Preview(ctx context.Context, input Input, dir fs.FS) (output *Output, diagn
127127
},
128128
}
129129
}
130-
130+
131131
outputs := hclext.ExportOutputs(modules)
132132

133133
diags := make(hcl.Diagnostics, 0)

previewe2e_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import (
4747
// The goal of the test is to compare `tfstate` with the output of `preview`.
4848
// If `preview`'s implementation of terraform is incorrect, the test will fail.
4949
// TODO: Adding varied parameter inputs would be a good idea.
50-
// TODO: Add workspace tag comparisions.
50+
// TODO: Add workspace tag comparisons.
5151
func Test_VerifyE2E(t *testing.T) {
5252
t.Parallel()
5353

@@ -135,7 +135,7 @@ func Test_VerifyE2E(t *testing.T) {
135135
require.NoError(t, err, "terraform show plan")
136136

137137
pd, err := json.Marshal(plan)
138-
require.NoError(t, err, "marshalling plan")
138+
require.NoError(t, err, "marshaling plan")
139139

140140
err = os.WriteFile(filepath.Join(wp, "plan.json"), pd, 0644)
141141
require.NoError(t, err, "writing plan.json")

scripts/rules.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Package gorules defines custom lint rules for ruleguard.
2+
//
3+
// golangci-lint runs these rules via go-critic, which includes support
4+
// for ruleguard. All Go files in this directory define lint rules
5+
// in the Ruleguard DSL; see:
6+
//
7+
// - https://go-ruleguard.github.io/by-example/
8+
// - https://pkg.go.dev/github.com/quasilyte/go-ruleguard/dsl
9+
//
10+
// You run one of the following commands to execute your go rules only:
11+
//
12+
// golangci-lint run
13+
// golangci-lint run --disable-all --enable=gocritic
14+
//
15+
// Note: don't forget to run `golangci-lint cache clean`!
16+
package gorules

0 commit comments

Comments
 (0)