-
Notifications
You must be signed in to change notification settings - Fork 0
/
genPermissions.go
170 lines (130 loc) · 4.12 KB
/
genPermissions.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
package gen
import (
"encoding/json"
"io/ioutil"
"path"
"sort"
"strings"
"unicode"
"github.com/macinnir/dvc/core/lib"
"github.com/macinnir/dvc/core/lib/fetcher"
)
func fetchAllPermissions(controllersDir string) (map[string]string, error) {
cf := fetcher.NewControllerFetcher()
permissionMap := LoadPermissionsFromJSON()
controllers, _, e := cf.FetchAll()
if e != nil {
return nil, e
}
for k := range controllers {
controller := controllers[k]
// Extract the permissions from the controller
permissionMap[controller.Name+"_View"] = "View " + controller.Name
for k := range controller.Routes {
permissionMap[controller.Routes[k].Permission] = controller.Routes[k].Description
}
}
return permissionMap, nil
}
// LoadPermissionsFromJSON loads a set of permissions from a JSON file
func LoadPermissionsFromJSON() map[string]string {
permissionMap := map[string]string{}
fileBytes, e := ioutil.ReadFile(lib.PermissionsFile)
if e != nil {
panic(e)
}
json.Unmarshal(fileBytes, &permissionMap)
return permissionMap
}
func GenTSPerms(config *lib.Config) (e error) {
lib.EnsureDir(config.TypescriptPermissionsPath)
var permissionMap map[string]string
permissionMap, e = fetchAllPermissions(config.Dirs.Controllers)
if e != nil {
return
}
str := BuildTypescriptPermissions(permissionMap)
e = ioutil.WriteFile(path.Join(config.TypescriptPermissionsPath, "permissions.ts"), []byte(str), 0777)
return
}
func GenGoPerms(config *lib.Config) (e error) {
var permissionMap map[string]string
permissionMap, e = fetchAllPermissions(config.Dirs.Controllers)
if e != nil {
return
}
BuildPermissionsGoFile(permissionMap)
return
}
func BuildPermissionsGoFile(permissionMap map[string]string) {
permissions := make([]string, 0, len(permissionMap))
for k := range permissionMap {
if len(k) == 0 {
continue
}
permissions = append(permissions, k)
}
sort.Strings(permissions)
// for k := range permissions {
// permission := permissions[k]
// description := permissionMap[permissions[k]]
// // fmt.Println(permission + ": " + description)
// }
var b strings.Builder
b.WriteString(`// Generated Code; DO NOT EDIT.
package permissions
import (
"github.com/macinnir/dvc/core/lib/utils"
)
const (
`)
for k := range permissions {
// fmt.Println("Permission: ", k, permissions[k])
permTitle := string(unicode.ToUpper(rune(permissions[k][0]))) + permissions[k][1:]
b.WriteString(` // ` + permTitle + ` permission grants the ability of "` + permissionMap[permissions[k]] + `"
` + permTitle + ` utils.Permission = "` + permissions[k] + `"
`)
}
b.WriteString(`)
// Permissions returns a slice of permissions
func Permissions() map[utils.Permission]string {
return map[utils.Permission]string {
`)
for k := range permissions {
permTitle := string(unicode.ToUpper(rune(permissions[k][0]))) + permissions[k][1:]
b.WriteString(` ` + permTitle + `: "` + permissionMap[permissions[k]] + `",
`)
}
b.WriteString(` }
}
`)
lib.EnsureDir(lib.GoPermissionsPath)
// permissionsFilePath := path.Join("core", "definitions", "constants", "permissions", "permissions.go")
permissionsFilePath := path.Join(lib.GoPermissionsPath, "permissions.go")
var permissionsFileBytes []byte
var e error
permissionsFileBytes, e = lib.FormatCode(b.String())
if e != nil {
panic(e)
}
ioutil.WriteFile(permissionsFilePath, permissionsFileBytes, 0777)
}
// BuildTypescriptPermissions returns a formatted typescript file of permission constants
func BuildTypescriptPermissions(permissionMap map[string]string) string {
permissions := make([]string, 0, len(permissionMap))
for k := range permissionMap {
if len(k) == 0 {
continue
}
permissions = append(permissions, k)
}
sort.Strings(permissions)
permissionsFile := "// Generated Code; DO NOT EDIT.\n\n"
for k := range permissions {
permission := strings.TrimSpace(permissions[k])
permTitle := string(unicode.ToUpper(rune(permission[0]))) + permission[1:]
permissionsFile += "// " + permTitle + " -- " + permissionMap[permission] + "\n"
permissionsFile += "export const " + permTitle + "Permission = \"" + permission + "\";\n"
}
return permissionsFile
}