/
server_group_list.go
87 lines (75 loc) · 1.64 KB
/
server_group_list.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
package teaconfigs
import (
"github.com/TeaWeb/build/internal/teaconfigs/shared"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/logs"
"gopkg.in/yaml.v3"
"io/ioutil"
"os"
)
type ServerGroupList struct {
Groups []*ServerGroup `yaml:"groups" json:"groups"`
}
func SharedServerGroupList() *ServerGroupList {
list := &ServerGroupList{
Groups: []*ServerGroup{},
}
filename := "servergrouplist.conf"
data, err := ioutil.ReadFile(Tea.ConfigFile(filename))
if err != nil {
if !os.IsNotExist(err) {
logs.Error(err)
}
return list
}
err = yaml.Unmarshal(data, list)
if err != nil {
logs.Error(err)
return list
}
return list
}
func (this *ServerGroupList) Add(group *ServerGroup) {
this.Groups = append(this.Groups, group)
}
func (this *ServerGroupList) Remove(id string) {
result := []*ServerGroup{}
for _, group := range this.Groups {
if group.Id == id {
continue
}
result = append(result, group)
}
this.Groups = result
}
// 查找分组
func (this *ServerGroupList) Find(id string) *ServerGroup {
for _, group := range this.Groups {
if group.Id == id {
return group
}
}
return nil
}
// 检查是否包含某个代理服务ID
func (this *ServerGroupList) ContainsServer(serverId string) bool {
for _, group := range this.Groups {
for _, id := range group.ServerIds {
if id == serverId {
return true
}
}
}
return false
}
// 保存
func (this *ServerGroupList) Save() error {
data, err := yaml.Marshal(this)
if err != nil {
return err
}
shared.Locker.Lock()
defer shared.Locker.WriteUnlock()
filename := "servergrouplist.conf"
return ioutil.WriteFile(Tea.ConfigFile(filename), data, 0666)
}