diff --git a/exit.go b/exit.go index 6f16d13..65123a6 100644 --- a/exit.go +++ b/exit.go @@ -33,7 +33,9 @@ func Exitf(rc int, output string, args ...interface{}) { func ExitRaw(rc int, output ...string) { var text strings.Builder - text.WriteString("[" + StatusText(rc) + "] -") + status, _ := GetStatusText(rc) + + text.WriteString("[" + status + "] -") for _, s := range output { text.WriteString(" " + s) diff --git a/result/overall.go b/result/overall.go index e822ef5..6d50d23 100644 --- a/result/overall.go +++ b/result/overall.go @@ -56,7 +56,8 @@ func NewPartialResult() PartialResult { // 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) + status, _ := check.GetStatusText(s.GetStatus()) + return fmt.Sprintf("[%s] %s", status, s.Output) } // Add adds a return state explicitly @@ -77,7 +78,9 @@ func (o *Overall) Add(state int, output string) { // 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)) + status, _ := check.GetStatusText(state) + + o.Outputs = append(o.Outputs, fmt.Sprintf("[%s] %s", status, output)) } // AddSubcheck adds a PartialResult to the Overall diff --git a/status.go b/status.go index 9c43e44..fa3e27e 100644 --- a/status.go +++ b/status.go @@ -1,5 +1,10 @@ package check +import ( + "fmt" + "strings" +) + const ( // OK means everything is fine OK = 0 @@ -15,17 +20,37 @@ const ( UnknownString = "UNKNOWN" ) -// StatusText returns the string corresponding to a state -func StatusText(status int) string { +// GetStatusText returns the string corresponding to a state +func GetStatusText(status int) (string, error) { switch status { case OK: - return OKString + return OKString, nil case Warning: - return WarningString + return WarningString, nil case Critical: - return CriticalString + return CriticalString, nil case Unknown: + return UnknownString, nil + } + + return "", fmt.Errorf("no status text for status: %d", status) +} + +// GetStatusInt returns a state corresponding to its +// common string representation +func GetStatusInt(status string) (int, error) { + status = strings.ToUpper(status) + + switch status { + case OKString, "0": + return OK, nil + case WarningString, "1": + return Warning, nil + case CriticalString, "2": + return Critical, nil + case UnknownString, "3": + return Unknown, nil } - return UnknownString + return -1, fmt.Errorf("no integer for status: %s", status) } diff --git a/status_test.go b/status_test.go index e2d5441..ceaf9f4 100644 --- a/status_test.go +++ b/status_test.go @@ -29,7 +29,7 @@ func TestStatusText(t *testing.T) { for name, tc := range testcases { t.Run(name, func(t *testing.T) { - actual := StatusText(tc.input) + actual, _ := GetStatusText(tc.input) if actual != tc.expected { t.Fatalf("expected %v, got %v", tc.expected, actual) @@ -37,3 +37,41 @@ func TestStatusText(t *testing.T) { }) } } + +func TestStatusInt(t *testing.T) { + testcases := map[string]struct { + input string + expected int + }{ + "OK": { + expected: 0, + input: "OK", + }, + "WARNING": { + expected: 1, + input: "warning", + }, + "CRITICAL": { + expected: 2, + input: "Critical", + }, + "UNKNOWN": { + expected: 3, + input: "unknown", + }, + "Invalid-Input": { + expected: -1, + input: "Something else", + }, + } + + for name, tc := range testcases { + t.Run(name, func(t *testing.T) { + actual, _ := GetStatusInt(tc.input) + + if actual != tc.expected { + t.Error("\nActual: ", actual, "\nExpected: ", tc.expected) + } + }) + } +}