Skip to content

Commit

Permalink
overwrite status.
Browse files Browse the repository at this point in the history
  • Loading branch information
yseto committed Sep 13, 2023
1 parent 490c13a commit c5d0d38
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 0 deletions.
56 changes: 56 additions & 0 deletions asstatusparse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package checkers

import (
"fmt"
"strings"

"golang.org/x/exp/slices"
)

var Keywords = []string{"OK", "WARNING", "CRITICAL", "UNKNOWN"}

func StrToStatus(s string) Status {
switch s {
case "OK":
return OK
case "WARNING":
return WARNING
case "CRITICAL":
return CRITICAL
case "UNKNOWN":
return UNKNOWN
}
panic("invalid inputs.")
}

func Parse(arg string) (map[Status]Status, error) {
if arg == "" {
return nil, nil
}

maps := make(map[Status]Status, 0)
args := strings.Split(arg, ",")

for _, s := range args {
if s == "" {
continue
}
v := strings.Split(s, "=")
if len(v) != 2 {
return nil, fmt.Errorf("arguments is invalid : %v", v)
}
from := strings.ToUpper(v[0])
to := strings.ToUpper(v[1])
if !slices.Contains(Keywords, from) {
return nil, fmt.Errorf("from argument is invalid : %s", v[0])
}
if !slices.Contains(Keywords, to) {
return nil, fmt.Errorf("to argument is invalid : %s", v[1])
}
if _, ok := maps[StrToStatus(from)]; ok {
return nil, fmt.Errorf("arguments is duplicate : %v", from)
}
maps[StrToStatus(from)] = StrToStatus(to)
}
return maps, nil
}
63 changes: 63 additions & 0 deletions asstatusparse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package checkers

import (
"reflect"
"testing"
)

func TestParse(t *testing.T) {
tests := []struct {
name string
source string
expected map[Status]Status
wantErr bool
}{
{
name: "normal",
source: "unknown=critical",
expected: map[Status]Status{
UNKNOWN: CRITICAL,
},
},
{
name: "multi",
source: "unknown=critical,warning=critical",
expected: map[Status]Status{
UNKNOWN: CRITICAL,
WARNING: CRITICAL,
},
},
{
name: "from error",
source: "invalid=critical",
wantErr: true,
},
{
name: "to error",
source: "critical=invalid",
wantErr: true,
},
{
name: "argument error",
source: "invalid=critical=invalid",
wantErr: true,
},
{
name: "duplicate error",
source: "critical=unknown,critical=warning",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual, err := Parse(tt.source)
if (err != nil) != tt.wantErr {
t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(tt.expected, actual) {
t.Errorf("Parse() should be %+v but got: %+v", tt.expected, actual)
}
})
}
}
9 changes: 9 additions & 0 deletions checkers.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ func (ckr *Checker) Exit() {
exit(int(ckr.Status))
}

func (ckr *Checker) ExitAsStatus(maps map[Status]Status) {
if _, ok := maps[ckr.Status]; ok {
ckr.Status = maps[ckr.Status]
}

fmt.Println(ckr.String())
exit(int(ckr.Status))
}

func (ckr *Checker) String() string {
return fmt.Sprintf("%s %s: %s", ckr.Name, ckr.Status, ckr.Message)
}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/mackerelio/checkers

go 1.18

require golang.org/x/exp v0.0.0-20230905200255-921286631fa9
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=

0 comments on commit c5d0d38

Please sign in to comment.