-
Notifications
You must be signed in to change notification settings - Fork 34
/
access_log_storage_policy_list.go
80 lines (71 loc) · 1.83 KB
/
access_log_storage_policy_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
package teaconfigs
import (
"github.com/TeaWeb/code/teaconfigs/shared"
"github.com/go-yaml/yaml"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/files"
"github.com/iwind/TeaGo/logs"
"io/ioutil"
)
var accessLogStoragePolicyListFilename = "accesslog.storage.list.conf"
// 获取共享的存储策略列表
func SharedAccessLogStoragePolicyList() *AccessLogStoragePolicyList {
path := Tea.ConfigFile(accessLogStoragePolicyListFilename)
file := files.NewFile(path)
if !file.Exists() {
return &AccessLogStoragePolicyList{}
}
reader, err := file.Reader()
if err != nil {
logs.Error(err)
return &AccessLogStoragePolicyList{}
}
defer reader.Close()
policyList := &AccessLogStoragePolicyList{}
err = reader.ReadYAML(policyList)
if err != nil {
logs.Error(err)
return policyList
}
return policyList
}
// 存储策略列表
type AccessLogStoragePolicyList struct {
Ids []string `yaml:"id" json:"id"`
}
// 添加策略ID
func (this *AccessLogStoragePolicyList) AddId(id string) {
this.Ids = append(this.Ids, id)
}
// 删除策略ID
func (this *AccessLogStoragePolicyList) RemoveId(id string) {
result := []string{}
for _, id2 := range this.Ids {
if id2 == id {
continue
}
result = append(result, id2)
}
this.Ids = result
}
// 保存
func (this *AccessLogStoragePolicyList) Save() error {
data, err := yaml.Marshal(this)
if err != nil {
return err
}
return ioutil.WriteFile(Tea.ConfigFile(accessLogStoragePolicyListFilename), data, 0666)
}
// 查找所有的策略列表
func (this *AccessLogStoragePolicyList) FindAllPolicies() []*AccessLogStoragePolicy {
shared.Locker.Lock()
defer shared.Locker.WriteUnlockNotify()
result := []*AccessLogStoragePolicy{}
for _, id := range this.Ids {
policy := NewAccessLogStoragePolicyFromId(id)
if policy != nil {
result = append(result, policy)
}
}
return result
}