forked from zsakvo/dhbooker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getConfig.go
104 lines (94 loc) · 2.33 KB
/
getConfig.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
package main
import (
"fmt"
"os"
"github.com/Unknwon/goconfig"
)
//读取配置文件
func getConfig() {
if isFileExist("conf.ini") {
cfg, err := goconfig.LoadConfigFile("conf.ini")
if err != nil {
os.Remove("conf.ini")
initSettings()
}
config = cfg
} else {
initSettings()
fmt.Println("已初始化 conf.ini 配置文件,请按提示填写后运行本程序")
os.Exit(0)
}
}
//获取 section
func getSection(value string) map[string]string {
account, err := config.GetSection(value)
if err != nil {
initSettings()
}
return account
}
func getAccountSettings() accountSettings {
accountMap := getSection("account")
var aStruct accountSettings
aStruct.username = accountMap["username"]
aStruct.password = accountMap["password"]
if len(aStruct.username) == 0 {
fmt.Println("请于 conf.ini 中填写用户名")
os.Exit(1)
}
if len(aStruct.password) == 0 {
fmt.Println("请于 conf.ini 中填写密码")
os.Exit(1)
}
return aStruct
}
func getPathSettings() {
pathMap := getSection("path")
var aStruct pathSettings
aStruct.tmp = pathMap["tmp"]
aStruct.out = pathMap["out"]
if len(aStruct.tmp) == 0 {
fmt.Println("请于 conf.ini 中填写临时目录路径")
os.Exit(1)
}
if len(aStruct.out) == 0 {
fmt.Println("请于 conf.ini 中填写输出目录路径")
os.Exit(1)
}
path = aStruct
}
func getToken() string {
tokenMap := getSection("token")
return tokenMap["token"]
}
func initSettings() {
os.Create("conf.ini")
cfg, err := goconfig.LoadConfigFile("conf.ini")
check(err)
cfg.SetValue("account", "username", "")
cfg.SetKeyComments("account", "username", "# 用户名,必填")
cfg.SetValue("account", "password", "")
cfg.SetKeyComments("account", "password", "# 密码,必填")
cfg.SetValue("token", "token", "")
cfg.SetKeyComments("token", "token", "# 自动生成,请勿修改")
cfg.SetValue("path", "tmp", "tmp")
cfg.SetKeyComments("path", "tmp", "# 临时目录,必填")
cfg.SetValue("path", "out", "output")
cfg.SetKeyComments("path", "out", "# 输出目录,必填")
err1 := goconfig.SaveConfigFile(cfg, "conf.ini")
check(err1)
}
func setSeparator() {
if ostype == "windows" {
pathSeparator = "\\"
} else if ostype == "linux" {
pathSeparator = "/"
} else if ostype == "darwin" {
pathSeparator = "/"
}
}
func initConfig() {
getConfig()
getPathSettings()
setSeparator()
}