-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
339 lines (285 loc) · 6.74 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package config
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"os"
"strings"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
const (
VCAP_SERVICES = "VCAP_SERVICES"
VCAP_APPLICATION = "VCAP_APPLICATION"
)
type Config struct {
Targets Targets `yaml:"targets"`
Listen string `yaml:"listen"`
Log *Log `yaml:"log"`
Components Components `yaml:"components"`
BaseInfo *BaseInfo `yaml:"base_info"`
Username string `yaml:"username"`
Password string `yaml:"password"`
CookieKey string `yaml:"cookie_key"`
Notifiers []Notifier `yaml:"notifiers"`
Theme *Theme `yaml:"theme"`
}
func (c *Config) Merge(other Config) {
c.Targets = append(c.Targets, other.Targets...)
c.Notifiers = append(c.Notifiers, other.Notifiers...)
c.Components = append(c.Components, other.Components...)
if len(c.Listen) == 0 {
c.Listen = other.Listen
}
if len(c.Username) == 0 || len(c.Password) == 0 {
c.Username = other.Username
c.Password = other.Password
}
if len(c.CookieKey) == 0 {
c.CookieKey = other.CookieKey
}
if c.Theme == nil {
c.Theme = other.Theme
}
if c.BaseInfo == nil {
c.BaseInfo = other.BaseInfo
}
if c.Log == nil {
c.Log = other.Log
}
}
func (c *Config) Validate() error {
if len(c.Components) == 0 {
return fmt.Errorf("At least one component must be define")
}
if c.Username == "" {
c.Username = uuid.NewString()
log.Infof("Generated username (set username in config): %s", c.Username)
}
if c.Password == "" {
c.Password = uuid.NewString()
log.Infof("Generated password (set password in config): %s", c.Password)
}
host := "0.0.0.0"
port := "8080"
splitListen := strings.Split(c.Listen, ":")
if splitListen[0] != "" {
host = splitListen[0]
}
if len(splitListen) == 2 {
port = splitListen[1]
}
envPort := os.Getenv("PORT")
if envPort != "" {
port = envPort
}
c.Listen = host + ":" + port
if c.CookieKey == "" {
c.CookieKey = uuid.NewString()
}
if c.Theme == nil {
c.Theme = &Theme{}
}
if err := c.Theme.Validate(); err != nil {
return nil
}
if c.BaseInfo == nil {
c.BaseInfo = &BaseInfo{}
}
if err := c.BaseInfo.Validate(c.Listen); err != nil {
return nil
}
if c.Log == nil {
c.Log = &Log{}
}
if err := c.Log.Validate(); err != nil {
return err
}
if len(c.Targets) == 0 {
return fmt.Errorf("At least one target must be define")
}
if err := c.Targets.Validate(); err != nil {
return err
}
return nil
}
type Target string
func (t Target) Validate() (*url.URL, error) {
log.Debugf("url: %s", string(t))
u, err := url.Parse(string(t))
if err != nil {
return nil, err
}
log.Debugf("-> value: %+v", u)
return u, nil
}
type Targets []Target
func (t Targets) Validate() error {
for _, v := range t {
if _, err := v.Validate(); err != nil {
return err
}
}
return nil
}
type Log struct {
Level string `yaml:"level"`
NoColor bool `yaml:"no_color"`
InJson bool `yaml:"in_json"`
}
func (c *Log) Validate() error {
log.SetFormatter(&log.TextFormatter{
DisableColors: c.NoColor,
})
if c.Level != "" {
lvl, err := log.ParseLevel(c.Level)
if err != nil {
return err
}
log.SetLevel(lvl)
}
if c.InJson {
log.SetFormatter(&log.JSONFormatter{})
}
return nil
}
type BaseInfo struct {
BaseURL string `yaml:"base_url"`
Support string `yaml:"support"`
Contact string `yaml:"contact"`
Title string `yaml:"title"`
TimeZone string `yaml:"time_zone"`
}
func (b *BaseInfo) Validate(listen string) error {
b.BaseURL = strings.TrimSuffix(b.BaseURL, "/")
if b.Title == "" {
b.Title = "Statusetat"
}
if b.BaseURL == "" {
b.BaseURL = "http://" + listen
}
if b.Support == "" {
b.Support = b.BaseURL
}
if b.Contact == "" {
b.Contact = b.Support
}
if b.TimeZone == "" {
b.TimeZone = "UTC"
}
return nil
}
type Notifier struct {
For ForComponent `yaml:"for"`
Type string `yaml:"type"`
Params map[string]interface{} `yaml:"params"`
}
type Theme struct {
PreStatus string `yaml:"pre_status"`
PostStatus string `yaml:"post_status"`
PreTimeline string `yaml:"pre_timeline"`
PostTimeline string `yaml:"post_timeline"`
PreMaintenance string `yaml:"pre_maintenance"`
PostMaintenance string `yaml:"post_maintenance"`
PersistentDisplayName string `yaml:"persistent_display_name"`
PrePersistent string `yaml:"pre_persistent"`
PostPersistent string `yaml:"post_persistent"`
Footer string `yaml:"footer"`
}
func (t *Theme) Validate() error {
if t.PersistentDisplayName == "" {
t.PersistentDisplayName = "persistent incident"
}
return nil
}
type Component struct {
Name string
Description string
Group string
}
type Components []Component
func (cs Components) Regroups() map[string][]Component {
regroups := make(map[string][]Component)
for _, c := range cs {
_, ok := regroups[c.Group]
if ok {
regroups[c.Group] = append(regroups[c.Group], c)
continue
}
regroups[c.Group] = []Component{c}
}
return regroups
}
func (c Component) String() string {
if c.Group == "" {
return c.Name
}
return c.Group + " - " + c.Name
}
func (cs Components) Inline() []string {
components := make([]string, len(cs))
for i, co := range cs {
components[i] = co.String()
}
return components
}
func LoadConfig(content []byte) (Config, error) {
var config Config
err := yaml.Unmarshal(content, &config)
if err != nil {
return Config{}, err
}
return config, nil
}
func LoadConfigFromFile(filename string) (Config, error) {
b, err := ioutil.ReadFile(filename)
if err != nil {
return Config{}, err
}
return LoadConfig(b)
}
func LoadConfigFromEnv() (Config, error) {
type config map[string]interface{}
type configs []config
type vcapservices map[string]configs
var (
services vcapservices
res Config
)
vcap := os.Getenv(VCAP_SERVICES)
if vcap == "" {
return Config{}, fmt.Errorf("empty %s env variable", VCAP_SERVICES)
}
if err := json.Unmarshal([]byte(vcap), &services); err != nil {
return Config{}, fmt.Errorf("invalid json in %s env variable: %s", VCAP_SERVICES, err)
}
for _, cConfigs := range services {
for _, cConfig := range cConfigs {
name, ok := cConfig["name"]
if !ok {
continue
}
nameStr, ok := name.(string)
if !ok {
continue
}
creds, err := json.Marshal(cConfig["credentials"])
if err != nil {
return Config{}, fmt.Errorf("unable to marshal config '%s': %s", nameStr, err)
}
log.Infof("loading vcap service '%s'", nameStr)
c, err := LoadConfig(creds)
if err != nil {
log.Errorf("error while loading vcap service '%s': %s", nameStr, err)
continue
}
res.Merge(c)
}
}
if err := res.Validate(); err != nil {
return Config{}, fmt.Errorf("could not find any valid configuration in %s: %s", VCAP_SERVICES, err)
}
return res, nil
}