/
dealer.go
84 lines (72 loc) · 1.95 KB
/
dealer.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 service
import (
"encoding/json"
"os"
"github.com/Kyomotoi/go-ATRI/lib"
log "github.com/sirupsen/logrus"
)
const serviceDIR = "data/services/"
func init() {
exi := lib.IsDir(serviceDIR)
if !exi {
err := os.MkdirAll(serviceDIR, 0777)
if err != nil {
panic("service: 创建文件夹: " + serviceDIR + " 失败, 请尝试手动创建")
}
}
}
func generateServiceConfig(service string, docs string) {
tData := &ServiceInfo{
Service: service,
Docs: docs,
CommandList: make(map[string]CommandInfo),
Enabled: true,
}
data, _ := json.Marshal(tData)
filePath := serviceDIR + service + ".json"
err := os.WriteFile(filePath, data, 0777)
if err != nil {
log.Error("Write service info failed!")
}
}
func LoadCommandList(service string) map[string]CommandInfo {
filePath := serviceDIR + service + ".json"
data, err := os.ReadFile(filePath)
if err != nil {
if os.IsNotExist(err) {
generateServiceConfig(service, "nothing")
}
data, _ = os.ReadFile(filePath)
}
var si ServiceInfo
_ = json.Unmarshal(data, &si)
return si.CommandList
}
func StoneCommandList(service string, cmds map[string]CommandInfo) {
sd := LoadServiceData(service)
sd.CommandList = cmds
StoreServiceData(service, sd)
}
func LoadServiceData(service string) ServiceInfo {
var s ServiceInfo
filePath := serviceDIR + service + ".json"
data, err := os.ReadFile(filePath)
if err != nil {
if os.IsNotExist(err) {
panic("无法读取服务 " + service + " 中的信息,请重启以尝试修复该错误")
}
}
_ = json.Unmarshal(data, &s)
return s
}
func StoreServiceData(serv string, d ServiceInfo) {
filePath := serviceDIR + serv + ".json"
if !lib.IsExists(filePath) {
panic("无法读取服务 " + serv + " 中的信息,请重启以尝试修复该错误")
}
data, _ := json.Marshal(d)
err := os.WriteFile(filePath, data, 0777)
if err != nil {
panic("写入服务 " + serv + " 时失败,请检查是否给予程序足够的权限")
}
}