-
Notifications
You must be signed in to change notification settings - Fork 25
/
config.go
67 lines (55 loc) · 1.25 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
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"sync"
"github.com/litl/shuttle/client"
"github.com/litl/shuttle/log"
)
func loadConfig() {
for _, cfgPath := range []string{stateConfig, defaultConfig} {
if cfgPath == "" {
continue
}
cfgData, err := ioutil.ReadFile(cfgPath)
if err != nil {
log.Warnln("Error reading config:", err)
continue
}
var cfg client.Config
err = json.Unmarshal(cfgData, &cfg)
if err != nil {
log.Warnln("Config error:", err)
continue
}
log.Debug("Loaded config from:", cfgPath)
if err := Registry.UpdateConfig(cfg); err != nil {
log.Printf("Unable to load config: error: %s", err)
}
}
}
// protects the state config file
var configMutex sync.Mutex
func writeStateConfig() {
configMutex.Lock()
defer configMutex.Unlock()
if stateConfig == "" {
log.Debug("No state file. Not saving changes")
return
}
cfg := marshal(Registry.Config())
if len(cfg) == 0 {
return
}
lastCfg, _ := ioutil.ReadFile(stateConfig)
if bytes.Equal(cfg, lastCfg) {
log.Println("No change in config")
return
}
// We should probably write a temp file and mv for atomic update.
err := ioutil.WriteFile(stateConfig, cfg, 0644)
if err != nil {
log.Println("Error saving config state:", err)
}
}