-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
62 lines (55 loc) · 1.16 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
package main
import (
_ "embed"
"github.com/fsnotify/fsnotify"
"github.com/gookit/config/v2"
"github.com/gookit/config/v2/yamlv3"
log "github.com/sirupsen/logrus"
"os"
)
func initConfig() {
config.AddDriver(yamlv3.Driver)
if _, err := os.Stat("config.yaml"); os.IsNotExist(err) {
log.Panic("Config file does not exist")
}
if err := config.LoadFiles("config.yaml"); err != nil {
log.Panic("Failed to load config file: ", err)
}
}
func watchConfig() (err error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
defer watcher.Close()
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Has(fsnotify.Write) {
log.Info("Config file changed, reloading...")
err := config.LoadFiles("config.yaml")
if err != nil {
log.Panic("Failed to load config file: ", err)
}
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Error("Failed to listen config file: ", err)
}
}
}()
// Add a path.
err = watcher.Add("config.yaml")
if err != nil {
return err
}
// Block main goroutine forever.
<-make(chan struct{})
return nil
}