-
Notifications
You must be signed in to change notification settings - Fork 1
/
TaskConfigs.go
84 lines (68 loc) · 1.87 KB
/
TaskConfigs.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
package Tasks
import (
"encoding/gob"
"github.com/CloudWise-OpenSource/GoCrab/Api"
"github.com/CloudWise-OpenSource/GoCrab/Helpers"
"github.com/SeasX/SeasConfig/Enums"
"github.com/SeasX/SeasConfig/Models"
"os"
)
func LoadConfigs() error {
GoCrab.Debug("LoadConfigs Start")
for pid, apps := range Models.Apps {
if _, ok := Models.ConfigStore[pid]; !ok {
Models.ConfigStore[pid] = make(map[string]map[string]interface{})
}
if len(apps) > 0 {
for aid, app := range apps {
if _, ok := Models.ConfigStore[pid][aid]; !ok {
configs := make(map[string]interface{})
GoCrab.Debug("LoadConfig", app.Name, "start.")
GoCrab.Debug("App.Name ->", app.Name, "App.CreateTime ->", app.CreateTime)
LoadConfig(pid, aid, &configs)
Models.ConfigStore[pid][aid] = configs
GoCrab.Debug("LoadConfig", app.Name, "end.")
}
}
}
}
GoCrab.Debug("LoadConfigs Complete")
return nil
}
func LoadConfig(pid string, aid string, apps interface{}) error {
fileName := Enums.GetDBConfigFile(pid, aid)
if !Helpers.FileExists(fileName) {
GoCrab.Warn("LoadConfig Do not exists", fileName)
return nil
}
file, err := os.Open(fileName)
if err == nil {
decoder := gob.NewDecoder(file)
err = decoder.Decode(apps)
}
if err != nil {
GoCrab.Error("LoadConfig Error", fileName, err)
} else {
GoCrab.Debug("LoadConfig Successful", fileName)
}
file.Close()
return err
}
func SaveConfigs(pid string, aid string, apps interface{}) error {
dir := Enums.GetDBProductDir(pid)
if !Helpers.FileExists(dir) {
if _, err := Helpers.MakeDir(dir); err != nil {
GoCrab.Error("MakeDir error", dir, err)
return err
}
}
file, err := os.Create(Enums.GetDBConfigFile(pid, aid))
if err == nil {
encoder := gob.NewEncoder(file)
encoder.Encode(apps)
} else {
GoCrab.Error("SaveConfigs error", Enums.GetDBConfigFile(pid, aid), err)
}
file.Close()
return err
}