From a89b59199d8493b63c246ac92e3aa46f117f431b Mon Sep 17 00:00:00 2001 From: Markus Frosch Date: Fri, 18 Jun 2021 11:05:31 +0200 Subject: [PATCH] Refactor exit behaviour and deprecate Exit() Shall be replaced by either Exitf() or ExitRaw(). fixes #31 --- README.md | 13 ++++++++----- cmd/check_example/main.go | 6 +++--- config_test.go | 26 ++++++------------------- exit.go | 41 ++++++++++++++++++++++++++++++++------- exit_test.go | 16 +++++++++++++-- timeout.go | 6 +++--- 6 files changed, 68 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 1e2fbe5..a97594c 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,6 @@ package main import ( "github.com/NETWAYS/go-check" - log "github.com/sirupsen/logrus" - "os" ) func main() { @@ -28,14 +26,19 @@ func main() { _ = config.FlagSet.StringP("hostname", "H", "localhost", "Hostname to check") - os.Args = []string{"check_example", "--help"} - config.ParseArguments() - log.Info("test") + // Some checking should be done here, when --help is not passed + + check.Exitf(check.OK, "Everything is fine - answer=%d", 42) } ``` +``` +OK - Everything is fine - answer=42 +would exit with code 0 +``` + See the [documentation on pkg.go.dev](https://pkg.go.dev/github.com/NETWAYS/go-check) for more details and examples. ## Plugins diff --git a/cmd/check_example/main.go b/cmd/check_example/main.go index a098f1b..68777a7 100644 --- a/cmd/check_example/main.go +++ b/cmd/check_example/main.go @@ -24,10 +24,10 @@ func main() { // time.Sleep(20 * time.Second) if *value > *critical { - check.Exit(check.Critical, "value is %d", *value) + check.Exitf(check.Critical, "value is %d", *value) } else if *value > *warning { - check.Exit(check.Warning, "value is %d", *value) + check.Exitf(check.Warning, "value is %d", *value) } else { - check.Exit(check.OK, "value is %d", *value) + check.Exitf(check.OK, "value is %d", *value) } } diff --git a/config_test.go b/config_test.go index 3b7883b..d7db1ac 100644 --- a/config_test.go +++ b/config_test.go @@ -1,10 +1,5 @@ package check -import ( - log "github.com/sirupsen/logrus" - "os" -) - func ExampleConfig() { config := NewConfig() config.Name = "check_test" @@ -13,21 +8,12 @@ func ExampleConfig() { _ = config.FlagSet.StringP("hostname", "H", "localhost", "Hostname to check") - os.Args = []string{"check_example", "--help"} - config.ParseArguments() - log.Info("test") - // Output: Usage of check_test - // - // Test Plugin - // - // Arguments: - // -H, --hostname string Hostname to check (default "localhost") - // -t, --timeout int Abort the check after n seconds (default 30) - // -d, --debug Enable debug mode - // -v, --verbose Enable verbose mode - // -V, --version Print version and exit - // UNKNOWN - pflag: help requested - // would exit with code 3 + // Some checking should be done here + + Exitf(OK, "Everything is fine - answer=%d", 42) + + // Output: OK - Everything is fine - answer=42 + // would exit with code 0 } diff --git a/exit.go b/exit.go index 60ff93c..db6b081 100644 --- a/exit.go +++ b/exit.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "runtime/debug" + "strconv" "github.com/mitchellh/go-ps" ) @@ -16,25 +17,51 @@ var AllowExit = true // PrintStack prints the error stack when recovering from a panic with CatchPanic() var PrintStack = true +// Exitf prints the plugin output using formatting and exits the program. +// +// Output is the formatting string, and the rest of the arguments help adding values. +// +// Also see fmt package: https://golang.org/pkg/fmt +func Exitf(rc int, output string, args ...interface{}) { + ExitRaw(rc, fmt.Sprintf(output, args...)) +} + +// ExitRaw prints the plugin output with the state prefixed and exits the program. +// +// Example: +// OK - everything is fine +func ExitRaw(rc int, output ...string) { + text := StatusText(rc) + " -" + + for _, s := range output { + text += " " + s + } + + text += "\n" + + _, _ = os.Stdout.WriteString(text) + + BaseExit(rc) +} + // Exit prints the plugin output and exits the program +// +// Deprecated, please use Exitf or ExitRaw. func Exit(rc int, output string, args ...interface{}) { - fmt.Println(StatusText(rc), "-", fmt.Sprintf(output, args...)) - BaseExit(rc) + Exitf(rc, output, args...) } func BaseExit(rc int) { if AllowExit { os.Exit(rc) } else { - fmt.Println("would exit with code", rc) + _, _ = os.Stdout.WriteString("would exit with code " + strconv.Itoa(rc) + "\n") } } // ExitError exists with an Unknown state while reporting the error -// -// TODO: more information about the error func ExitError(err error) { - Exit(Unknown, err.Error()) + Exitf(Unknown, "%s (%T)", err.Error(), err) } // CatchPanic is a general function for defer, to capture any panic that occurred during runtime of a check @@ -56,6 +83,6 @@ func CatchPanic() { output += "\n\n" + string(debug.Stack()) } - Exit(Unknown, output) + ExitRaw(Unknown, output) } } diff --git a/exit_test.go b/exit_test.go index 8b41149..e74dfeb 100644 --- a/exit_test.go +++ b/exit_test.go @@ -7,7 +7,19 @@ import ( ) func ExampleExit() { - Exit(OK, "Everything is fine") + Exitf(OK, "Everything is fine - value=%d", 42) + // Output: OK - Everything is fine - value=42 + // would exit with code 0 +} + +func ExampleExitf() { + Exitf(OK, "Everything is fine - value=%d", 42) + // Output: OK - Everything is fine - value=42 + // would exit with code 0 +} + +func ExampleExitRaw() { + ExitRaw(OK, "Everything is fine") // Output: OK - Everything is fine // would exit with code 0 } @@ -15,7 +27,7 @@ func ExampleExit() { func ExampleExitError() { err := fmt.Errorf("connection to %s has been timed out", "localhost:12345") ExitError(err) - // Output: UNKNOWN - connection to localhost:12345 has been timed out + // Output: UNKNOWN - connection to localhost:12345 has been timed out (*errors.errorString) // would exit with code 3 } diff --git a/timeout.go b/timeout.go index abe8d6d..778ebf8 100644 --- a/timeout.go +++ b/timeout.go @@ -25,7 +25,7 @@ func HandleTimeout(timeout int) { signal.Notify(signals, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP) if timeout < 1 { - Exit(Unknown, "Invalid timeout: %d", timeout) + Exitf(Unknown, "Invalid timeout: %d", timeout) } timedOut := time.After(time.Duration(timeout) * time.Second) @@ -33,8 +33,8 @@ func HandleTimeout(timeout int) { select { case s := <-signals: - Exit(Unknown, "Received signal: %s", s) + Exitf(Unknown, "Received signal: %s", s) case <-timedOut: - Exit(Unknown, "Timeout reached") + ExitRaw(Unknown, "Timeout reached") } }