Skip to content

Commit

Permalink
Refactor exit behaviour and deprecate Exit()
Browse files Browse the repository at this point in the history
Shall be replaced by either Exitf() or ExitRaw().

fixes #31
  • Loading branch information
lazyfrosch committed Jun 18, 2021
1 parent ef93dab commit a89b591
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 40 deletions.
13 changes: 8 additions & 5 deletions README.md
Expand Up @@ -16,8 +16,6 @@ package main

import (
"github.com/NETWAYS/go-check"
log "github.com/sirupsen/logrus"
"os"
)

func main() {
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions cmd/check_example/main.go
Expand Up @@ -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)
}
}
26 changes: 6 additions & 20 deletions config_test.go
@@ -1,10 +1,5 @@
package check

import (
log "github.com/sirupsen/logrus"
"os"
)

func ExampleConfig() {
config := NewConfig()
config.Name = "check_test"
Expand All @@ -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
}
41 changes: 34 additions & 7 deletions exit.go
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"runtime/debug"
"strconv"

"github.com/mitchellh/go-ps"
)
Expand All @@ -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
Expand All @@ -56,6 +83,6 @@ func CatchPanic() {
output += "\n\n" + string(debug.Stack())
}

Exit(Unknown, output)
ExitRaw(Unknown, output)
}
}
16 changes: 14 additions & 2 deletions exit_test.go
Expand Up @@ -7,15 +7,27 @@ 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
}

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
}

Expand Down
6 changes: 3 additions & 3 deletions timeout.go
Expand Up @@ -25,16 +25,16 @@ 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)
timeoutEnabled = true

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")
}
}

0 comments on commit a89b591

Please sign in to comment.