-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
262 lines (225 loc) · 6.23 KB
/
config.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package main
import (
"errors"
"fmt"
"io/ioutil"
"strings"
"gopkg.in/yaml.v2"
)
// ConfigSchedule represents a PagerDuty schedule identified by either ID or name.
type ConfigSchedule struct {
ID string `yaml:"id"`
Name string `yaml:"name"`
UserGroups UserGroups `yaml:"userGroups"`
}
func (cs ConfigSchedule) String() string {
return fmt.Sprintf("{ID:%s Name:%q}", cs.ID, cs.Name)
}
type UserGroups []UserGroup
func (ugs UserGroups) find(ug2 UserGroup) *UserGroup {
for _, ug := range ugs {
if (ug2.ID != "" && ug.ID == ug2.ID) ||
(ug2.Name != "" && ug.Name == ug2.Name) ||
(ug2.Handle != "" && ug.Handle == ug2.Handle) {
return &ug
}
}
return nil
}
func (ugs *UserGroups) getOrCreate(ug2 UserGroup) *UserGroup {
if ugs == nil {
}
for _, ug := range *ugs {
if (ug2.ID != "" && ug.ID == ug2.ID) ||
(ug2.Name != "" && ug.Name == ug2.Name) ||
(ug2.Handle != "" && ug.Handle == ug2.Handle) {
return &ug
}
}
return nil
}
type UserGroup struct {
ID string `yaml:"id"`
Name string `yaml:"name"`
Handle string `yaml:"handle"`
}
func (ug UserGroup) String() string {
return fmt.Sprintf("{ID:%s Name:%q Handle:%s}", ug.ID, ug.Name, ug.Handle)
}
// ConfigChannel represents a Slack channel identified by either ID or name.
type ConfigChannel struct {
ID string `yaml:"id"`
Name string `yaml:"name"`
}
func (cc ConfigChannel) String() string {
return fmt.Sprintf("{ID:%s Name:%q}", cc.ID, cc.Name)
}
// ConfigSlackSync represents a synchronization between a set of PagerDuty schedules and a Slack channel.
type ConfigSlackSync struct {
Name string `yaml:"name"`
Schedules []ConfigSchedule `yaml:"schedules"`
Channel ConfigChannel `yaml:"channel"`
Template string `yaml:"template"`
PretendUsers bool `yaml:"pretendUsers"`
DryRun bool `yaml:"dryRun"`
}
type config struct {
SlackSyncs []ConfigSlackSync `yaml:"slackSyncs"`
}
func generateConfig(p params) (config, error) {
var (
cfg config
err error
)
if p.config != "" {
cfg, err = readConfigFile(p.config)
if err != nil {
return config{}, err
}
} else {
if p.tmplFile != "" {
b, err := ioutil.ReadFile(p.tmplFile)
if err != nil {
return config{}, err
}
p.tmplString = string(b)
}
cfg, err = singleSlackSync(p)
if err != nil {
return config{}, err
}
}
// Let globally defined parameters override per-sync ones.
if p.pretendUsers != nil {
for i := range cfg.SlackSyncs {
cfg.SlackSyncs[i].PretendUsers = *p.pretendUsers
}
}
if p.dryRun != nil {
for i := range cfg.SlackSyncs {
cfg.SlackSyncs[i].DryRun = *p.dryRun
}
}
err = validateConfig(&cfg)
if err != nil {
return config{}, err
}
return cfg, err
}
func readConfigFile(file string) (config, error) {
content, err := ioutil.ReadFile(file)
if err != nil {
return config{}, err
}
var cfg config
err = yaml.Unmarshal(content, &cfg)
return cfg, err
}
func singleSlackSync(p params) (config, error) {
slackSync := ConfigSlackSync{
Name: "default",
Channel: ConfigChannel{
ID: p.channelID,
Name: p.channelName,
},
Template: p.tmplString,
}
for _, schedule := range p.schedules {
cfgSchedule, err := parseSchedule(schedule)
if err != nil {
return config{}, err
}
slackSync.Schedules = append(slackSync.Schedules, cfgSchedule)
}
return config{
SlackSyncs: []ConfigSlackSync{slackSync},
}, nil
}
func parseSchedule(schedule string) (ConfigSchedule, error) {
kvs := map[string][]string{}
for _, elem := range strings.Split(schedule, ";") {
kv := strings.SplitN(elem, "=", 2)
if len(kv) < 2 {
return ConfigSchedule{}, fmt.Errorf("missing separator on element %q", elem)
}
key := kv[0]
value := kv[1]
kvs[key] = append(kvs[key], value)
}
var id string
if ids := kvs["id"]; len(ids) > 0 {
if len(ids) > 1 {
return ConfigSchedule{}, errors.New(`multiple values for key "id" not allowed`)
}
id = ids[0]
delete(kvs, "id")
}
var name string
if names := kvs["name"]; len(names) > 0 {
if len(names) > 1 {
return ConfigSchedule{}, errors.New(`multiple values for key "name" not allowed`)
}
name = names[0]
delete(kvs, "name")
}
if id != "" && name != "" {
return ConfigSchedule{}, errors.New(`"id" and "name" cannot be specified simultaneously`)
}
cfgSchedule := ConfigSchedule{
ID: id,
Name: name,
}
for _, userGroup := range kvs["userGroup"] {
kv := strings.Split(userGroup, "=")
if len(kv) != 2 {
return ConfigSchedule{}, fmt.Errorf("user group %s does not follow key=value pattern", userGroup)
}
ugKey := kv[0]
ugValue := kv[1]
var ug UserGroup
switch ugKey {
case "id":
ug.ID = ugValue
case "name":
ug.Name = ugValue
case "handle":
ug.Handle = ugValue
default:
return ConfigSchedule{}, fmt.Errorf("user group %s has unexpected key %q", userGroup, ugKey)
}
cfgSchedule.UserGroups = append(cfgSchedule.UserGroups, ug)
}
delete(kvs, "userGroup")
if len(kvs) > 0 {
return ConfigSchedule{}, fmt.Errorf("unsupported key/value pairs left: %s", kvs)
}
return cfgSchedule, nil
}
func validateConfig(cfg *config) error {
foundNames := map[string]bool{}
for _, sync := range cfg.SlackSyncs {
if _, ok := foundNames[sync.Name]; ok {
return fmt.Errorf("slack sync name %q already used", sync.Name)
}
foundNames[sync.Name] = true
for _, cfgSchedule := range sync.Schedules {
if cfgSchedule.ID == "" && cfgSchedule.Name == "" {
return fmt.Errorf("slack sync %q invalid: must specify either schedule ID or schedule name", sync.Name)
}
for _, cfgUserGroup := range cfgSchedule.UserGroups {
if cfgUserGroup.ID == "" && cfgUserGroup.Name == "" && cfgUserGroup.Handle == "" {
return fmt.Errorf("slack sync %q user group %s invalid: must specify either user group ID or user group name or user group handle", sync.Name, cfgUserGroup)
}
}
}
channelGiven := sync.Channel.ID != "" || sync.Channel.Name != ""
if sync.Template != "" {
if !channelGiven {
return fmt.Errorf("slack sync %q invalid: must specify either channel ID or channel name when topic is given", sync.Name)
}
} else if channelGiven {
return fmt.Errorf("slack sync %q invalid: must specify template when either channel ID or channel name is given", sync.Name)
}
}
return nil
}