-
Notifications
You must be signed in to change notification settings - Fork 1
/
thresholds.go
62 lines (49 loc) · 1.15 KB
/
thresholds.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
package thresholds
import (
"strings"
"github.com/NETWAYS/go-check"
"github.com/spf13/pflag"
)
type ThresholdWrapper struct {
Th check.Threshold
IsSet bool
}
type Thresholds struct {
Warn ThresholdWrapper
Crit ThresholdWrapper
}
func (t *ThresholdWrapper) Set(foo string) error {
tmp, err := check.ParseThreshold(foo)
if err != nil {
return err
}
t.Th.Inside = tmp.Inside
t.Th.Lower = tmp.Lower
t.Th.Upper = tmp.Upper
t.IsSet = true
return nil
}
func (t *ThresholdWrapper) String() string {
return t.Th.String()
}
func (t *ThresholdWrapper) Type() string {
return "Range_Expression"
}
type ThresholdOption struct {
Th *ThresholdWrapper
FlagString string
Description string
Default ThresholdWrapper
}
func AddFlags(flagP *pflag.FlagSet, ths *[]ThresholdOption) {
for i := range *ths {
desc := strings.Builder{}
desc.WriteString((*ths)[i].Description)
if (*ths)[i].Default.IsSet {
desc.WriteString(" (Default: " + (*ths)[i].Default.Th.String() + ")")
(*ths)[i].Th.IsSet = (*ths)[i].Default.IsSet
(*ths)[i].Th.Th = (*ths)[i].Default.Th
}
flagP.Var((*ths)[i].Th, (*ths)[i].FlagString, desc.String())
}
}