mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2025-11-06 14:50:24 +08:00
修复从API代码中提取角色时无法查找子目录的Bug
This commit is contained in:
7704
build/rpc.json
7704
build/rpc.json
File diff suppressed because it is too large
Load Diff
@@ -9,6 +9,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
_ "github.com/iwind/TeaGo/bootstrap"
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
|
"github.com/iwind/TeaGo/logs"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -96,85 +97,101 @@ func main() {
|
|||||||
var methodRolesMap = map[string][]string{} // method => roles
|
var methodRolesMap = map[string][]string{} // method => roles
|
||||||
{
|
{
|
||||||
var rootDir = filepath.Clean(Tea.Root + "/../../EdgeAPI/internal/rpc/services")
|
var rootDir = filepath.Clean(Tea.Root + "/../../EdgeAPI/internal/rpc/services")
|
||||||
files, err := filepath.Glob(rootDir + "/service_*.go")
|
entries, err := os.ReadDir(rootDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("[ERROR]list service implementation files failed: " + err.Error())
|
logs.Println("[ERROR]read api services from '" + rootDir + "' failed: " + err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var methodNameReg = regexp.MustCompile(`func\s*\(\w+\s+\*\s*(\w+Service)\)\s*(\w+)\s*\(`) // $1: serviceName, $2 methodName
|
var rootDirs = []string{rootDir}
|
||||||
for _, file := range files {
|
|
||||||
data, err := os.ReadFile(file)
|
for _, entry := range entries {
|
||||||
|
if entry.IsDir() {
|
||||||
|
rootDirs = append(rootDirs, rootDir+string(os.PathSeparator)+entry.Name())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, rootDir := range rootDirs {
|
||||||
|
files, err := filepath.Glob(rootDir + "/service_*.go")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("[ERROR]read file '" + file + "' failed: " + err.Error())
|
fmt.Println("[ERROR]list service implementation files failed: " + err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var sourceCode = string(data)
|
|
||||||
|
|
||||||
var locList = methodNameReg.FindAllStringIndex(sourceCode, -1)
|
var methodNameReg = regexp.MustCompile(`func\s*\(\w+\s+\*\s*(\w+Service)\)\s*(\w+)\s*\(`) // $1: serviceName, $2 methodName
|
||||||
for index, loc := range locList {
|
for _, file := range files {
|
||||||
var methodSource = ""
|
data, err := os.ReadFile(file)
|
||||||
if index == len(locList)-1 { // last one
|
if err != nil {
|
||||||
methodSource = sourceCode[loc[0]:]
|
fmt.Println("[ERROR]read file '" + file + "' failed: " + err.Error())
|
||||||
} else {
|
return
|
||||||
methodSource = sourceCode[loc[0]:locList[index+1][0]]
|
|
||||||
}
|
}
|
||||||
|
var sourceCode = string(data)
|
||||||
|
|
||||||
// 方法名
|
var locList = methodNameReg.FindAllStringIndex(sourceCode, -1)
|
||||||
var submatch = methodNameReg.FindStringSubmatch(methodSource)
|
for index, loc := range locList {
|
||||||
if len(submatch) == 0 {
|
var methodSource = ""
|
||||||
continue
|
if index == len(locList)-1 { // last one
|
||||||
}
|
methodSource = sourceCode[loc[0]:]
|
||||||
var serviceName = submatch[1]
|
} else {
|
||||||
if serviceName == "BaseService" {
|
methodSource = sourceCode[loc[0]:locList[index+1][0]]
|
||||||
continue
|
}
|
||||||
}
|
|
||||||
var methodName = submatch[2]
|
|
||||||
if methodName[0] < 'A' || methodName[0] > 'Z' {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
var roles = []string{}
|
|
||||||
if strings.Contains(methodSource, ".ValidateNode(") {
|
|
||||||
roles = append(roles, "node")
|
|
||||||
}
|
|
||||||
if strings.Contains(methodSource, ".ValidateUserNode(") {
|
|
||||||
roles = append(roles, "user")
|
|
||||||
}
|
|
||||||
if strings.Contains(methodSource, ".ValidateAdmin(") {
|
|
||||||
roles = append(roles, "admin")
|
|
||||||
}
|
|
||||||
if strings.Contains(methodSource, ".ValidateAdminAndUser(") {
|
|
||||||
roles = append(roles, "admin", "user")
|
|
||||||
}
|
|
||||||
if strings.Contains(methodSource, ".ValidateNSNode(") {
|
|
||||||
roles = append(roles, "dns")
|
|
||||||
}
|
|
||||||
if strings.Contains(methodSource, ".ValidateMonitorNode(") {
|
|
||||||
roles = append(roles, "monitor")
|
|
||||||
}
|
|
||||||
if strings.Contains(methodSource, "rpcutils.UserTypeDNS") {
|
|
||||||
roles = append(roles, "dns")
|
|
||||||
}
|
|
||||||
if strings.Contains(methodSource, "rpcutils.UserTypeUser") {
|
|
||||||
roles = append(roles, "user")
|
|
||||||
}
|
|
||||||
if strings.Contains(methodSource, "rpcutils.UserTypeNode") {
|
|
||||||
roles = append(roles, "node")
|
|
||||||
}
|
|
||||||
if strings.Contains(methodSource, "rpcutils.UserTypeMonitor") {
|
|
||||||
roles = append(roles, "monitor")
|
|
||||||
}
|
|
||||||
if strings.Contains(methodSource, "rpcutils.UserTypeReport") {
|
|
||||||
roles = append(roles, "report")
|
|
||||||
}
|
|
||||||
if strings.Contains(methodSource, "rpcutils.UserTypeCluster") {
|
|
||||||
roles = append(roles, "cluster")
|
|
||||||
}
|
|
||||||
if strings.Contains(methodSource, "rpcutils.UserTypeAdmin") {
|
|
||||||
roles = append(roles, "admin")
|
|
||||||
}
|
|
||||||
|
|
||||||
methodRolesMap[strings.ToLower(methodName)] = removeDuplicates(roles)
|
// 方法名
|
||||||
|
var submatch = methodNameReg.FindStringSubmatch(methodSource)
|
||||||
|
if len(submatch) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
var serviceName = submatch[1]
|
||||||
|
if serviceName == "BaseService" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
var methodName = submatch[2]
|
||||||
|
if methodName[0] < 'A' || methodName[0] > 'Z' {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
var roles = []string{}
|
||||||
|
if strings.Contains(methodSource, ".ValidateNode(") {
|
||||||
|
roles = append(roles, "node")
|
||||||
|
}
|
||||||
|
if strings.Contains(methodSource, ".ValidateUserNode(") {
|
||||||
|
roles = append(roles, "user")
|
||||||
|
}
|
||||||
|
if strings.Contains(methodSource, ".ValidateAdmin(") {
|
||||||
|
roles = append(roles, "admin")
|
||||||
|
}
|
||||||
|
if strings.Contains(methodSource, ".ValidateAdminAndUser(") {
|
||||||
|
roles = append(roles, "admin", "user")
|
||||||
|
}
|
||||||
|
if strings.Contains(methodSource, ".ValidateNSNode(") {
|
||||||
|
roles = append(roles, "dns")
|
||||||
|
}
|
||||||
|
if strings.Contains(methodSource, ".ValidateMonitorNode(") {
|
||||||
|
roles = append(roles, "monitor")
|
||||||
|
}
|
||||||
|
if strings.Contains(methodSource, "rpcutils.UserTypeDNS") {
|
||||||
|
roles = append(roles, "dns")
|
||||||
|
}
|
||||||
|
if strings.Contains(methodSource, "rpcutils.UserTypeUser") {
|
||||||
|
roles = append(roles, "user")
|
||||||
|
}
|
||||||
|
if strings.Contains(methodSource, "rpcutils.UserTypeNode") {
|
||||||
|
roles = append(roles, "node")
|
||||||
|
}
|
||||||
|
if strings.Contains(methodSource, "rpcutils.UserTypeMonitor") {
|
||||||
|
roles = append(roles, "monitor")
|
||||||
|
}
|
||||||
|
if strings.Contains(methodSource, "rpcutils.UserTypeReport") {
|
||||||
|
roles = append(roles, "report")
|
||||||
|
}
|
||||||
|
if strings.Contains(methodSource, "rpcutils.UserTypeCluster") {
|
||||||
|
roles = append(roles, "cluster")
|
||||||
|
}
|
||||||
|
if strings.Contains(methodSource, "rpcutils.UserTypeAdmin") {
|
||||||
|
roles = append(roles, "admin")
|
||||||
|
}
|
||||||
|
|
||||||
|
methodRolesMap[strings.ToLower(methodName)] = removeDuplicates(roles)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user