-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
398 lines (345 loc) · 9.13 KB
/
Copy pathconfig.go
File metadata and controls
398 lines (345 loc) · 9.13 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/astaxie/beego/logs"
"github.com/lxn/walk"
)
type ApplicationLogConfig struct {
Filename string `json:"filename"`
Level int `json:"level"`
MaxLines int `json:"maxlines"`
MaxSize int `json:"maxsize"`
Daily bool `json:"daily"`
MaxDays int `json:"maxdays"`
Color bool `json:"color"`
}
type ApplicationConfig struct {
Startup bool `json:"startup"`
LastPath string `json:"lastpath"`
LogPath string `json:"logpath"`
LogConfig ApplicationLogConfig `json:"logconfig"`
}
type DataStoreConfig struct {
Enable bool `json:"enable"`
Address string `json:"address"`
Port int `json:"port"`
UserName string `json:"username"`
PassWord string `json:"password"`
DataBase string `json:"database"`
Expired int `json:"expired"`
}
type ClientConfig struct {
Enable bool `json:"enable"`
Timeout int `json:"timeout"`
Name string `json:"name"`
Endpoint string `json:"endpoint"`
Store bool `json:"store"`
NodeList []NodeInfo `json:"nodes"`
}
type ServerNodeInfo struct {
ClientName string `json:"clientName"`
ClientEndpoint string `json:"clientEndpoint"`
ClientNode NodeInfo `json:"clientNode"`
ServerName string `json:"serverName"`
ServerNode NodeInfo `json:"serverNode"`
}
type ServerConfig struct {
Enable bool `json:"enable"`
Name string `json:"name"`
Endpoint string `json:"endpoint"`
Port int `json:"port"`
NodeList []ServerNodeInfo `json:"nodes"`
}
type Config struct {
Filepath string `json:"-"`
Startup bool `json:"startup"`
Clients []ClientConfig `json:"clients"`
Server ServerConfig `json:"server"`
Datastore DataStoreConfig `json:"datastore"`
}
var defaultApplicationConfig = ApplicationConfig{
Startup: false,
LastPath: "",
LogPath: "",
LogConfig: ApplicationLogConfig{
Filename: "runlog.log",
Level: logs.LevelInformational,
Daily: true,
MaxSize: 100 * 1024 * 1024,
MaxLines: 100 * 1024,
MaxDays: 7,
Color: false,
},
}
var defaultConfig = Config{
Filepath: "",
Startup: false,
Clients: make([]ClientConfig, 0),
Server: ServerConfig{Enable: false, Name: "Server", Endpoint: "0.0.0.0", Port: 4840},
Datastore: DataStoreConfig{
Enable: false,
Address: "localhost", Port: 3306,
UserName: "root", PassWord: "root",
DataBase: "opcua", Expired: 30},
}
func (c *Config) statusUpdate() {
body, err := os.ReadFile(c.Filepath)
if err != nil {
logs.Error("read config file from app data dir fail, %s", err.Error())
return
}
value, err := json.Marshal(*c)
if err != nil {
logs.Error("covert config data to json fail, %s", err.Error())
return
}
if string(body) == string(value) {
StatusConfig(c.Filepath)
} else {
StatusConfig(c.Filepath + "*")
}
}
func (c *Config) UpdateStartup(startup bool) {
defer c.statusUpdate()
c.Startup = startup
}
func (c *Config) UpdateDataBase(database DataStoreConfig) {
defer c.statusUpdate()
c.Datastore = database
}
func (c *Config) UpdateServer(server ServerConfig) {
defer c.statusUpdate()
c.Server = server
}
func (c *Config) UpdateClient(clients []ClientConfig) {
defer c.statusUpdate()
c.Clients = clients
}
func (c *Config) Add(client ClientConfig) error {
defer c.statusUpdate()
for _, item := range c.Clients {
if item.Name == client.Name {
return fmt.Errorf("client name %s already exist", client.Name)
}
}
c.Clients = append(c.Clients, client)
return nil
}
func (c *Config) Update(client ClientConfig) error {
defer c.statusUpdate()
for i, item := range c.Clients {
if item.Name == client.Name {
c.Clients[i] = client
return nil
}
}
return fmt.Errorf("client name %s not exist", client.Name)
}
func (c *Config) Delete(name string) {
defer c.statusUpdate()
for i, client := range c.Clients {
if client.Name == name {
c.Clients = append(c.Clients[:i], c.Clients[i+1:]...)
return
}
}
}
func (c *Config) Save() error {
defer c.statusUpdate()
value, err := json.Marshal(*c)
if err != nil {
logs.Error("covert config data to json fail, %s", err.Error())
return err
}
err = os.WriteFile(c.Filepath, value, 0664)
if err != nil {
logs.Error("write config data to file fail, %s", err.Error())
return err
}
logs.Info("config save %s", string(value))
return nil
}
func (c *Config) ClientConfig(name string) ClientConfig {
for _, client := range c.Clients {
if client.Name == name {
return client
}
}
return ClientConfig{}
}
func (c *Config) ClientNames(filters ...string) []string {
names := make([]string, 0)
for _, client := range c.Clients {
if len(filters) == 0 {
names = append(names, client.Name)
} else {
var flag bool
for _, name := range filters {
if client.Name == name {
flag = true
}
}
if !flag {
names = append(names, client.Name)
}
}
}
return names
}
func (c *ServerConfig) Add(name string, endpoint string, node NodeInfo) bool {
serverName := fmt.Sprintf("%s.%s", name, node.NodeID)
for _, node := range c.NodeList {
if node.ServerName == serverName {
return false
}
}
c.NodeList = append(c.NodeList, ServerNodeInfo{
ClientName: name,
ClientEndpoint: endpoint,
ClientNode: node,
ServerName: serverName,
ServerNode: NodeInfo{NsIndex: node.NsIndex, NodeID: serverName},
})
return true
}
func (c *ServerConfig) Update(serverName string, node NodeInfo) bool {
for i, item := range c.NodeList {
if item.ServerName == serverName {
c.NodeList[i].ServerNode = node
return true
}
}
return false
}
func (c *ServerConfig) Delete(serverName string) bool {
for i, node := range c.NodeList {
if node.ServerName == serverName {
c.NodeList = append(c.NodeList[:i], c.NodeList[i+1:]...)
return true
}
}
return false
}
func (c *ServerConfig) Clean() {
c.NodeList = make([]ServerNodeInfo, 0)
logs.Info("server config node list clean")
}
func (c *ClientConfig) Reset(nodes []NodeInfo) {
c.NodeList = nodes
}
func ConfigCreate(filepath string) (*Config, error) {
config := defaultConfig
config.Filepath = filepath
err := config.Save()
if err != nil {
logs.Error("save default config to file fail, %s", err.Error())
return nil, err
}
config.statusUpdate()
return &config, nil
}
func ConfigLoad(filepath string) (*Config, error) {
value, err := os.ReadFile(filepath)
if err != nil {
logs.Error("read config file from app data dir fail, %s", err.Error())
return nil, err
}
logs.Info("config load %s value %s", filepath, string(value))
var config Config
err = json.Unmarshal(value, &config)
if err != nil {
logs.Error("json unmarshal config fail, %s", err.Error())
return nil, err
}
config.Filepath = filepath
config.statusUpdate()
return &config, nil
}
func FileDialogOpen(from walk.Form, prevFilePath string) (string, error) {
dlg := new(walk.FileDialog)
dlg.FilePath = prevFilePath
dlg.Filter = "*.json|*.json"
dlg.Title = "Please select a configuration file"
if ok, err := dlg.ShowOpen(from); err != nil {
return "", err
} else if !ok {
return "", nil
}
logs.Info("config file dialog open %s", dlg.FilePath)
return dlg.FilePath, nil
}
func FileDialogSave(from walk.Form, prevFilePath string) (string, error) {
dlg := new(walk.FileDialog)
dlg.FilePath = prevFilePath
dlg.Filter = "*.json|*.json"
dlg.Title = "Create an empty configuration file"
if ok, err := dlg.ShowSave(from); err != nil {
return "", err
} else if !ok {
return "", nil
}
logs.Info("config file dialog save %s.json", dlg.FilePath)
return dlg.FilePath + ".json", nil
}
func (cfg *ApplicationConfig) Load() error {
body, err := os.ReadFile(filepath.Join(ConfigDirGet(), "config.json"))
if err != nil {
fmt.Printf("read log config file from app data dir fail, %s", err.Error())
return err
}
var config ApplicationConfig
err = json.Unmarshal(body, &config)
if err != nil {
fmt.Printf("json unmarshal log config fail, %s", err.Error())
return err
}
*cfg = config
return nil
}
func (cfg *ApplicationConfig) Save() {
value, err := json.Marshal(*cfg)
if err != nil {
fmt.Printf("json marshal log config fail, %s", err.Error())
return
}
err = os.WriteFile(filepath.Join(ConfigDirGet(), "config.json"), value, 0664)
if err != nil {
fmt.Printf("write log config to file fail, %s", err.Error())
return
}
}
func (cfg *ApplicationConfig) String() string {
value, err := json.Marshal(*cfg)
if err != nil {
return ""
}
return string(value)
}
func (cfg *ApplicationConfig) UpdateStartup(startup bool) {
cfg.Startup = startup
cfg.Save()
}
func (cfg *ApplicationConfig) UpdateLastPath(path string) {
cfg.LastPath = path
cfg.Save()
}
func (cfg *ApplicationConfig) UpdateLogPath(path string) {
cfg.LogPath = path
cfg.Save()
ApplicationLogInit(defaultApplicationConfig)
}
func ConfigInit() {
err := defaultApplicationConfig.Load()
if err != nil {
defaultApplicationConfig.Save()
}
if defaultApplicationConfig.LogPath == "" {
defaultApplicationConfig.LogPath = RunlogDirGet()
defaultApplicationConfig.Save()
}
ApplicationLogInit(defaultApplicationConfig)
logs.Info("config init %s", defaultApplicationConfig.String())
}