Skip to content

Commit

Permalink
Update golangci-lint configuration (#105)
Browse files Browse the repository at this point in the history
* Set golangci-lint to be more strict

* Fix various golangci-lint issues
  • Loading branch information
martialblog committed Oct 18, 2023
1 parent f4ebaad commit 1a88748
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 69 deletions.
59 changes: 40 additions & 19 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions convert/bytes_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions exit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions perfdata/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion perfdata/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
69 changes: 35 additions & 34 deletions result/overall.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,35 @@ 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
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:
Expand All @@ -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))
}
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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")
}
}

Expand All @@ -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))
}
}

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

s.state = state
s.stateSetExplicitely = true
s.stateSetExplicitly = true

return nil
}

// 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
}

Expand Down
10 changes: 5 additions & 5 deletions result/overall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion result/worst.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion testhelper/for_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions threshold.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 1a88748

Please sign in to comment.