-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
190 lines (170 loc) · 3.98 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
package core
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"time"
"github.com/fsnotify/fsnotify"
"github.com/hexbotio/hex/models"
"github.com/hexbotio/hex/parse"
)
func Config(config *models.Config) {
locateConfigFile(config)
if checkConfig(config) {
go watchConfig(config)
readConfig(config)
subConfig(config)
configRules(config)
if config.Validate {
fmt.Println("SUCCESS - Config file is valid: " + config.ConfigFile)
os.Exit(0)
}
} else {
os.Exit(1)
}
config.StartTime = time.Now().Unix()
}
func locateConfigFile(config *models.Config) {
locations := []string{
os.Getenv("HEX_CONFIG"),
config.ConfigFile,
"/etc/hex.json",
"/etc/hex/hex.json",
}
for _, location := range locations {
if FileExists(location) {
config.ConfigFile = location
return
}
}
}
func watchConfig(config *models.Config) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Print(err)
}
defer watcher.Close()
done := make(chan bool)
go func() {
for {
select {
case event := <-watcher.Events:
if event.Op&fsnotify.Write == fsnotify.Write {
readConfig(config)
subConfig(config)
configRules(config)
log.Print("Config Reloaded: ", event.Name)
}
case err := <-watcher.Errors:
log.Print("CONFIG ERROR:", err)
}
}
}()
err = watcher.Add(config.ConfigFile)
if err != nil {
log.Print(err)
}
<-done
}
func readConfig(config *models.Config) {
file, err := ioutil.ReadFile(config.ConfigFile)
if err != nil {
log.Print(err)
}
err = json.Unmarshal(file, &config)
if err != nil {
log.Print(err)
}
}
func subConfig(config *models.Config) {
// handle bot name
if os.Getenv("HEX_BOT_NAME") != "" {
config.BotName = os.Getenv("HEX_BOT_NAME")
} else if config.BotName == "" {
config.BotName = "hex"
}
if strings.HasPrefix(config.BotName, "@") {
config.BotName = strings.Replace(config.BotName, "@", "", -1)
}
// handle workspace
if os.Getenv("HEX_WORKSPACE") != "" {
config.BotName = os.Getenv("HEX_WORKSPACE")
} else if config.Workspace == "" {
config.Workspace = "/tmp"
}
if !strings.HasSuffix(config.Workspace, "/") {
config.Workspace = config.Workspace + "/"
}
if _, err := os.Stat(config.Workspace); os.IsNotExist(err) {
fmt.Println("ERROR - Workspace directory is invalid: " + config.Workspace)
os.Exit(1)
}
// handle debug
if os.Getenv("HEX_DEBUG") != "" {
if strings.ToLower(os.Getenv("HEX_DEBUG")) == "true" || config.Debug {
config.Debug = true
} else {
config.Debug = false
}
}
for i := 0; i < len(config.Services); i++ {
for k, v := range config.Services[i].Config {
config.Services[i].Config[k] = parse.SubstituteEnv(v)
}
config.Services[i].BotName = config.BotName
}
// handle logfile
if os.Getenv("HEX_LOGFILE") != "" {
config.LogFile = os.Getenv("HEX_LOGFILE")
}
}
func configRules(config *models.Config) {
// check for service name uniqueness
serviceChk := make(map[string]bool)
for _, service := range config.Services {
serviceChk[service.Name] = true
}
if len(config.Services) > len(serviceChk) {
log.Print("ERROR - Duplicate Service Names exist")
os.Exit(1)
}
// check for pipeline name uniqueness
pipelineChk := make(map[string]bool)
for _, pipeline := range config.Pipelines {
pipelineChk[pipeline.Name] = true
}
if len(config.Pipelines) > len(pipelineChk) {
log.Print("ERROR - Duplicate Pipeline Names exist")
os.Exit(1)
}
}
func checkConfig(config *models.Config) (exists bool) {
exists = true
if _, err := os.Stat(config.ConfigFile); os.IsNotExist(err) {
fmt.Println("Error - Missing a config file")
exists = false
}
if exists {
file, err := ioutil.ReadFile(config.ConfigFile)
if err != nil {
log.Print(err)
}
var js interface{}
exists = json.Unmarshal(file, &js) == nil
if !exists {
fmt.Println("Error - Config file does not appear to be valid json")
}
}
return exists
}
func FileExists(file string) bool {
if _, err := os.Stat(file); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}