Skip to content

Commit a89b591

Browse files
committed
Refactor exit behaviour and deprecate Exit()
Shall be replaced by either Exitf() or ExitRaw(). fixes #31
1 parent ef93dab commit a89b591

File tree

6 files changed

+68
-40
lines changed

6 files changed

+68
-40
lines changed

README.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ package main
1616

1717
import (
1818
"github.com/NETWAYS/go-check"
19-
log "github.com/sirupsen/logrus"
20-
"os"
2119
)
2220

2321
func main() {
@@ -28,14 +26,19 @@ func main() {
2826

2927
_ = config.FlagSet.StringP("hostname", "H", "localhost", "Hostname to check")
3028

31-
os.Args = []string{"check_example", "--help"}
32-
3329
config.ParseArguments()
3430

35-
log.Info("test")
31+
// Some checking should be done here, when --help is not passed
32+
33+
check.Exitf(check.OK, "Everything is fine - answer=%d", 42)
3634
}
3735
```
3836

37+
```
38+
OK - Everything is fine - answer=42
39+
would exit with code 0
40+
```
41+
3942
See the [documentation on pkg.go.dev](https://pkg.go.dev/github.com/NETWAYS/go-check) for more details and examples.
4043

4144
## Plugins

cmd/check_example/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ func main() {
2424
// time.Sleep(20 * time.Second)
2525

2626
if *value > *critical {
27-
check.Exit(check.Critical, "value is %d", *value)
27+
check.Exitf(check.Critical, "value is %d", *value)
2828
} else if *value > *warning {
29-
check.Exit(check.Warning, "value is %d", *value)
29+
check.Exitf(check.Warning, "value is %d", *value)
3030
} else {
31-
check.Exit(check.OK, "value is %d", *value)
31+
check.Exitf(check.OK, "value is %d", *value)
3232
}
3333
}

config_test.go

+6-20
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
package check
22

3-
import (
4-
log "github.com/sirupsen/logrus"
5-
"os"
6-
)
7-
83
func ExampleConfig() {
94
config := NewConfig()
105
config.Name = "check_test"
@@ -13,21 +8,12 @@ func ExampleConfig() {
138

149
_ = config.FlagSet.StringP("hostname", "H", "localhost", "Hostname to check")
1510

16-
os.Args = []string{"check_example", "--help"}
17-
1811
config.ParseArguments()
1912

20-
log.Info("test")
21-
// Output: Usage of check_test
22-
//
23-
// Test Plugin
24-
//
25-
// Arguments:
26-
// -H, --hostname string Hostname to check (default "localhost")
27-
// -t, --timeout int Abort the check after n seconds (default 30)
28-
// -d, --debug Enable debug mode
29-
// -v, --verbose Enable verbose mode
30-
// -V, --version Print version and exit
31-
// UNKNOWN - pflag: help requested
32-
// would exit with code 3
13+
// Some checking should be done here
14+
15+
Exitf(OK, "Everything is fine - answer=%d", 42)
16+
17+
// Output: OK - Everything is fine - answer=42
18+
// would exit with code 0
3319
}

exit.go

+34-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"runtime/debug"
7+
"strconv"
78

89
"github.com/mitchellh/go-ps"
910
)
@@ -16,25 +17,51 @@ var AllowExit = true
1617
// PrintStack prints the error stack when recovering from a panic with CatchPanic()
1718
var PrintStack = true
1819

20+
// Exitf prints the plugin output using formatting and exits the program.
21+
//
22+
// Output is the formatting string, and the rest of the arguments help adding values.
23+
//
24+
// Also see fmt package: https://golang.org/pkg/fmt
25+
func Exitf(rc int, output string, args ...interface{}) {
26+
ExitRaw(rc, fmt.Sprintf(output, args...))
27+
}
28+
29+
// ExitRaw prints the plugin output with the state prefixed and exits the program.
30+
//
31+
// Example:
32+
// OK - everything is fine
33+
func ExitRaw(rc int, output ...string) {
34+
text := StatusText(rc) + " -"
35+
36+
for _, s := range output {
37+
text += " " + s
38+
}
39+
40+
text += "\n"
41+
42+
_, _ = os.Stdout.WriteString(text)
43+
44+
BaseExit(rc)
45+
}
46+
1947
// Exit prints the plugin output and exits the program
48+
//
49+
// Deprecated, please use Exitf or ExitRaw.
2050
func Exit(rc int, output string, args ...interface{}) {
21-
fmt.Println(StatusText(rc), "-", fmt.Sprintf(output, args...))
22-
BaseExit(rc)
51+
Exitf(rc, output, args...)
2352
}
2453

2554
func BaseExit(rc int) {
2655
if AllowExit {
2756
os.Exit(rc)
2857
} else {
29-
fmt.Println("would exit with code", rc)
58+
_, _ = os.Stdout.WriteString("would exit with code " + strconv.Itoa(rc) + "\n")
3059
}
3160
}
3261

3362
// ExitError exists with an Unknown state while reporting the error
34-
//
35-
// TODO: more information about the error
3663
func ExitError(err error) {
37-
Exit(Unknown, err.Error())
64+
Exitf(Unknown, "%s (%T)", err.Error(), err)
3865
}
3966

4067
// CatchPanic is a general function for defer, to capture any panic that occurred during runtime of a check
@@ -56,6 +83,6 @@ func CatchPanic() {
5683
output += "\n\n" + string(debug.Stack())
5784
}
5885

59-
Exit(Unknown, output)
86+
ExitRaw(Unknown, output)
6087
}
6188
}

exit_test.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,27 @@ import (
77
)
88

99
func ExampleExit() {
10-
Exit(OK, "Everything is fine")
10+
Exitf(OK, "Everything is fine - value=%d", 42)
11+
// Output: OK - Everything is fine - value=42
12+
// would exit with code 0
13+
}
14+
15+
func ExampleExitf() {
16+
Exitf(OK, "Everything is fine - value=%d", 42)
17+
// Output: OK - Everything is fine - value=42
18+
// would exit with code 0
19+
}
20+
21+
func ExampleExitRaw() {
22+
ExitRaw(OK, "Everything is fine")
1123
// Output: OK - Everything is fine
1224
// would exit with code 0
1325
}
1426

1527
func ExampleExitError() {
1628
err := fmt.Errorf("connection to %s has been timed out", "localhost:12345")
1729
ExitError(err)
18-
// Output: UNKNOWN - connection to localhost:12345 has been timed out
30+
// Output: UNKNOWN - connection to localhost:12345 has been timed out (*errors.errorString)
1931
// would exit with code 3
2032
}
2133

timeout.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ func HandleTimeout(timeout int) {
2525
signal.Notify(signals, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP)
2626

2727
if timeout < 1 {
28-
Exit(Unknown, "Invalid timeout: %d", timeout)
28+
Exitf(Unknown, "Invalid timeout: %d", timeout)
2929
}
3030

3131
timedOut := time.After(time.Duration(timeout) * time.Second)
3232
timeoutEnabled = true
3333

3434
select {
3535
case s := <-signals:
36-
Exit(Unknown, "Received signal: %s", s)
36+
Exitf(Unknown, "Received signal: %s", s)
3737
case <-timedOut:
38-
Exit(Unknown, "Timeout reached")
38+
ExitRaw(Unknown, "Timeout reached")
3939
}
4040
}

0 commit comments

Comments
 (0)