-
Notifications
You must be signed in to change notification settings - Fork 3
/
struct.go
118 lines (96 loc) · 2.87 KB
/
struct.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
package config
import (
"fmt"
"io/ioutil"
"path/filepath"
"time"
"github.com/fsnotify/fsnotify"
"github.com/inconshreveable/log15"
"gopkg.in/yaml.v2"
"github.com/nsec/askgod/api"
"github.com/nsec/askgod/internal/utils"
)
// Config represents the internal view of the configuration
type Config struct {
*api.Config
logger log15.Logger
handlers []func(*Config)
}
// RegisterHandler makes it possible to register a function to be called on config changes
func (c *Config) RegisterHandler(handler func(*Config)) error {
c.handlers = append(c.handlers, handler)
return nil
}
func parseConfig(configPath string, conf interface{}) error {
// Read the file's content
content, err := ioutil.ReadFile(configPath)
if err != nil {
return fmt.Errorf("Failed to read file content: %v", err)
}
// Parse the yaml file
err = yaml.Unmarshal(content, conf)
if err != nil {
return fmt.Errorf("Failed to parse yaml: %v", err)
}
return nil
}
// ReadConfigFile will return a Config struct from the content of a yaml file
func ReadConfigFile(configPath string, monitor bool, logger log15.Logger) (*Config, error) {
if !utils.PathExists(configPath) {
return nil, fmt.Errorf("The configuration file doesn't exist: %s", configPath)
}
logger.Info("Parsing configuration", log15.Ctx{"path": configPath})
conf := Config{logger: logger}
err := parseConfig(configPath, &conf.Config)
if err != nil {
return nil, err
}
// Watch for configuration changes
if monitor {
logger.Info("Setting up configuration watch", log15.Ctx{"path": configPath})
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, fmt.Errorf("Unable to setup fsnotify: %v", err)
}
err = watcher.Add(filepath.Dir(configPath))
if err != nil {
return nil, fmt.Errorf("Unable to setup fsnotify watch: %v", err)
}
pathDir := filepath.Dir(configPath)
if pathDir == "" {
pathDir = "./"
}
pathBase := filepath.Base(configPath)
go func() {
for {
select {
case ev := <-watcher.Events:
if ev.Name != fmt.Sprintf("%s/%s", pathDir, pathBase) {
continue
}
// Store the old config for comparison
oldData, _ := yaml.Marshal(conf.Config)
// Wait for 1s for ownership changes
time.Sleep(time.Second)
// Parse the new ocnfig
err := parseConfig(configPath, conf.Config)
if err != nil {
logger.Error("Failed to read the new configuration", log15.Ctx{"path": configPath, "error": err})
}
// Check if something changed
newData, _ := yaml.Marshal(conf.Config)
if string(oldData) == string(newData) {
continue
}
logger.Info("Configuration file changed, reloading", log15.Ctx{"path": configPath})
for _, handler := range conf.handlers {
handler(&conf)
}
case err := <-watcher.Errors:
logger.Error("Got bad file notification", log15.Ctx{"error": err})
}
}
}()
}
return &conf, nil
}