-
Notifications
You must be signed in to change notification settings - Fork 152
/
slack.go
65 lines (54 loc) · 1.34 KB
/
slack.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
package slack
import (
"context"
"strconv"
"strings"
"github.com/influxdata/flux/codes"
"github.com/influxdata/flux/internal/errors"
"github.com/influxdata/flux/runtime"
"github.com/influxdata/flux/semantic"
"github.com/influxdata/flux/values"
)
var defaultColors = map[string]struct{}{
"good": {},
"warning": {},
"danger": {},
}
var errColorParse = errors.New(codes.Invalid, "could not parse color string")
func validateColorString(color string) error {
if _, ok := defaultColors[color]; ok {
return nil
}
if strings.HasPrefix(color, "#") {
hex, err := strconv.ParseInt(color[1:], 16, 64)
if err != nil {
return err
}
if hex < 0 || hex > 0xffffff {
return errColorParse
}
return nil
}
return errColorParse
}
var validateColorStringFluxFn = values.NewFunction(
"validateColorString",
runtime.MustLookupBuiltinType("slack", "validateColorString"),
func(ctx context.Context, args values.Object) (values.Value, error) {
v, ok := args.Get("color")
if !ok {
return nil, errors.New(codes.Invalid, "missing argument: color")
}
if v.Type().Nature() == semantic.String {
if err := validateColorString(v.Str()); err != nil {
return nil, err
}
return v, nil
}
return nil, errColorParse
},
false,
)
func init() {
runtime.RegisterPackageValue("slack", "validateColorString", validateColorStringFluxFn)
}