-
Notifications
You must be signed in to change notification settings - Fork 78
/
notify.go
73 lines (61 loc) · 1.76 KB
/
notify.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
package notify
import (
"errors"
"fmt"
"sort"
"strings"
"github.com/ovh/utask"
"github.com/ovh/utask/pkg/notify"
"github.com/ovh/utask/pkg/plugins/taskplugin"
)
// the notify plugin broadcasts a message over all registered notification senders
var (
Plugin = taskplugin.New("notify", "0.1", exec,
taskplugin.WithConfig(validConfig, Config{}))
)
// Config is the configuration needed to send a notification
// consisting of a message and extra fields
// implements notify.Payload
type Config struct {
Msg string `json:"message"`
Flds map[string]string `json:"fields"`
Backends []string `json:"backends"`
}
// Message returns the config's message
func (nc *Config) Message() *notify.Message {
return ¬ify.Message{MainMessage: nc.Msg, Fields: nc.Flds}
}
func validConfig(config interface{}) error {
cfg := config.(*Config)
if len(cfg.Backends) == 0 {
// if no backends defined, implies that all backends will be contacted
return nil
}
snames := notify.ListSendersNames()
// The slice must be sorted in ascending order
// From https://golang.org/pkg/sort/#SearchStrings
sort.Strings(snames)
if len(snames) == 0 {
return errors.New("registered notify senders list is empty")
}
for _, backend := range cfg.Backends {
i := sort.SearchStrings(snames, backend)
if i >= len(snames) && snames[i] != backend {
return fmt.Errorf(
"can't find backend name: %s. Available backends: %s",
backend,
strings.Join(snames, ", "))
}
}
return nil
}
func exec(stepName string, config interface{}, ctx interface{}) (interface{}, interface{}, error) {
cfg := config.(*Config)
notify.Send(
cfg.Message(),
utask.NotifyActionsParameters{
Disabled: false,
NotifyBackends: cfg.Backends,
})
return nil, nil, nil
}