-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |