-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflag_config.go
More file actions
173 lines (155 loc) · 3.4 KB
/
flag_config.go
File metadata and controls
173 lines (155 loc) · 3.4 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package flag
import (
"os"
"strings"
"github.com/spf13/viper"
)
type Config struct {
*viper.Viper
path string
}
// 新的配置文件
func newConfig(path string) Config {
if _, err := os.Stat(path); err != nil {
os.Create(path)
}
var c Config
c.path = path
c.Viper = viper.New()
c.Viper.AddConfigPath(path)
c.Viper.SetConfigFile(path)
config := strings.Split(path, ".")
if len(config) >= 2 {
c.Viper.SetConfigType(config[len(config)-1])
} else {
c.Viper.SetConfigType("toml")
}
c.Viper.ReadInConfig()
return c
}
// 写入配置文件
func (c *Config) WriteToConfig(title, key string, value any) {
var readKey string
if title != "" && key != "" {
readKey = title + "." + key
} else {
if title == "" {
readKey = key
}
if key == "" {
readKey = title
}
}
c.Viper.Set(readKey, value)
c.Viper.WriteConfig()
}
// 读取配置信息为任意类型
func (c *Config) ReadConfigToAny(title, key string) any {
var readKey string
if title != "" && key != "" {
readKey = title + "." + key
} else {
if title == "" {
readKey = key
}
if key == "" {
readKey = title
}
}
return c.Viper.Get(readKey)
}
// 读取配置信息为string类型
func (c *Config) ReadConfigToString(title, key string) string {
var readKey string
if title != "" && key != "" {
readKey = title + "." + key
} else {
if title == "" {
readKey = key
}
if key == "" {
readKey = title
}
}
return c.GetString(readKey)
}
// 写入配置文件
func (c *Config) WriteStringToConfig(title, key, value string) {
c.WriteToConfig(title, key, value)
}
// 读取配置信息为string类型map
func (c *Config) ReadConfigToStringMap(title, key string) map[string]string {
var readKey string
if title != "" && key != "" {
readKey = title + "." + key
} else {
if title == "" {
readKey = key
}
if key == "" {
readKey = title
}
}
return c.Viper.GetStringMapString(readKey)
}
func (c *Config) ReadConfigToKVMap(title, key string) map[string]any {
var readKey string
if title != "" && key != "" {
readKey = title + "." + key
} else {
if title == "" {
readKey = key
}
if key == "" {
readKey = title
}
}
return c.Viper.GetStringMap(readKey)
}
// 写入配置文件
func (c *Config) WriteKVMapToConfig(title, key string, value map[string]any) {
c.WriteToConfig(title, key, value)
}
func (c *Config) WriteStringMapToConfig(title, key string, value map[string]string) {
c.WriteToConfig(title, key, value)
}
// 读取配置信息为string类型map
func (c *Config) ReadConfigToAnySlice(title, key string) []any {
var readKey string
if title != "" && key != "" {
readKey = title + "." + key
} else {
if title == "" {
readKey = key
}
if key == "" {
readKey = title
}
}
data, ok := c.Viper.Get(readKey).([]any)
if ok {
return data
} else {
return []any{c.Viper.Get(readKey)}
}
}
// 写入配置文件
func (c *Config) WriteStringMapsToConfig(title, key string, value []map[string]any) {
c.WriteToConfig(title, key, value)
}
// 删除配置文件的值
func (c *Config) DeleteConfigValue(title, key string) {
c.WriteToConfig(title, key, "")
}
// 获取配置文件名称
func (c *Config) GetConfigPath() string {
return c.Viper.ConfigFileUsed()
}
// 获取配置文件所有Key
func (c *Config) GetAllConfigKeys() []string {
return c.Viper.AllKeys()
}
// 获取配置文件所有Key和Value
func (c *Config) GetAllConfigValues() map[string]any {
return c.Viper.AllSettings()
}