Skip to content

Commit 1a88748

Browse files
authored
Update golangci-lint configuration (#105)
* Set golangci-lint to be more strict * Fix various golangci-lint issues
1 parent f4ebaad commit 1a88748

File tree

11 files changed

+94
-69
lines changed

11 files changed

+94
-69
lines changed

.golangci.yml

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,44 @@
11
run:
22
timeout: 5m
33
tests: false
4+
skip-files:
5+
- 'testhelper/for_main.go'
6+
issues:
7+
exclude-rules:
8+
- path: 'result/overall.go'
9+
linters:
10+
- nestif
11+
- path: 'convert/bytes_common.go'
12+
linters:
13+
- ireturn
414
linters:
5-
disable-all: false
6-
enable:
7-
- funlen
8-
- dogsled
9-
- dupl
10-
- lll
11-
- whitespace
12-
- wsl
13-
- exportloopref
14-
disable:
15-
- scopelint
16-
- deadcode
17-
- structcheck
18-
- varcheck
19-
- musttag
20-
presets:
21-
- bugs
22-
- unused
23-
fast: false
15+
enable-all: true
16+
disable:
17+
- cyclop
18+
- depguard
19+
- exhaustivestruct
20+
- exhaustruct
21+
- forbidigo
22+
- forcetypeassert
23+
- gci
24+
- gochecknoglobals
25+
- gochecknoinits
26+
- godox
27+
- godot
28+
- goerr113
29+
- gofumpt
30+
- gomnd
31+
- lll
32+
- musttag
33+
- nakedret
34+
- nlreturn
35+
- nolintlint
36+
- nonamedreturns
37+
- tagliatelle
38+
- varnamelen
39+
- wrapcheck
40+
linters-settings:
41+
estif:
42+
min-complexity: 4
43+
maligned:
44+
suggest-new: true

config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func LoadFromEnv(config interface{}) {
126126

127127
// Potential for addding different types, for now we only use strings
128128
// since the main use case is credentials
129-
// nolint: exhaustive
129+
// nolint: exhaustive, gocritic
130130
switch field.Type.Kind() {
131131
case reflect.String:
132132
configValue.Field(i).SetString(envValue)

convert/bytes_common.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ func ParseBytes(value string) (ByteAny, error) {
7171
// If the input value is 0, humanReadable will always return "0B"
7272
//
7373
// Examples:
74-
// 1073741824B -> 1000KB
75-
// 2147483648B -> 2MB
76-
// 0 -> 0MB
7774
//
75+
// 1073741824B -> 1000KB
76+
// 2147483648B -> 2MB
77+
// 0 -> 0MB
7878
func humanReadable(b uint64, units []string, base float64) (float64, string) {
7979
if b == 0 {
8080
return 0, "B"

exit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ func ExitRaw(rc int, output ...string) {
5252
func BaseExit(rc int) {
5353
if AllowExit {
5454
os.Exit(rc)
55-
} else {
56-
_, _ = os.Stdout.WriteString("would exit with code " + strconv.Itoa(rc) + "\n")
5755
}
56+
57+
_, _ = os.Stdout.WriteString("would exit with code " + strconv.Itoa(rc) + "\n")
5858
}
5959

6060
// ExitError exists with an Unknown state while reporting the error

perfdata/list.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
)
66

77
// PerfdataList can store multiple perfdata and brings a simple fmt.Stringer interface
8+
// nolint: golint, revive
89
type PerfdataList []*Perfdata
910

1011
// String returns string representations of all Perfdata

perfdata/type.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package perfdata
22

33
import (
44
"fmt"
5-
"github.com/NETWAYS/go-check"
65
"strings"
6+
7+
"github.com/NETWAYS/go-check"
78
)
89

910
// Replace not allowed characters inside a label

result/overall.go

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,35 @@ import (
2020
// one suffices, but one fails, the whole check might be OK and only the subcheck
2121
// Warning or Critical.
2222
type Overall struct {
23-
oks int
24-
warnings int
25-
criticals int
26-
unknowns int
27-
Summary string
28-
stateSetExplicitely bool
29-
Outputs []string // Deprecate this in a future version
30-
PartialResults []PartialResult
23+
oks int
24+
warnings int
25+
criticals int
26+
unknowns int
27+
Summary string
28+
stateSetExplicitly bool
29+
Outputs []string // Deprecate this in a future version
30+
PartialResults []PartialResult
3131
}
3232

3333
// PartialResult represents a sub-result for an Overall struct
3434
type PartialResult struct {
35-
state int // Result state, either set explicitely or derived from partialResults
36-
Output string
37-
stateSetExplicitely bool // nolint: unused
38-
defaultState int // Default result state, if no partial results are available and no state is set explicitely
39-
defaultStateSet bool // nolint: unused
40-
Perfdata perfdata.PerfdataList
41-
PartialResults []PartialResult
35+
Perfdata perfdata.PerfdataList
36+
PartialResults []PartialResult
37+
Output string
38+
state int // Result state, either set explicitly or derived from partialResults
39+
defaultState int // Default result state, if no partial results are available and no state is set explicitly
40+
stateSetExplicitly bool // nolint: unused
41+
defaultStateSet bool // nolint: unused
4242
}
4343

4444
// String returns the status and output of the PartialResult
4545
func (s *PartialResult) String() string {
4646
return fmt.Sprintf("[%s] %s", check.StatusText(s.GetStatus()), s.Output)
4747
}
4848

49-
// Add adds a return state explicitely
49+
// Add adds a return state explicitly
5050
//
51-
// Hint: This will set stateSetExplicitely to true
51+
// Hint: This will set stateSetExplicitly to true
5252
func (o *Overall) Add(state int, output string) {
5353
switch state {
5454
case check.OK:
@@ -61,8 +61,8 @@ func (o *Overall) Add(state int, output string) {
6161
o.unknowns++
6262
}
6363

64-
// TODO: Might be a bit obscure that the Add method also sets stateSetExplicitely
65-
o.stateSetExplicitely = true
64+
// TODO: Might be a bit obscure that the Add method also sets stateSetExplicitly
65+
o.stateSetExplicitly = true
6666

6767
o.Outputs = append(o.Outputs, fmt.Sprintf("[%s] %s", check.StatusText(state), output))
6868
}
@@ -73,13 +73,14 @@ func (o *Overall) AddSubcheck(subcheck PartialResult) {
7373
}
7474

7575
// AddSubcheck adds a PartialResult to the PartialResult
76-
func (o *PartialResult) AddSubcheck(subcheck PartialResult) {
77-
o.PartialResults = append(o.PartialResults, subcheck)
76+
func (s *PartialResult) AddSubcheck(subcheck PartialResult) {
77+
s.PartialResults = append(s.PartialResults, subcheck)
7878
}
7979

8080
// GetStatus returns the current state (ok, warning, critical, unknown) of the Overall
8181
func (o *Overall) GetStatus() int {
82-
if o.stateSetExplicitely {
82+
if o.stateSetExplicitly {
83+
// nolint: gocritic
8384
if o.criticals > 0 {
8485
return check.Critical
8586
} else if o.unknowns > 0 {
@@ -92,7 +93,7 @@ func (o *Overall) GetStatus() int {
9293
return check.Unknown
9394
}
9495
} else {
95-
// state not set explicitely!
96+
// state not set explicitly!
9697
if len(o.PartialResults) == 0 {
9798
return check.Unknown
9899
}
@@ -144,8 +145,8 @@ func (o *Overall) GetSummary() string {
144145
return o.Summary
145146
}
146147

147-
// Was the state set explicitely?
148-
if o.stateSetExplicitely {
148+
// Was the state set explicitly?
149+
if o.stateSetExplicitly {
149150
// Yes, so lets generate it from the sum of the overall states
150151
if o.criticals > 0 {
151152
o.Summary += fmt.Sprintf("critical=%d ", o.criticals)
@@ -169,7 +170,7 @@ func (o *Overall) GetSummary() string {
169170
}
170171
}
171172

172-
if !o.stateSetExplicitely {
173+
if !o.stateSetExplicitly {
173174
// No, so lets combine the partial ones
174175
if len(o.PartialResults) == 0 {
175176
// Oh, we actually don't have those either
@@ -238,10 +239,10 @@ func (o *Overall) GetOutput() string {
238239
pdata.WriteString(" " + o.PartialResults[i].getPerfdata())
239240
}
240241

241-
pdata_string := strings.Trim(pdata.String(), " ")
242+
pdataString := strings.Trim(pdata.String(), " ")
242243

243-
if len(pdata_string) > 0 {
244-
output.WriteString("|" + pdata_string + "\n")
244+
if len(pdataString) > 0 {
245+
output.WriteString("|" + pdataString + "\n")
245246
}
246247
}
247248

@@ -266,15 +267,15 @@ func (s *PartialResult) getPerfdata() string {
266267
}
267268

268269
// getOutput generates indented output for all subsequent PartialResults
269-
func (s *PartialResult) getOutput(indent_level int) string {
270+
func (s *PartialResult) getOutput(indentLevel int) string {
270271
var output strings.Builder
271272

272-
prefix := strings.Repeat(" ", indent_level)
273+
prefix := strings.Repeat(" ", indentLevel)
273274
output.WriteString(prefix + "\\_ " + s.String() + "\n")
274275

275276
if s.PartialResults != nil {
276277
for _, ss := range s.PartialResults {
277-
output.WriteString(ss.getOutput(indent_level + 2))
278+
output.WriteString(ss.getOutput(indentLevel + 2))
278279
}
279280
}
280281

@@ -300,15 +301,15 @@ func (s *PartialResult) SetState(state int) error {
300301
}
301302

302303
s.state = state
303-
s.stateSetExplicitely = true
304+
s.stateSetExplicitly = true
304305

305306
return nil
306307
}
307308

308309
// GetStatus returns the current state (ok, warning, critical, unknown) of the PartialResult
309310
// nolint: unused
310311
func (s *PartialResult) GetStatus() int {
311-
if s.stateSetExplicitely {
312+
if s.stateSetExplicitly {
312313
return s.state
313314
}
314315

result/overall_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,22 @@ func TestOverall_GetStatus_GetSummary(t *testing.T) {
5353
expectedStatus: 3,
5454
},
5555
{
56-
actual: Overall{oks: 1, stateSetExplicitely: true},
56+
actual: Overall{oks: 1, stateSetExplicitly: true},
5757
expectedSummary: "states: ok=1",
5858
expectedStatus: 0,
5959
},
6060
{
61-
actual: Overall{criticals: 2, oks: 1, warnings: 2, unknowns: 1, stateSetExplicitely: true},
61+
actual: Overall{criticals: 2, oks: 1, warnings: 2, unknowns: 1, stateSetExplicitly: true},
6262
expectedSummary: "states: critical=2 unknown=1 warning=2 ok=1",
6363
expectedStatus: 2,
6464
},
6565
{
66-
actual: Overall{unknowns: 2, oks: 1, warnings: 2, stateSetExplicitely: true},
66+
actual: Overall{unknowns: 2, oks: 1, warnings: 2, stateSetExplicitly: true},
6767
expectedSummary: "states: unknown=2 warning=2 ok=1",
6868
expectedStatus: 3,
6969
},
7070
{
71-
actual: Overall{oks: 1, warnings: 2, stateSetExplicitely: true},
71+
actual: Overall{oks: 1, warnings: 2, stateSetExplicitly: true},
7272
expectedSummary: "states: warning=2 ok=1",
7373
expectedStatus: 1,
7474
},
@@ -112,7 +112,7 @@ func ExampleOverall_Add() {
112112
overall.Add(check.Critical, "The other is critical")
113113

114114
fmt.Printf("%#v\n", overall)
115-
// Output: result.Overall{oks:1, warnings:0, criticals:1, unknowns:0, Summary:"", stateSetExplicitely:true, Outputs:[]string{"[OK] One element is good", "[CRITICAL] The other is critical"}, PartialResults:[]result.PartialResult(nil)}
115+
// Output: result.Overall{oks:1, warnings:0, criticals:1, unknowns:0, Summary:"", stateSetExplicitly:true, Outputs:[]string{"[OK] One element is good", "[CRITICAL] The other is critical"}, PartialResults:[]result.PartialResult(nil)}
116116
}
117117

118118
func ExampleOverall_GetOutput() {

result/worst.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import "github.com/NETWAYS/go-check"
1010
// Order of preference: Critical, Unknown, Warning, Ok
1111
func WorstState(states ...int) int {
1212
overall := -1
13-
13+
// nolint: gocritic
1414
for _, state := range states {
1515
if state == check.Critical {
1616
overall = check.Critical

testhelper/for_main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package testhelper
22

33
import (
44
"bytes"
5-
"github.com/NETWAYS/go-check"
65
"io"
76
"os"
7+
8+
"github.com/NETWAYS/go-check"
89
)
910

1011
// Execute main function from a main package, while capturing its stdout

0 commit comments

Comments
 (0)