Skip to content

Commit cb28916

Browse files
authored
Merge pull request #10 from rdner/add-linter
Create linter configuration and address found issues. Also added magefile targets so it's running on the CI.
2 parents 556ff76 + bc2c107 commit cb28916

24 files changed

+353
-100
lines changed

.ci/Jenkinsfile

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,7 @@ pipeline {
3737
withGithubNotify(context: "Lint") {
3838
dir("${BASE_DIR}"){
3939
withMageEnv(){
40-
sh(label: 'lint', script: '''
41-
go mod tidy && git diff --exit-code
42-
gofmt -l . | read && echo "Code differs from gofmt's style. Run 'gofmt -w .'" 1>&2 && exit 1 || true
43-
''')
4440
sh(label: 'Mage check', script: 'mage check')
45-
sh(label: 'Go vet', script: 'go vet ./...')
4641
}
4742
}
4843
}

.golangci.yml

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# options for analysis running
2+
run:
3+
# timeout for analysis, e.g. 30s, 5m, default is 1m
4+
timeout: 1m
5+
6+
output:
7+
sort-results: true
8+
9+
# Uncomment and add a path if needed to exclude
10+
# skip-dirs:
11+
# - some/path
12+
# skip-files:
13+
# - ".*\\.my\\.go$"
14+
# - lib/bad.go
15+
16+
# Find the whole list here https://golangci-lint.run/usage/linters/
17+
linters:
18+
disable-all: true
19+
enable:
20+
- deadcode # finds unused code
21+
- errcheck # checking for unchecked errors in go programs
22+
- errorlint # errorlint is a linter for that can be used to find code that will cause problems with the error wrapping scheme introduced in Go 1.13.
23+
- goconst # finds repeated strings that could be replaced by a constant
24+
- dupl # tool for code clone detection
25+
- forbidigo # forbids identifiers matched by reg exps
26+
- gomoddirectives # manage the use of 'replace', 'retract', and 'excludes' directives in go.mod.
27+
- gosimple # linter for Go source code that specializes in simplifying a code
28+
- misspell # finds commonly misspelled English words in comments
29+
- nakedret # finds naked returns in functions greater than a specified function length
30+
- prealloc # finds slice declarations that could potentially be preallocated
31+
- nolintlint # reports ill-formed or insufficient nolint directives
32+
- staticcheck # Staticcheck is a go vet on steroids, applying a ton of static analysis checks
33+
- stylecheck # a replacement for golint
34+
- unparam # reports unused function parameters
35+
- unused # checks Go code for unused constants, variables, functions and types
36+
37+
- govet # Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string
38+
- ineffassign # detects when assignments to existing variables are not used
39+
- structcheck # finds unused struct fields
40+
- typecheck # Like the front-end of a Go compiler, parses and type-checks Go code
41+
- varcheck # Finds unused global variables and constants
42+
- asciicheck # simple linter to check that your code does not contain non-ASCII identifiers
43+
- bodyclose # checks whether HTTP response body is closed successfully
44+
- durationcheck # check for two durations multiplied together
45+
- exportloopref # checks for pointers to enclosing loop variables
46+
- goimports # Goimports does everything that gofmt does. Additionally it checks unused imports
47+
- gosec # inspects source code for security problems
48+
- importas # enforces consistent import aliases
49+
- nilerr # finds the code that returns nil even if it checks that the error is not nil.
50+
- noctx # noctx finds sending http request without context.Context
51+
- unconvert # Remove unnecessary type conversions
52+
- wastedassign # wastedassign finds wasted assignment statements.
53+
- godox # tool for detection of FIXME, TODO and other comment keywords
54+
55+
# all available settings of specific linters
56+
linters-settings:
57+
errcheck:
58+
# report about not checking of errors in type assertions: `a := b.(MyStruct)`;
59+
# default is false: such cases aren't reported by default.
60+
check-type-assertions: true
61+
62+
errorlint:
63+
# Check whether fmt.Errorf uses the %w verb for formatting errors. See the readme for caveats
64+
errorf: true
65+
# Check for plain type assertions and type switches
66+
asserts: true
67+
# Check for plain error comparisons
68+
comparison: true
69+
70+
goconst:
71+
# minimal length of string constant, 3 by default
72+
min-len: 3
73+
# minimal occurrences count to trigger, 3 by default
74+
min-occurrences: 2
75+
76+
dupl:
77+
# tokens count to trigger issue, 150 by default
78+
threshold: 100
79+
80+
forbidigo:
81+
# Forbid the following identifiers
82+
forbid:
83+
- fmt.Print.* # too much log noise
84+
# Exclude godoc examples from forbidigo checks. Default is true.
85+
exclude_godoc_examples: true
86+
87+
gomoddirectives:
88+
# Allow local `replace` directives. Default is false.
89+
replace-local: false
90+
91+
gosimple:
92+
# Select the Go version to target. The default is '1.13'.
93+
go: "1.17"
94+
95+
misspell:
96+
# Correct spellings using locale preferences for US or UK.
97+
# Default is to use a neutral variety of English.
98+
# Setting locale to US will correct the British spelling of 'colour' to 'color'.
99+
# locale: US
100+
# ignore-words:
101+
# - IdP
102+
103+
nakedret:
104+
# make an issue if func has more lines of code than this setting and it has naked returns; default is 30
105+
max-func-lines: 0
106+
107+
prealloc:
108+
# Report preallocation suggestions only on simple loops that have no returns/breaks/continues/gotos in them.
109+
# True by default.
110+
simple: true
111+
range-loops: true # Report preallocation suggestions on range loops, true by default
112+
for-loops: false # Report preallocation suggestions on for loops, false by default
113+
114+
nolintlint:
115+
# Enable to ensure that nolint directives are all used. Default is true.
116+
allow-unused: false
117+
# Disable to ensure that nolint directives don't have a leading space. Default is true.
118+
allow-leading-space: true
119+
# Exclude following linters from requiring an explanation. Default is [].
120+
allow-no-explanation: []
121+
# Enable to require an explanation of nonzero length after each nolint directive. Default is false.
122+
require-explanation: true
123+
# Enable to require nolint directives to mention the specific linter being suppressed. Default is false.
124+
require-specific: true
125+
126+
staticcheck:
127+
# Select the Go version to target. The default is '1.13'.
128+
go: "1.17"
129+
130+
stylecheck:
131+
# Select the Go version to target. The default is '1.13'.
132+
go: "1.17"
133+
134+
unparam:
135+
# Inspect exported functions, default is false. Set to true if no external program/library imports your code.
136+
# XXX: if you enable this setting, unparam will report a lot of false-positives in text editors:
137+
# if it's called for subdir of a project it can't find external interfaces. All text editor integrations
138+
# with golangci-lint call it on a directory with the changed file.
139+
check-exported: false
140+
141+
unused:
142+
# Select the Go version to target. The default is '1.13'.
143+
go: "1.17"

config/config.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ type Namespace struct {
3838
config *C
3939
}
4040

41+
const mask = "xxxxx"
42+
4143
var (
4244
configOpts = []ucfg.Option{
4345
ucfg.PathSep("."),
@@ -60,11 +62,6 @@ var (
6062
)
6163
)
6264

63-
const (
64-
selectorConfig = "config"
65-
selectorConfigWithPassword = "config-with-passwords"
66-
)
67-
6865
func NewConfig() *C {
6966
return fromConfig(ucfg.New())
7067
}
@@ -369,10 +366,10 @@ func applyLoggingMask(c interface{}) {
369366
if maskList.Has(strings.ToLower(k)) {
370367
if arr, ok := v.([]interface{}); ok {
371368
for i := range arr {
372-
arr[i] = "xxxxx"
369+
arr[i] = mask
373370
}
374371
} else {
375-
cfg[k] = "xxxxx"
372+
cfg[k] = mask
376373
}
377374
} else {
378375
applyLoggingMask(v)

config/flags.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
cfgflag "github.com/elastic/go-ucfg/flag"
2626
)
2727

28+
const typeString = "string"
29+
2830
// StringsFlag collects multiple usages of the same flag into an array of strings.
2931
// Duplicate values will be ignored.
3032
type StringsFlag struct {
@@ -157,7 +159,7 @@ func (f *StringsFlag) List() []string {
157159
// Type reports the type of contents (string) expected to be parsed by Set.
158160
// It is used to build the CLI usage string.
159161
func (f *StringsFlag) Type() string {
160-
return "string"
162+
return typeString
161163
}
162164

163165
// SettingFlag defines a setting flag, name and it's usage. The return value is
@@ -214,7 +216,7 @@ func (f *SettingsFlag) Get() interface{} {
214216
return f.Config()
215217
}
216218

217-
// String always returns an empty string. It is required to fulfil
219+
// String always returns an empty string. It is required to fulfill
218220
// the flag.Value interface.
219221
func (f *SettingsFlag) String() string {
220222
return ""
@@ -286,5 +288,5 @@ func (f *flagOverwrite) Get() interface{} {
286288
}
287289

288290
func (f *flagOverwrite) Type() string {
289-
return "string"
291+
return typeString
290292
}

config/flags_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828

2929
"github.com/spf13/cobra"
3030
"github.com/stretchr/testify/assert"
31+
"github.com/stretchr/testify/require"
3132
)
3233

3334
func TestStringArrFlag(t *testing.T) {
@@ -158,7 +159,8 @@ func TestOverwriteFlag(t *testing.T) {
158159
cobraExpectedUsage := " -a, --a string message\n"
159160
assert.Equal(t, cobraExpectedUsage, cobraUsage)
160161

161-
fs.Set("a", "overwrite")
162+
err = fs.Set("a", "overwrite")
163+
require.NoError(t, err)
162164
final, err := config.String("a", -1)
163165
assert.NoError(t, err)
164166
assert.Equal(t, "overwrite", final)

file/helper_other.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ func SafeFileRotate(path, tempfile string) error {
3737
// filesystems, so to update the parents directory metadata to actually
3838
// contain the new file being rotated in.
3939
f, err := os.Open(parent)
40+
41+
// nolint: nilerr // ignore error
4042
if err != nil {
41-
return nil // ignore error
43+
return nil
4244
}
4345
defer f.Close()
4446

file/helper_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/stretchr/testify/assert"
3030
)
3131

32+
// nolint: gosec // file permissions are valid for test purposes
3233
func TestSafeFileRotateExistingFile(t *testing.T) {
3334
tempdir, err := ioutil.TempDir("", "")
3435
assert.NoError(t, err)

0 commit comments

Comments
 (0)