forked from litl/shuttle
/
config.go
66 lines (55 loc) · 1.29 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
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"sync"
"github.com/skyfii/shuttle/client"
"github.com/skyfii/shuttle/log"
)
func loadConfig() {
for _, cfgPath := range []string{stateConfig, defaultConfig} {
if cfgPath == "" {
continue
}
cfgData, err := ioutil.ReadFile(cfgPath)
if err != nil {
log.Warnln("WARN: Reading config ", err)
continue
}
var cfg client.Config
err = json.Unmarshal(cfgData, &cfg)
if err != nil {
log.Warnln("WARN: Config error:", err)
continue
}
log.Debug("DEBUG: Loaded config from:", cfgPath)
if err := Registry.UpdateConfig(cfg); err != nil {
log.Errorf("ERROR: Unable to load config: %s", err)
}
}
}
// protects the state config file
var configMutex sync.Mutex
func writeStateConfig() {
configMutex.Lock()
defer configMutex.Unlock()
if stateConfig == "" {
log.Debug("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("INFO: 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.Errorln("ERROR: Can't save config state:", err)
}
}