This repository has been archived by the owner on Jul 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
248 lines (197 loc) · 4.79 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package config
import (
"github.com/go-ini/ini"
"os"
"path"
"path/filepath"
"strings"
"sync"
)
//config format error
type FormatError struct {
message string
//err error
}
func (this *FormatError) Error() string {
return this.message
}
type Configurator interface {
Get(keys string) (interface{}, error)
Set(key string, value string) error
All() map[string]*ini.File
}
var (
config *Config
//mu sync.Mutex
once sync.Once
)
type Config struct {
directory string
configs map[string]*ini.File
delimiter string
extension string
}
// 构造函数
func NewConfig(directory string) (*Config, error) {
// singleton
if config != nil {
return config, nil
}
directory, err := absPath(directory)
if err != nil {
return nil, err
}
// 单例加锁
//mu.Lock()
//defer mu.Unlock()
//直接使用once,其实就是调用mu.Lock和Unlock
once.Do(func() {
config = &Config{directory: directory, delimiter: `.`, extension: `.conf`}
})
// loadAll
err = config.loadAll()
if err != nil {
return nil, err
}
return config, nil
}
// 获取指定目录的绝对路径
func absPath(directory string) (string, error) {
return filepath.Abs(directory)
}
// 获取指定key配置
func (this *Config) Get(keys string) (interface{}, error) {
keySlices := parseKey(keys, this.delimiter)
length := len(keySlices)
var cfg *ini.File
if _, ok := this.configs[keySlices[0]]; !ok {
return nil, &FormatError{message: `index error`}
} else {
cfg = this.configs[keySlices[0]]
}
if length == 1 {
return cfg, nil
} else if length == 2 {
if !cfg.Section(ini.DefaultSection).HasKey(keySlices[1]) {
return nil, &FormatError{message: `value not found`}
}
return cfg.Section(ini.DefaultSection).GetKey(keySlices[1])
} else {
// 取得可能是section的slice拼接
sectionName := strings.Join(keySlices[1:length-1], this.delimiter)
section, err := cfg.GetSection(sectionName)
// 如果section不存在拼接全部的
if err != nil {
sectionName = sectionName + `.` + keySlices[length-1:][0]
section, err = cfg.GetSection(sectionName)
if err != nil {
return nil, err
}
return section, nil
}
// 否则最后一位肯定是key
key := keySlices[length-1:][0]
if section.HasKey(key) {
return section.GetKey(key)
}
return nil, &FormatError{message: `value not found`}
}
}
func (this *Config) GetDefault(keys string, defaults ...interface{}) interface{} {
defaultValue := ``
if len(defaults) >= 1 {
defaultValue = defaults[0].(string)
}
value, err := this.Get(keys)
if err != nil {
return defaultValue
}
return value
}
// 设置配置
func (this *Config) Set(keys string, value string) error {
keySlices := parseKey(keys, this.delimiter)
length := len(keySlices)
if length == 1 {
return &FormatError{message: "incorrect parameter format"}
}
var err error
if _, ok := this.configs[keySlices[0]]; !ok {
this.configs[keySlices[0]], err = loadConf(this.fullPath(keySlices[0]))
if err != nil {
return err
}
}
if length == 2 {
_, err = this.configs[keySlices[0]].Section(ini.DefaultSection).NewKey(keySlices[1], value)
} else {
_, err = this.configs[keySlices[0]].Section(strings.Join(keySlices[1:length-1], this.delimiter)).NewKey(keySlices[length-1], value)
}
if err != nil {
return err
}
file, err := os.OpenFile(this.fullPath(keySlices[0]), os.O_WRONLY, 0666)
if err != nil {
return err
}
defer file.Close()
_, err = this.configs[keySlices[0]].WriteTo(file)
if err != nil {
return err
}
return nil
}
// 获取所有配置
func (this *Config) All() map[string]*ini.File {
return this.configs
}
// 一次加载所有配置文件
func (this *Config) loadAll() error {
// init File map
this.configs = make(map[string]*ini.File)
err := filepath.Walk(this.directory, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
filename := filepath.Base(path)
if strings.Contains(filename, this.extension) && !info.IsDir() {
fileKey := strings.Replace(filename, this.extension, "", 1)
cfg, err := loadConf(path)
if err != nil {
return err
}
this.configs[fileKey] = cfg
}
return nil
})
if err != nil {
return err
}
return nil
}
// 通过文件名(不包含扩展名),得到当前的完整路径
func (this *Config) fullPath(filename string) string {
return path.Clean(this.directory + "/" + filename + this.extension)
}
// 加载配置文件
func loadConf(filename string) (*ini.File, error) {
_, err := os.Stat(filename)
if err != nil {
if os.IsNotExist(err) {
file, err := os.Create(filename)
if err != nil {
return nil, err
}
defer file.Close()
}
}
cfg, err := ini.Load(filename)
if err != nil {
return nil, err
}
return cfg, nil
}
// 解析数据key
func parseKey(key string, delimiter string) []string {
return strings.Split(key, delimiter)
}