forked from gaochao1/swcollector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hostcfg.go
53 lines (39 loc) · 879 Bytes
/
hostcfg.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
package g
import (
"encoding/json"
"log"
"sync"
"github.com/toolkits/file"
)
type HostsConfig struct {
Hosts map[string]string `json:"hosts"`
}
var (
HostConfigFile string
hostconfig *HostsConfig
hostlock = new(sync.RWMutex)
)
func HostConfig() *HostsConfig {
hostlock.RLock()
defer hostlock.RUnlock()
return hostconfig
}
func ParseHostConfig(cfg string) {
if !file.IsExist(cfg) {
log.Fatalln("config file:", cfg, "is not existent")
}
HostConfigFile = cfg
configContent, err := file.ToTrimString(cfg)
if err != nil {
log.Fatalln("read config file:", cfg, "fail:", err)
}
var c HostsConfig
err = json.Unmarshal([]byte(configContent), &c)
if err != nil {
log.Fatalln("parse config file:", cfg, "fail:", err)
}
hostlock.Lock()
defer hostlock.Unlock()
hostconfig = &c
log.Println("read hostconfig file:", cfg, "successfully")
}