Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

overwrite status. #20

Merged
merged 5 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"}
lufia marked this conversation as resolved.
Show resolved Hide resolved

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.")
}
lufia marked this conversation as resolved.
Show resolved Hide resolved

yseto marked this conversation as resolved.
Show resolved Hide resolved
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, "=")
lufia marked this conversation as resolved.
Show resolved Hide resolved
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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case emits an error by invalid, not the second =.

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) ExitStatusAs(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=
Loading