-
Notifications
You must be signed in to change notification settings - Fork 101
/
initialization.go
187 lines (165 loc) · 3.64 KB
/
initialization.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
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
//go:build init
package main
import (
"bytes"
"encoding/csv"
"fmt"
"os"
"sort"
"strings"
"time"
_ "github.com/APIParkLab/APIPark/resources/access"
_ "github.com/APIParkLab/APIPark/resources/permit"
_ "github.com/APIParkLab/APIPark/resources/plugin"
"github.com/eolinker/eosc/log"
"github.com/eolinker/go-common/access"
"github.com/eolinker/go-common/permit"
"github.com/eolinker/go-common/pm3"
"github.com/eolinker/go-common/utils"
)
const unsetValue = "-"
func doCheck() {
accessConf, unset := loadAccess()
drivers := pm3.List()
newAccess := 0
for _, p := range drivers {
if ac, ok := p.(pm3.AccessConfig); ok {
if len(ac.Access()) > 0 {
for asKey := range ac.Access() {
key := strings.ToLower(asKey)
if _, has := accessConf[key]; !has {
accessConf[key] = empty(key)
newAccess++
}
}
}
}
}
for asKey := range permit.All() {
key := strings.ToLower(asKey)
if _, has := accessConf[key]; !has {
accessConf[key] = empty(key)
newAccess++
}
}
if newAccess > 0 || unset > 0 {
f := accessFile()
fmt.Printf("%d access need set, see : %s and %s", newAccess+unset, saveTemplate(accessConf, f), saveCsv(accessConf, f))
}
os.Exit(0)
}
func accessFile() string {
if version == "" {
return time.Now().Format("20060102-150405")
}
return version
}
func saveCsv(as map[string]*Access, key string) string {
buf := &bytes.Buffer{}
w := csv.NewWriter(buf)
err := w.Write([]string{"group", "name", "cname", "desc"})
if err != nil {
return ""
}
list := utils.MapToSliceNoKey(as)
sort.Sort(AccessListSort(list))
for _, i := range list {
err := w.Write([]string{i.Group, i.Name, i.Cname, i.Desc})
if err != nil {
return ""
}
}
w.Flush()
filePath := fmt.Sprintf("access.%s.csv", key)
err = os.WriteFile(filePath, buf.Bytes(), 0666)
if err != nil {
log.Fatal(err)
}
return filePath
}
type AccessListSort []*Access
func (ls AccessListSort) Len() int {
return len(ls)
}
func (ls AccessListSort) Less(i, j int) bool {
if ls[i].Group != ls[j].Group {
return ls[i].Group < ls[j].Group
}
if ls[i].Cname != ls[j].Cname {
return ls[i].Cname < ls[j].Cname
}
return ls[i].Name < ls[j].Name
}
func (ls AccessListSort) Swap(i, j int) {
ls[i], ls[j] = ls[j], ls[i]
}
func saveTemplate(as map[string]*Access, key string) string {
out := make(map[string][]access.Access)
for _, a := range as {
out[a.Group] = append(out[a.Group], access.Access{
Name: a.Name,
CName: a.Cname,
Desc: a.Desc,
})
}
buf := &bytes.Buffer{}
err := yaml.NewEncoder(buf).Encode(out)
if err != nil {
log.Fatal(err)
return ""
}
filePath := fmt.Sprintf("access.%s.yml", key)
err = os.WriteFile(filePath, buf.Bytes(), 0666)
if err != nil {
log.Fatal(err)
}
return filePath
}
type Access struct {
Key string
Group string
Name string
Cname string
Desc string
}
func empty(key string) *Access {
group, name := readKey(key)
return &Access{
Key: key,
Group: group,
Name: name,
Cname: unsetValue,
Desc: unsetValue,
}
}
func readKey(key string) (group string, name string) {
ls := strings.Split(key, ".")
if len(ls) != 2 {
log.Fatal("invalid access key:[%s]", key)
}
return ls[0], ls[1]
}
func loadAccess() (map[string]*Access, int) {
confAccess := access.All()
unset := 0
as := make(map[string]*Access)
for group, al := range confAccess {
for _, a := range al {
g, name := readKey(a.Name)
if g != group {
log.Fatal("invalid access key:[%s]", a.Name)
}
as[a.Name] = &Access{
Key: a.Name,
Group: group,
Name: name,
Cname: a.CName,
Desc: a.Desc,
}
if a.CName == unsetValue || a.Desc == unsetValue {
unset++
}
}
}
return as, unset
}