-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
168 lines (156 loc) · 3.5 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
package main
import (
"bufio"
"code.google.com/p/gcfg"
"errors"
"os"
"strings"
)
type Config struct {
stopChan chan chan bool
Logging struct {
Main string
Level string
}
Mediator struct {
Host string
Port uint
Database string
Username string
Password string
Where string
}
Warehouse struct {
Host string
Port uint
Database string
Username string
Password string
}
Realtime struct {
Host string
Port uint
Database string
Username string
Password string
}
Stats struct {
Host string
Port uint
Database string
Username string
Password string
}
}
func (s *Config) WarehouseProvided() bool {
return len(s.Warehouse.Host) != 0
}
func (s *Config) RealtimeProvided() bool {
return len(s.Realtime.Host) != 0
}
func Keys(x map[string]bool) (res []string) {
for k, _ := range x {
res = append(res, k)
}
return
}
func validateConfig(cfg Config) (result Config, err error) {
result = cfg
valid_loglevels := map[string]bool{
"none": true,
"debug": true,
}
if len(cfg.Logging.Main) == 0 {
err = errors.New("Logging: Main logging destination not provided.")
return
} else {
var isDir bool
isDir, err = fileIsDir(cfg.Logging.Main)
if err != nil {
return
}
if !isDir {
err = errors.New("Logging: Main must be a directory.")
return
}
}
if len(cfg.Logging.Level) == 0 {
err = errors.New("Logging: Log level not provided.")
return
} else if !valid_loglevels[cfg.Logging.Level] {
err = errors.New("Logging: Log level not one of: " + strings.Join(Keys(valid_loglevels), ", "))
return
}
if len(cfg.Mediator.Host) == 0 {
err = errors.New("Config: No host provided.")
return
} else if cfg.Mediator.Port == 0 {
err = errors.New("Config: No port provided.")
return
} else if len(cfg.Mediator.Database) == 0 {
err = errors.New("Config: No database provided.")
return
} else if len(cfg.Mediator.Username) == 0 {
err = errors.New("Config: No username provided.")
return
} else if len(cfg.Mediator.Password) == 0 {
err = errors.New("Config: No password provided.")
return
} else if len(cfg.Mediator.Where) == 0 {
err = errors.New("Config: No filter provided.")
return
}
if cfg.WarehouseProvided() {
if cfg.Warehouse.Port == 0 {
err = errors.New("Warehouse: No port provided.")
return
} else if len(cfg.Warehouse.Database) == 0 {
err = errors.New("Warehouse: No database provided.")
return
} else if len(cfg.Warehouse.Username) == 0 {
err = errors.New("Warehouse: No username provided.")
return
} else if len(cfg.Warehouse.Password) == 0 {
err = errors.New("Warehouse: No password provided.")
return
}
}
if cfg.RealtimeProvided() {
if cfg.Realtime.Port == 0 {
err = errors.New("Realtime: No port provided.")
return
} else if len(cfg.Realtime.Database) == 0 {
err = errors.New("Realtime: No database provided.")
return
} else if len(cfg.Realtime.Username) == 0 {
err = errors.New("Realtime: No username provided.")
return
} else if len(cfg.Realtime.Password) == 0 {
err = errors.New("Realtime: No password provided.")
return
}
}
return
}
func getPollerConfig(path string) (cfg Config, err error) {
f, err := os.Open(path)
if err != nil {
return
}
defer f.Close()
info, err := f.Stat()
if err != nil {
return
}
if !info.IsDir() {
file_reader := bufio.NewReader(f)
err = gcfg.ReadInto(&cfg, file_reader)
if err != nil {
return
}
cfg, err = validateConfig(cfg)
} else {
err = errors.New(path + " is not a file.")
}
return
}