-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheck_functions.go
64 lines (55 loc) · 1.72 KB
/
check_functions.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
package main
import (
"fmt"
"regexp"
"time"
"github.com/Knetic/govaluate"
)
// CheckFunctions will hold all custom govaluate functions for Check 'If'
// expressions
var CheckFunctions map[string]govaluate.ExpressionFunction
// CheckFunctionsInit will initialize CheckFunctions global variable
func CheckFunctionsInit() {
CheckFunctions = map[string]govaluate.ExpressionFunction{
"strlen": func(args ...interface{}) (interface{}, error) {
length := len(args[0].(string))
return (float64)(length), nil
},
"ping": func(args ...interface{}) (interface{}, error) {
if len(args) > 0 {
return nil, fmt.Errorf("ping function: too much arguments")
}
return (string)("pong"), nil
},
"date": func(args ...interface{}) (interface{}, error) {
if len(args) != 1 {
return nil, fmt.Errorf("date function: wrong argument count (1 required)")
}
format := args[0].(string)
now := time.Now()
switch format {
case "hour":
return (float64)(now.Hour()), nil
case "minute":
return (float64)(now.Minute()), nil
case "time":
return (float64)((float64)(now.Hour()) + (float64)(now.Minute())/60.0), nil
case "dow", "day-of-week":
// Sunday = 0
return (float64)(now.Weekday()), nil
case "dom", "day-of-month":
return (float64)(now.Day()), nil
case "now":
return (float64)(now.Unix()), nil
}
if match, _ := regexp.MatchString("^[0-9]{1,2}:[0-9]{2}$", format); match == true {
t, err := alertCheckHour(format)
if err != nil {
return nil, fmt.Errorf("date function: invalid hour '%s': %s", format, err)
}
return (float64)((float64)(t[0]) + (float64)(t[1])/60.0), nil
}
return nil, fmt.Errorf("date function: invalid format '%s'", format)
},
}
}