-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinit.go
More file actions
232 lines (209 loc) · 4.48 KB
/
Copy pathinit.go
File metadata and controls
232 lines (209 loc) · 4.48 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
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
package goconfig
import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"sync"
)
//const middle = "========="
var (
SEP = "=" // key 和 value 分隔符
NOTE = "#;" // #开头的为注释
MODEL_START = "[" // 模块开头符号
MODEL_END = "]" // 模块结尾符号
WELL = "#" // 写入的注释
)
type node struct {
key string
value string
note []string
}
type groupLine struct {
group []node // 组的行
note []string // 组注释
name string // 组名
}
// 这个是ini的config
type config struct {
Groups []*groupLine // 组
Lines []*node // 单key
Read []byte // 文件读出来的所有内容
Write []byte // 文件写的所有内容
KeyValue map[string]string // 键值缓存, key value的值 key or group.key
env map[string]string
Filepath string // 配置文件路径
sjson map[string]interface{}
mu *sync.RWMutex
}
// 保存配置的文件
var kvconfig *config
var Deep = 3
var tp typ
type typ int
const (
// INI typ = iota
// JSON
// YAML
)
func (t typ) String() string {
switch t {
case 0:
return "ini"
case 1:
return "json"
case 2:
return "yaml"
default:
return "ini"
}
}
// 读取配置文件到全局变量,并检查重复项, 重载配置文件执行这个函数
func Reload() error {
file := kvconfig.Filepath
if file == "" {
return errors.New("not found config")
}
//判断文件目录是否存在
_, err := os.Stat(kvconfig.Filepath)
if err != nil {
// 不存在就先创建目录
return err
}
// 清空数据
// 检查是否有错
tmp := &config{
Filepath: file,
Lines: make([]*node, 0),
Groups: make([]*groupLine, 0),
KeyValue: make(map[string]string),
env: make(map[string]string),
}
tmp.Read, err = ioutil.ReadFile(file)
if err != nil {
return err
}
// switch tp {
// case JSON:
// if err := tmp.readJson(); err != nil {
// return err
// }
// case YAML:
// if err := tmp.readYaml(); err != nil {
// return err
// }
// default:
if err := tmp.readIni(); err != nil {
return err
}
// }
kvconfig.mu.Lock()
kvconfig.Filepath = tmp.Filepath
kvconfig.Lines = tmp.Lines
kvconfig.Groups = tmp.Groups
kvconfig.KeyValue = tmp.KeyValue
kvconfig.env = tmp.env
kvconfig.mu.Unlock()
// 更新值
return nil
}
func InitConf(path string) error {
fptmp := filepath.Clean(path)
//判断文件目录是否存在
_, err := os.Stat(filepath.Dir(fptmp))
if err != nil {
// 不存在就先创建目录
if err := os.MkdirAll(filepath.Dir(fptmp), 0755); err != nil {
return err
}
}
// 创建文件
if _, err = os.OpenFile(fptmp, os.O_CREATE, 0644); err != nil {
return err
}
kvconfig = &config{
Filepath: fptmp,
Lines: make([]*node, 0),
Groups: make([]*groupLine, 0),
KeyValue: make(map[string]string),
env: make(map[string]string),
mu: &sync.RWMutex{},
}
kvconfig.Read, err = ioutil.ReadFile(fptmp)
if err != nil {
return err
}
// switch t {
// case JSON:
// tp = t
// if err := kvconfig.readJson(); err != nil {
// return err
// }
// case YAML:
// tp = t
// if err := kvconfig.readYaml(); err != nil {
// return err
// }
// default:
if err := kvconfig.readIni(); err != nil {
return err
}
// }
return nil
}
// 从bytes 解析, Reload方法
func InitFromBytes(data []byte) error {
kvconfig = &config{
Lines: make([]*node, 0),
Groups: make([]*groupLine, 0),
KeyValue: make(map[string]string),
env: make(map[string]string),
mu: &sync.RWMutex{},
}
kvconfig.Read = data
// switch t {
// case JSON:
// tp = t
// if err := kvconfig.readJson(); err != nil {
// return err
// }
// case YAML:
// tp = t
// if err := kvconfig.readYaml(); err != nil {
// return err
// }
// default:
if err := kvconfig.readIni(); err != nil {
return err
}
// }
return nil
}
// 初始化写入文件的方法, 会清空内容
func InitWriteConf(configpath string) {
fptmp := filepath.Clean(configpath)
//判断文件目录是否存在
_, err := os.Stat(filepath.Dir(fptmp))
if err != nil {
// 不存在就先创建目录
if err := os.MkdirAll(filepath.Dir(fptmp), 0755); err != nil {
panic(err)
}
}
os.Remove(fptmp)
// switch t {
// case JSON:
// tp = t
// case YAML:
// tp = t
// default:
// }
kvconfig = &config{
Filepath: configpath,
Lines: make([]*node, 0),
Groups: make([]*groupLine, 0),
KeyValue: make(map[string]string),
env: make(map[string]string),
mu: &sync.RWMutex{},
}
}