-
Notifications
You must be signed in to change notification settings - Fork 14
/
config.go
536 lines (451 loc) · 12.4 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"strings"
"github.com/fsnotify/fsnotify"
"github.com/goccy/go-yaml"
)
var (
// stores info on files for easier loading
files confFiles
// literally to make sure files load in order
fileLoad = make(chan string)
)
type confFiles struct {
Files []confFile
}
type confFile struct {
Location string // full file path can be a dir or file
Context string // conf, botConf, serverConf
Service string // parkertron, discord, irc, slack
BotName string // variable based on folder name
}
func initConfig(confDir string) (err error) {
if err = loadConfDirs(confDir); err != nil {
return nil
}
Log.Debug(files)
// sort all files to load in the correct order.
files = fileSort()
Log.Debug(files)
// for all files pass it to fsnotify.
Log.Debugf("loading files into file watcher")
for _, file := range files.Files {
go loadNWatch(file)
<-fileLoad
}
Log.Debugf("Files that were loaded.")
for _, v := range files.Files {
Log.Debugf("%+v", v)
}
return nil
}
func fileSort() (newFiles confFiles) {
// sort bot config
for _, file := range files.Files {
if file.Context == "conf" {
newFiles.Files = append(newFiles.Files, file)
}
}
// sort server config
for _, file := range files.Files {
if file.Context == "botConf" {
newFiles.Files = append(newFiles.Files, file)
}
}
// sort channel config
for _, file := range files.Files {
if file.Context == "serverConf" {
newFiles.Files = append(newFiles.Files, file)
}
}
return
}
func loadConfDirs(confdir string) (err error) {
cleanConfDir := path.Clean(confdir)
// Log.Debugf("reading from %s", cleanConfDir)
confFullPath, err := filepath.Abs(cleanConfDir)
if err != nil {
Log.Errorf("error converting path %s\n", err)
}
// walk config dir supplied in startup single dir deep.
err = filepath.Walk(confDir, func(fpath string, info os.FileInfo, err error) error {
// if there are errors log the error
if err != nil {
Log.Infof("prevent panic by handling failure accessing a path %q: %+v\n", fpath, err)
return err
}
// Log.Debugf("walking '%s'", fpath)
// if an object has example in the name skip it
if strings.Contains(info.Name(), "example") || strings.HasPrefix(info.Name(), ".") {
return nil
}
fileFullPath, err := filepath.Abs(fpath)
if err != nil {
Log.Errorf("error converting path %s\n", err)
}
// Log.Debugf("passing '%s' to depthCounter", fpath)
depth, err := depthCounter(confFullPath, fileFullPath)
if err != nil {
return err
}
// don't add to the file struct if it's a folder
if info.IsDir() {
return nil
}
// Log.Debugf(fpath)
if len(strings.Split(fpath, "/")) >= 4 {
// Log.Debug(strings.Split(fpath, "/")[2])
files.Files = append(files.Files, confFile{fileFullPath, getFileType(depth), getFileService(fileFullPath), strings.Split(fpath, "/")[2]})
} else {
files.Files = append(files.Files, confFile{fileFullPath, getFileType(depth), getFileService(fileFullPath), ""})
}
return nil
})
if err != nil {
return err
}
return nil
}
func depthCounter(confFullPath, fileFullPath string) (depth int, err error) {
// Log.Debugf("checking on file %s\n", fileFullPath)
// get depth of folder/file
for depth := 1; depth <= 4; depth++ {
// Log.Debugf("checking on %d", depth)
// each /* is another file depth.
match, err := path.Match(confFullPath+strings.Repeat("/*", depth), fileFullPath)
if err != nil {
return 0, err
}
if match {
// Log.Debugf("Match on depth %d", depth)
return depth, nil
}
}
return 0, nil
}
func getFileType(depth int) (fileType string) {
switch depth {
case 1:
fileType = "conf"
case 3:
fileType = "botConf"
case 4:
fileType = "serverConf"
}
return fileType
}
func getFileService(filePath string) (service string) {
if strings.Contains(filePath, "discord") {
service = "discord"
} else if strings.Contains(filePath, "irc") {
service = "irc"
} else if strings.Contains(filePath, "slack") {
service = "slack"
} else {
service = "parkertron"
}
return service
}
func loadNWatch(file confFile) {
Log.Debugf("Loading file")
if err := loadConf(file); err != nil {
Log.Error(err)
}
Log.Debugf("Setting up watcher")
watcher, err := fsnotify.NewWatcher()
if err != nil {
Log.Errorf("%+v", err)
return
}
Log.Debugf("defer closing watcher")
defer watcher.Close()
done := make(chan bool)
go func() {
for {
select {
case event := <-watcher.Events:
switch event.Op {
case fsnotify.Write:
Log.Infof("file changed: %s", event.Name)
if err := loadConf(file); err != nil {
Log.Errorf("%+v", err)
}
}
case err := <-watcher.Errors:
Log.Errorf("%+v", err)
}
}
}()
if err := watcher.Add(file.Location); err != nil {
Log.Error(err)
}
fileLoad <- ""
<-done
}
func loadConf(conf confFile) (err error) {
switch conf.Service {
case "parkertron":
if err = loadFromFile(conf.Location, &botConfig); err != nil {
Log.Error()
}
// discord conf loading
case "discord":
if conf.Context == "botConf" {
Log.Debugf("loading config for discord bot %s", conf.BotName)
// set up new temp var for the bot
var tempBot discordBot
tempBot.BotName = conf.BotName
if err = loadFromFile(conf.Location, &tempBot); err != nil {
Log.Errorf("there was an error loading configs")
Log.Error(err)
return
}
for bid, bot := range discordGlobal.Bots {
if bot.BotName == conf.BotName {
discordGlobal.Bots[bid].Config.Game = tempBot.Config.Game
discordGlobal.Bots[bid].Config.DMResp = tempBot.Config.DMResp
return
}
}
// add bot to discord bots array.
discordGlobal.Bots = append(discordGlobal.Bots, tempBot)
} else if conf.Context == "serverConf" {
Log.Debugf("loading discord server config for %s", conf.BotName)
// set up new temp var for the server
var tempServer discordServer
if err = loadFromFile(conf.Location, &tempServer); err != nil {
Log.Error(err)
return
}
for i := range tempServer.ChanGroups {
Log.Infof("channel groups %+v", tempServer.ChanGroups[i].ChannelIDs)
}
for bid, bot := range discordGlobal.Bots {
if bot.BotName == conf.BotName {
for sid, server := range discordGlobal.Bots[bid].Servers {
// if the server exists drop it and re-append config
if server.ServerID == tempServer.ServerID {
discordGlobal.Bots[bid].Servers[sid].ChanGroups = tempServer.ChanGroups
discordGlobal.Bots[bid].Servers[sid].Config = tempServer.Config
discordGlobal.Bots[bid].Servers[sid].Permissions = tempServer.Permissions
return
}
}
// if the server isn't in the list append it.
discordGlobal.Bots[bid].Servers = append(discordGlobal.Bots[bid].Servers, tempServer)
Log.Debugf("loaded server %s for bot %s", tempServer.ServerID, bot.BotName)
}
}
}
// irc conf loading
case "irc":
if conf.Context == "botConf" {
Log.Debugf("loading config for irc bot %s", conf.BotName)
var tempBot ircBot
tempBot.BotName = conf.BotName
// if there is an error loading the config return
if err = loadFromFile(conf.Location, &tempBot); err != nil {
return
}
// add bot to irc bots array
ircGlobal.Bots = append(ircGlobal.Bots, tempBot)
}
// slack config loading
case "slack":
}
return nil
}
// LoadConfig loads configs from a file to an interface
func loadFromFile(file string, iface interface{}) (err error) {
if strings.HasSuffix(file, ".json") { // if json file
Log.Debug("loading json file")
if err = readJSONFromFile(file, iface); err != nil {
Log.Error(err)
return
}
} else if strings.HasSuffix(file, ".yml") || strings.HasSuffix(file, ".yaml") { // if yaml file
Log.Debugf("loading yaml file %s", file)
if err = readYamlFromFile(file, iface); err != nil {
Log.Error(err)
return
}
// Log.Debugf("interface %+v", iface)
} else {
return errors.New("no supported file type located")
}
return nil
}
// SaveConfig saves interfaces to a file
func saveConfig(file string, iface interface{}) error {
// Log.Printf("converting struct data to bytesfor %s", file)
bytes, err := json.MarshalIndent(iface, "", " ")
if err != nil {
return fmt.Errorf("there was an error converting the user data to json")
}
// Log.Printf("writing bytes to file")
if err := writeJSONToFile(file, bytes); err != nil {
return err
}
return nil
}
// File management
func writeJSONToFile(file string, iface interface{}) (err error) {
jdata, err := json.MarshalIndent(iface, "", " ")
if err != nil {
return
}
// create a file with a supplied name
if jsonFile, err := os.Create(file); err != nil {
return err
} else if _, err = jsonFile.Write(jdata); err != nil {
return err
}
return nil
}
func readJSONFromFile(file string, iface interface{}) error {
// Log.Printf("opening json file\n")
jsonFile, err := os.Open(file)
// if we os.Open returns an error then handle it
if err != nil {
return err
}
// Log.Printf("holding file open\n")
// defer the closing of our jsonFile so that we can parse it later on
defer func() {
if err := jsonFile.Close(); err != nil {
Log.Printf("Error while closing JSON file: %+v", err)
}
}()
// Log.Printf("reading file\n")
// read our opened xmlFile as a byte array.
byteValue, _ := ioutil.ReadAll(jsonFile)
if err = json.Unmarshal(byteValue, iface); err != nil {
return err
}
// return the json byte value.
return nil
}
func writeYamlToFile(file string, iface interface{}) (err error) {
ydata, err := yaml.Marshal(iface)
if err != nil {
return
}
// create a file with a supplied name
yamlFile, err := os.Create(file)
if err != nil {
return
}
if _, err = yamlFile.Write(ydata); err != nil {
return
}
return
}
func readYamlFromFile(file string, iface interface{}) (err error) {
// Log.Debugf("opening yaml file\n")
yamlFile, err := os.Open(file)
if err != nil {
return
}
// Log.Debugf("holding file open\n")
// defer the closing of our jsonFile so that we can parse it later on
defer func() {
if err := yamlFile.Close(); err != nil {
return
}
}()
// Log.Printf("reading file\n")
byteValue, _ := ioutil.ReadAll(yamlFile)
if err = yaml.Unmarshal(byteValue, iface); err != nil {
return
}
return
}
// Exists reports whether the named file or directory exists.
func createIfDoesntExist(name string) (err error) {
p, file := path.Split(name)
// if confdir exists carry on
if _, err := os.Stat(name); err != nil {
// if file doesn't exist
if os.IsNotExist(err) {
// stat
if _, err = os.Stat(name); err != nil {
if file == "" {
if err = os.Mkdir(p, 0755); err != nil {
}
} else {
if fileCheck, err := os.OpenFile(name, os.O_RDONLY|os.O_CREATE, 0644); err != nil {
} else {
if err := fileCheck.Close(); err != nil {
return err
}
}
}
}
}
}
return
}
func loadInitConfig(confDir, conf, verbose string) (botConfig parkertron, err error) {
// All of this is pre-Logrus init
if verbose == "debug" {
log.Printf("Checking for dir at %s", confDir)
}
// if the config dir doesn't exist make it
if err := createIfDoesntExist(confDir); err != nil {
return botConfig, err
// if we can't make a confdir log a fatal error.
}
// if confdir exists carry on
info, err := os.Stat(confDir)
if err != nil {
return botConfig, err
}
// if not a directory error out
if !info.IsDir() {
return botConfig, errors.New("given file not directory")
}
if verbose == "debug" {
log.Printf("loading initial bot config at %s%s", confDir, conf)
}
// if config doesn't exist make it.
if err = createIfDoesntExist(confDir + conf); err != nil {
if verbose == "debug" {
log.Printf("creating config %s", confDir+conf)
if err := createExampleBotConfig(confDir, conf, verbose); err != nil {
return parkertron{}, err
}
}
}
// if conf file exists carry on
if verbose == "debug" {
log.Printf("file %s%s exists", confDir, conf)
}
// if confdir exists carry on
file, err := os.Stat(confDir + conf)
if err != nil {
return botConfig, err
}
if file.Size() == 0 {
if err := createExampleBotConfig(confDir, conf, verbose); err != nil {
return parkertron{}, err
}
}
if strings.HasSuffix(conf, "yaml") || strings.HasSuffix(conf, "yml") {
if err = readYamlFromFile(confDir+conf, &botConfig); err != nil {
return
}
} else if strings.HasSuffix(conf, "json") {
if err = readJSONFromFile(confDir+conf, &botConfig); err != nil {
return botConfig, err
}
}
return
}