-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.go
76 lines (64 loc) · 1.99 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
package main
import (
"gopkg.in/BlueDragonX/go-settings.v1"
)
func ConfigTemplates(configs []*settings.Settings) []Template {
templates := make([]Template, len(configs))
for n, config := range configs {
src := config.StringDflt("src", "")
dest := config.StringDflt("dest", "")
if src == "" {
logger.Fatalf("config '%s.src' is missing", config.Key)
}
if dest == "" {
logger.Fatalf("config '%s.dest' is missing", config.Key)
}
templates[n] = Template{Src: src, Dest: dest}
}
return templates
}
func ConfigSentinel(config *settings.Settings) *Sentinel {
client, err := NewEtcdClient(config.ObjectDflt("etcd", &settings.Settings{}))
if err != nil {
logger.Fatalf("failed to create client: %s", err)
}
sentinel := Sentinel{Client: client}
watchers, err := config.ObjectMap("watchers")
if err != nil {
logger.Fatal("config 'watchers' is invalid")
}
if len(watchers) == 0 {
logger.Fatal("config 'watchers' is missing")
}
for name, watcher := range watchers {
prefix := CleanPath(watcher.StringDflt("prefix", ""))
watch := ResolvePaths(prefix, watcher.StringArrayDflt("watch", []string{}))
context := ResolvePaths(prefix, watcher.StringArrayDflt("context", []string{}))
var templates []Template
templatesConfig, err := watcher.ObjectArray("templates")
if err != settings.KeyError {
if err != nil {
logger.Fatalf("config '%s.templates' is invalid", watcher.Key)
}
templates = ConfigTemplates(templatesConfig)
}
var command []string
if cmdStr, err := watcher.String("command"); err == nil {
command = []string{"bash", "-c", cmdStr}
} else if cmdArray, err := watcher.StringArray("command"); err == nil {
command = cmdArray
}
if len(templates) == 0 && len(command) == 0 {
logger.Fatalf("watcher %s templates and command both missing", name)
}
executor := &TemplateExecutor{
name: name,
prefix: prefix,
context: context,
Templates: templates,
Command: command,
}
sentinel.Add(watch, executor)
}
return &sentinel
}