-
Notifications
You must be signed in to change notification settings - Fork 123
/
conf.go
63 lines (59 loc) · 1.61 KB
/
conf.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
package main
import (
"github.com/spf13/viper"
)
type FetchConf struct {
Lang string
Client struct {
Interval int
Method string
SelectOrigin string
CustomUrl string
AutoFetch bool
}
Server struct {
Interval int
Port int
}
}
func (f *FetchConf) Storage() {
viper.Set("lang", f.Lang)
viper.Set("client.interval", f.Client.Interval)
viper.Set("client.method", f.Client.Method)
viper.Set("client.selectorigin", f.Client.SelectOrigin)
viper.Set("client.customurl", f.Client.CustomUrl)
viper.Set("client.autofetch", f.Client.AutoFetch)
viper.Set("server.interval", f.Server.Interval)
viper.Set("server.port", f.Server.Port)
if err := viper.WriteConfigAs("conf.yaml"); err != nil {
_fileLog.Print("持久化配置信息失败:" + err.Error())
}
}
func LoadFetchConf() *FetchConf {
viper.AddConfigPath(AppExecDir())
viper.SetConfigName("conf")
viper.SetConfigType("yaml")
viper.SetDefault("lang", "zh-CN")
viper.SetDefault("client.interval", 60)
viper.SetDefault("client.method", "官方指定hosts源")
viper.SetDefault("client.selectorigin", "FetchGithubHosts")
viper.SetDefault("client.autofetch", false)
viper.SetDefault("server.interval", 60)
viper.SetDefault("server.port", 9898)
var fileNotExits bool
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
fileNotExits = true
} else {
_fileLog.Print("加载配置文件错误: " + err.Error())
}
}
res := FetchConf{}
if err := viper.Unmarshal(&res); err != nil {
_fileLog.Print("配置文件解析失败")
}
if fileNotExits {
res.Storage()
}
return &res
}