-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathstatusmap.go
77 lines (69 loc) · 1.96 KB
/
statusmap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package checkers
import "strings"
type invalidStatusMapError string
func (err invalidStatusMapError) Error() string {
return "invalid status map: " + string(err)
}
type duplicateStatusMapError Status
func (err duplicateStatusMapError) Error() string {
return "duplicate status in map: " + Status(err).String()
}
func parseStatusPrefix(src string) (Status, string, bool) {
switch {
case strings.HasPrefix(src, "OK"):
return OK, src[len("OK"):], true
case strings.HasPrefix(src, "WARNING"):
return WARNING, src[len("WARNING"):], true
case strings.HasPrefix(src, "CRITICAL"):
return CRITICAL, src[len("CRITICAL"):], true
case strings.HasPrefix(src, "UNKNOWN"):
return UNKNOWN, src[len("UNKNOWN"):], true
default:
return OK, src, false
}
}
// Parse parses a string of the form <status>=<status>. <status> is one of:
//
// - ok
// - warning
// - critical
// - unknown
//
// You can specify multiple key-value pairs separated by commas.
//
// Deprecated: use ParseStatusMap instead.
func Parse(src string) (map[Status]Status, error) {
return ParseStatusMap(src)
}
// ParseStatusMap parses a string of the form <status>=<status>. <status> is one of:
//
// - ok
// - warning
// - critical
// - unknown
//
// You can specify multiple key-value pairs separated by commas.
func ParseStatusMap(src string) (map[Status]Status, error) {
orig, stm := src, map[Status]Status{}
for src = strings.ToUpper(src); src != ""; {
var from, to Status
var ok bool
if from, src, ok = parseStatusPrefix(src); !ok {
return nil, invalidStatusMapError(orig)
}
if src, ok = strings.CutPrefix(src, "="); !ok {
return nil, invalidStatusMapError(orig)
}
if to, src, ok = parseStatusPrefix(src); !ok {
return nil, invalidStatusMapError(orig)
}
if src, ok = strings.CutPrefix(src, ","); ok == (src == "") {
return nil, invalidStatusMapError(orig)
}
if _, ok = stm[from]; ok {
return nil, duplicateStatusMapError(from)
}
stm[from] = to
}
return stm, nil
}