From ecd42c4308bebed7f2508fbcfb8fd7e12570a18e Mon Sep 17 00:00:00 2001 From: RincewindsHat Date: Fri, 8 Nov 2024 10:08:16 +0100 Subject: [PATCH 1/6] Fix spelling + indentation --- README.md | 4 ++-- config.go | 2 +- perfdata/type.go | 4 ++-- result/overall.go | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 257ecce..dec50ff 100644 --- a/README.md +++ b/README.md @@ -34,8 +34,8 @@ func main() { // Some checking should be done here, when --help is not passed check.Exitf(check.OK, "Everything is fine - answer=%d", 42) - // Output: - // OK - Everything is fine - answer=42 + // Output: + // OK - Everything is fine - answer=42 } ``` diff --git a/config.go b/config.go index bbda5a2..ecc7ef6 100644 --- a/config.go +++ b/config.go @@ -124,7 +124,7 @@ func LoadFromEnv(config interface{}) { continue } - // Potential for addding different types, for now we only use strings + // Potential for adding different types, for now we only use strings // since the main use case is credentials // nolint: exhaustive, gocritic switch field.Type.Kind() { diff --git a/perfdata/type.go b/perfdata/type.go index d7c7963..9a29ee6 100644 --- a/perfdata/type.go +++ b/perfdata/type.go @@ -15,7 +15,7 @@ var replacer = strings.NewReplacer("=", "_", "`", "_", "'", "_", "\"", "_") // formatNumeric returns a string representation of various possible numerics // // This supports most internal types of Go and all fmt.Stringer interfaces. -// Returns an eror in some known cases where the value of a data type does not +// Returns an error in some known cases where the value of a data type does not // represent a valid measurement, e.g INF for floats // This error can probably ignored in most cases and the perfdata point omitted, // but silently dropping the value and returning the empty strings seems like bad style @@ -78,7 +78,7 @@ func (p Perfdata) String() string { } // ValidatedString returns the proper format for the plugin output -// Returns an eror in some known cases where the value of a data type does not +// Returns an error in some known cases where the value of a data type does not // represent a valid measurement, see the explanation for "formatNumeric" for // perfdata values. func (p Perfdata) ValidatedString() (string, error) { diff --git a/result/overall.go b/result/overall.go index cb58991..4276862 100644 --- a/result/overall.go +++ b/result/overall.go @@ -10,7 +10,7 @@ import ( "github.com/NETWAYS/go-check/perfdata" ) -// Overall is a singleton for a monitoring pluging that has several partial results (or sub-results) +// Overall is a singleton for a monitoring plugin that has several partial results (or sub-results) // // Design decisions: A check plugin has a single Overall (singleton), // each partial thing which is tested, gets its own subcheck. From db93f68363fd22c0dd683f3c8e4c7ef6938cad4c Mon Sep 17 00:00:00 2001 From: RincewindsHat Date: Fri, 8 Nov 2024 10:25:02 +0100 Subject: [PATCH 2/6] Update linter config --- .golangci.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 7a14c22..5590544 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,7 +1,7 @@ run: timeout: 5m tests: false - skip-files: +issues.exclude-files: - 'testhelper/for_main.go' issues: exclude-rules: @@ -16,7 +16,6 @@ linters: disable: - cyclop - depguard - - exhaustivestruct - exhaustruct - forbidigo - forcetypeassert @@ -25,7 +24,7 @@ linters: - gochecknoinits - godox - godot - - goerr113 + - err113 - gofumpt - gomnd - lll From 14b7b2d1f9c917905f41802460fdf86ba0343b4e Mon Sep 17 00:00:00 2001 From: RincewindsHat Date: Fri, 8 Nov 2024 10:25:44 +0100 Subject: [PATCH 3/6] Go to go 1.22 and run go mod tidy --- go.mod | 8 +++++++- go.sum | 9 --------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index d24a741..5989d16 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,14 @@ module github.com/NETWAYS/go-check -go 1.14 +go 1.22 require ( github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.9.0 ) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum index dde62d0..2989941 100644 --- a/go.sum +++ b/go.sum @@ -1,21 +1,12 @@ -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From d8d160945de9b7980a9507d2f1ed2fbe1dc6d1f1 Mon Sep 17 00:00:00 2001 From: RincewindsHat Date: Fri, 8 Nov 2024 10:25:56 +0100 Subject: [PATCH 4/6] Fix linter issues --- config.go | 4 +-- result/overall.go | 83 ++++++++++++++++++++++++----------------------- 2 files changed, 45 insertions(+), 42 deletions(-) diff --git a/config.go b/config.go index ecc7ef6..b10eeae 100644 --- a/config.go +++ b/config.go @@ -81,7 +81,7 @@ func (c *Config) ParseArray(arguments []string) { if c.PrintVersion { fmt.Println(c.Name, "version", c.Version) - BaseExit(3) + BaseExit(Unknown) } if c.DefaultHelper { @@ -109,7 +109,7 @@ func LoadFromEnv(config interface{}) { configValue := reflect.ValueOf(config).Elem() configType := configValue.Type() - for i := 0; i < configValue.NumField(); i++ { + for i := range configValue.NumField() { field := configType.Field(i) tag := field.Tag.Get("env") diff --git a/result/overall.go b/result/overall.go index 4276862..a820c4b 100644 --- a/result/overall.go +++ b/result/overall.go @@ -4,12 +4,15 @@ package result import ( "errors" "fmt" + "strconv" "strings" "github.com/NETWAYS/go-check" "github.com/NETWAYS/go-check/perfdata" ) +const indentationOffset = 2 + // Overall is a singleton for a monitoring plugin that has several partial results (or sub-results) // // Design decisions: A check plugin has a single Overall (singleton), @@ -98,53 +101,53 @@ func (o *Overall) GetStatus() int { return check.Warning } else if o.oks > 0 { return check.OK - } else { - return check.Unknown - } - } else { - // state not set explicitly! - if len(o.PartialResults) == 0 { - return check.Unknown } - var ( - criticals int - warnings int - oks int - unknowns int - ) + return check.Unknown + } - for _, sc := range o.PartialResults { - switch sc.GetStatus() { - case check.Critical: - criticals++ - case check.Warning: - warnings++ - case check.Unknown: - unknowns++ - case check.OK: - oks++ - } - } + // state not set explicitly! + if len(o.PartialResults) == 0 { + return check.Unknown + } - if criticals > 0 { - return check.Critical + var ( + criticals int + warnings int + oks int + unknowns int + ) + + for _, sc := range o.PartialResults { + switch sc.GetStatus() { + case check.Critical: + criticals++ + case check.Warning: + warnings++ + case check.Unknown: + unknowns++ + case check.OK: + oks++ } + } - if unknowns > 0 { - return check.Unknown - } + if criticals > 0 { + return check.Critical + } - if warnings > 0 { - return check.Warning - } + if unknowns > 0 { + return check.Unknown + } - if oks > 0 { - return check.OK - } + if warnings > 0 { + return check.Warning + } - return check.Unknown + if oks > 0 { + return check.OK } + + return check.Unknown } // GetSummary returns a text representation of the current state of the Overall @@ -284,7 +287,7 @@ func (s *PartialResult) getOutput(indentLevel int) string { if s.PartialResults != nil { for _, ss := range s.PartialResults { - output.WriteString(ss.getOutput(indentLevel + 2)) + output.WriteString(ss.getOutput(indentLevel + indentationOffset)) } } @@ -294,7 +297,7 @@ func (s *PartialResult) getOutput(indentLevel int) string { // SetDefaultState sets a new default state for a PartialResult func (s *PartialResult) SetDefaultState(state int) error { if state < check.OK || state > check.Unknown { - return errors.New("Default State is not a valid result state. Got " + fmt.Sprint(state) + " which is not valid") + return errors.New("Default State is not a valid result state. Got " + strconv.Itoa(state) + " which is not valid") } s.defaultState = state @@ -306,7 +309,7 @@ func (s *PartialResult) SetDefaultState(state int) error { // SetState sets a state for a PartialResult func (s *PartialResult) SetState(state int) error { if state < check.OK || state > check.Unknown { - return errors.New("Default State is not a valid result state. Got " + fmt.Sprint(state) + " which is not valid") + return errors.New("Default State is not a valid result state. Got " + strconv.Itoa(state) + " which is not valid") } s.state = state From e1ac0f2d333d266017aa95b6e0697a898626f711 Mon Sep 17 00:00:00 2001 From: RincewindsHat Date: Fri, 8 Nov 2024 10:33:52 +0100 Subject: [PATCH 5/6] Bump linter version in github runner config --- .github/workflows/golangci-lint.yml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index f5ee75e..4ee66d2 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -10,12 +10,9 @@ jobs: name: lint runs-on: ubuntu-latest steps: - - name: Set up Go - uses: actions/setup-go@v5 - - uses: actions/checkout@v4 - name: golangci-lint uses: golangci/golangci-lint-action@v6 with: - version: v1.54 + version: v1.61 From e6adfae8dae5142d80520763da08131cd661a93d Mon Sep 17 00:00:00 2001 From: RincewindsHat Date: Fri, 8 Nov 2024 13:49:00 +0100 Subject: [PATCH 6/6] Try to explain indentationOffset a bit --- result/overall.go | 1 + 1 file changed, 1 insertion(+) diff --git a/result/overall.go b/result/overall.go index a820c4b..1a9994e 100644 --- a/result/overall.go +++ b/result/overall.go @@ -11,6 +11,7 @@ import ( "github.com/NETWAYS/go-check/perfdata" ) +// The "width" of the indentation which is added on every level const indentationOffset = 2 // Overall is a singleton for a monitoring plugin that has several partial results (or sub-results)