From 1a88748653bd00f87bdca73e1a2271c2b9ba38b4 Mon Sep 17 00:00:00 2001 From: Markus Opolka Date: Wed, 18 Oct 2023 16:04:48 +0200 Subject: [PATCH] Update golangci-lint configuration (#105) * Set golangci-lint to be more strict * Fix various golangci-lint issues --- .golangci.yml | 59 +++++++++++++++++++++++------------ config.go | 2 +- convert/bytes_common.go | 6 ++-- exit.go | 4 +-- perfdata/list.go | 1 + perfdata/type.go | 3 +- result/overall.go | 69 +++++++++++++++++++++-------------------- result/overall_test.go | 10 +++--- result/worst.go | 2 +- testhelper/for_main.go | 3 +- threshold.go | 4 +-- 11 files changed, 94 insertions(+), 69 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index c554c16..7a14c22 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,23 +1,44 @@ run: timeout: 5m tests: false + skip-files: + - 'testhelper/for_main.go' +issues: + exclude-rules: + - path: 'result/overall.go' + linters: + - nestif + - path: 'convert/bytes_common.go' + linters: + - ireturn linters: - disable-all: false - enable: - - funlen - - dogsled - - dupl - - lll - - whitespace - - wsl - - exportloopref - disable: - - scopelint - - deadcode - - structcheck - - varcheck - - musttag - presets: - - bugs - - unused - fast: false + enable-all: true + disable: + - cyclop + - depguard + - exhaustivestruct + - exhaustruct + - forbidigo + - forcetypeassert + - gci + - gochecknoglobals + - gochecknoinits + - godox + - godot + - goerr113 + - gofumpt + - gomnd + - lll + - musttag + - nakedret + - nlreturn + - nolintlint + - nonamedreturns + - tagliatelle + - varnamelen + - wrapcheck +linters-settings: + estif: + min-complexity: 4 + maligned: + suggest-new: true diff --git a/config.go b/config.go index caef3ca..bbda5a2 100644 --- a/config.go +++ b/config.go @@ -126,7 +126,7 @@ func LoadFromEnv(config interface{}) { // Potential for addding different types, for now we only use strings // since the main use case is credentials - // nolint: exhaustive + // nolint: exhaustive, gocritic switch field.Type.Kind() { case reflect.String: configValue.Field(i).SetString(envValue) diff --git a/convert/bytes_common.go b/convert/bytes_common.go index 0ade0c5..c3e8fb0 100644 --- a/convert/bytes_common.go +++ b/convert/bytes_common.go @@ -71,10 +71,10 @@ func ParseBytes(value string) (ByteAny, error) { // If the input value is 0, humanReadable will always return "0B" // // Examples: -// 1073741824B -> 1000KB -// 2147483648B -> 2MB -// 0 -> 0MB // +// 1073741824B -> 1000KB +// 2147483648B -> 2MB +// 0 -> 0MB func humanReadable(b uint64, units []string, base float64) (float64, string) { if b == 0 { return 0, "B" diff --git a/exit.go b/exit.go index c5c4479..6f16d13 100644 --- a/exit.go +++ b/exit.go @@ -52,9 +52,9 @@ func ExitRaw(rc int, output ...string) { func BaseExit(rc int) { if AllowExit { os.Exit(rc) - } else { - _, _ = os.Stdout.WriteString("would exit with code " + strconv.Itoa(rc) + "\n") } + + _, _ = os.Stdout.WriteString("would exit with code " + strconv.Itoa(rc) + "\n") } // ExitError exists with an Unknown state while reporting the error diff --git a/perfdata/list.go b/perfdata/list.go index 3375ece..cca58a0 100644 --- a/perfdata/list.go +++ b/perfdata/list.go @@ -5,6 +5,7 @@ import ( ) // PerfdataList can store multiple perfdata and brings a simple fmt.Stringer interface +// nolint: golint, revive type PerfdataList []*Perfdata // String returns string representations of all Perfdata diff --git a/perfdata/type.go b/perfdata/type.go index b4d1a7e..e5f2563 100644 --- a/perfdata/type.go +++ b/perfdata/type.go @@ -2,8 +2,9 @@ package perfdata import ( "fmt" - "github.com/NETWAYS/go-check" "strings" + + "github.com/NETWAYS/go-check" ) // Replace not allowed characters inside a label diff --git a/result/overall.go b/result/overall.go index bab97ad..ad4ec9b 100644 --- a/result/overall.go +++ b/result/overall.go @@ -20,25 +20,25 @@ import ( // one suffices, but one fails, the whole check might be OK and only the subcheck // Warning or Critical. type Overall struct { - oks int - warnings int - criticals int - unknowns int - Summary string - stateSetExplicitely bool - Outputs []string // Deprecate this in a future version - PartialResults []PartialResult + oks int + warnings int + criticals int + unknowns int + Summary string + stateSetExplicitly bool + Outputs []string // Deprecate this in a future version + PartialResults []PartialResult } // PartialResult represents a sub-result for an Overall struct type PartialResult struct { - state int // Result state, either set explicitely or derived from partialResults - Output string - stateSetExplicitely bool // nolint: unused - defaultState int // Default result state, if no partial results are available and no state is set explicitely - defaultStateSet bool // nolint: unused - Perfdata perfdata.PerfdataList - PartialResults []PartialResult + Perfdata perfdata.PerfdataList + PartialResults []PartialResult + Output string + state int // Result state, either set explicitly or derived from partialResults + defaultState int // Default result state, if no partial results are available and no state is set explicitly + stateSetExplicitly bool // nolint: unused + defaultStateSet bool // nolint: unused } // String returns the status and output of the PartialResult @@ -46,9 +46,9 @@ func (s *PartialResult) String() string { return fmt.Sprintf("[%s] %s", check.StatusText(s.GetStatus()), s.Output) } -// Add adds a return state explicitely +// Add adds a return state explicitly // -// Hint: This will set stateSetExplicitely to true +// Hint: This will set stateSetExplicitly to true func (o *Overall) Add(state int, output string) { switch state { case check.OK: @@ -61,8 +61,8 @@ func (o *Overall) Add(state int, output string) { o.unknowns++ } - // TODO: Might be a bit obscure that the Add method also sets stateSetExplicitely - o.stateSetExplicitely = true + // TODO: Might be a bit obscure that the Add method also sets stateSetExplicitly + o.stateSetExplicitly = true o.Outputs = append(o.Outputs, fmt.Sprintf("[%s] %s", check.StatusText(state), output)) } @@ -73,13 +73,14 @@ func (o *Overall) AddSubcheck(subcheck PartialResult) { } // AddSubcheck adds a PartialResult to the PartialResult -func (o *PartialResult) AddSubcheck(subcheck PartialResult) { - o.PartialResults = append(o.PartialResults, subcheck) +func (s *PartialResult) AddSubcheck(subcheck PartialResult) { + s.PartialResults = append(s.PartialResults, subcheck) } // GetStatus returns the current state (ok, warning, critical, unknown) of the Overall func (o *Overall) GetStatus() int { - if o.stateSetExplicitely { + if o.stateSetExplicitly { + // nolint: gocritic if o.criticals > 0 { return check.Critical } else if o.unknowns > 0 { @@ -92,7 +93,7 @@ func (o *Overall) GetStatus() int { return check.Unknown } } else { - // state not set explicitely! + // state not set explicitly! if len(o.PartialResults) == 0 { return check.Unknown } @@ -144,8 +145,8 @@ func (o *Overall) GetSummary() string { return o.Summary } - // Was the state set explicitely? - if o.stateSetExplicitely { + // Was the state set explicitly? + if o.stateSetExplicitly { // Yes, so lets generate it from the sum of the overall states if o.criticals > 0 { o.Summary += fmt.Sprintf("critical=%d ", o.criticals) @@ -169,7 +170,7 @@ func (o *Overall) GetSummary() string { } } - if !o.stateSetExplicitely { + if !o.stateSetExplicitly { // No, so lets combine the partial ones if len(o.PartialResults) == 0 { // Oh, we actually don't have those either @@ -238,10 +239,10 @@ func (o *Overall) GetOutput() string { pdata.WriteString(" " + o.PartialResults[i].getPerfdata()) } - pdata_string := strings.Trim(pdata.String(), " ") + pdataString := strings.Trim(pdata.String(), " ") - if len(pdata_string) > 0 { - output.WriteString("|" + pdata_string + "\n") + if len(pdataString) > 0 { + output.WriteString("|" + pdataString + "\n") } } @@ -266,15 +267,15 @@ func (s *PartialResult) getPerfdata() string { } // getOutput generates indented output for all subsequent PartialResults -func (s *PartialResult) getOutput(indent_level int) string { +func (s *PartialResult) getOutput(indentLevel int) string { var output strings.Builder - prefix := strings.Repeat(" ", indent_level) + prefix := strings.Repeat(" ", indentLevel) output.WriteString(prefix + "\\_ " + s.String() + "\n") if s.PartialResults != nil { for _, ss := range s.PartialResults { - output.WriteString(ss.getOutput(indent_level + 2)) + output.WriteString(ss.getOutput(indentLevel + 2)) } } @@ -300,7 +301,7 @@ func (s *PartialResult) SetState(state int) error { } s.state = state - s.stateSetExplicitely = true + s.stateSetExplicitly = true return nil } @@ -308,7 +309,7 @@ func (s *PartialResult) SetState(state int) error { // GetStatus returns the current state (ok, warning, critical, unknown) of the PartialResult // nolint: unused func (s *PartialResult) GetStatus() int { - if s.stateSetExplicitely { + if s.stateSetExplicitly { return s.state } diff --git a/result/overall_test.go b/result/overall_test.go index b74319e..ff443e1 100644 --- a/result/overall_test.go +++ b/result/overall_test.go @@ -53,22 +53,22 @@ func TestOverall_GetStatus_GetSummary(t *testing.T) { expectedStatus: 3, }, { - actual: Overall{oks: 1, stateSetExplicitely: true}, + actual: Overall{oks: 1, stateSetExplicitly: true}, expectedSummary: "states: ok=1", expectedStatus: 0, }, { - actual: Overall{criticals: 2, oks: 1, warnings: 2, unknowns: 1, stateSetExplicitely: true}, + actual: Overall{criticals: 2, oks: 1, warnings: 2, unknowns: 1, stateSetExplicitly: true}, expectedSummary: "states: critical=2 unknown=1 warning=2 ok=1", expectedStatus: 2, }, { - actual: Overall{unknowns: 2, oks: 1, warnings: 2, stateSetExplicitely: true}, + actual: Overall{unknowns: 2, oks: 1, warnings: 2, stateSetExplicitly: true}, expectedSummary: "states: unknown=2 warning=2 ok=1", expectedStatus: 3, }, { - actual: Overall{oks: 1, warnings: 2, stateSetExplicitely: true}, + actual: Overall{oks: 1, warnings: 2, stateSetExplicitly: true}, expectedSummary: "states: warning=2 ok=1", expectedStatus: 1, }, @@ -112,7 +112,7 @@ func ExampleOverall_Add() { overall.Add(check.Critical, "The other is critical") fmt.Printf("%#v\n", overall) - // 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)} + // 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)} } func ExampleOverall_GetOutput() { diff --git a/result/worst.go b/result/worst.go index ecc0378..8809d6a 100644 --- a/result/worst.go +++ b/result/worst.go @@ -10,7 +10,7 @@ import "github.com/NETWAYS/go-check" // Order of preference: Critical, Unknown, Warning, Ok func WorstState(states ...int) int { overall := -1 - + // nolint: gocritic for _, state := range states { if state == check.Critical { overall = check.Critical diff --git a/testhelper/for_main.go b/testhelper/for_main.go index 6a48c52..703f09e 100644 --- a/testhelper/for_main.go +++ b/testhelper/for_main.go @@ -2,9 +2,10 @@ package testhelper import ( "bytes" - "github.com/NETWAYS/go-check" "io" "os" + + "github.com/NETWAYS/go-check" ) // Execute main function from a main package, while capturing its stdout diff --git a/threshold.go b/threshold.go index 73ebeed..0dd05d6 100644 --- a/threshold.go +++ b/threshold.go @@ -106,9 +106,9 @@ func (t Threshold) String() (s string) { func (t Threshold) DoesViolate(value float64) bool { if t.Inside { return value >= t.Lower && value <= t.Upper - } else { - return value < t.Lower || value > t.Upper } + + return value < t.Lower || value > t.Upper } // BoundaryToString returns the string representation of a Threshold boundary.