提交 20940883 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

close task#7692

上级 2f9bead8
......@@ -50,7 +50,7 @@ func GenerateFieldValuesFromExcel(path, sheet string, field *model.DefField) (ma
}
func getDbName(path string) (dbName string) {
dbName = strings.Replace(path, vari.WorkDir + constant.ResDirData, "", -1)
dbName = strings.Replace(path, vari.WorkDir + constant.ResDirData + constant.PthSep, "", -1)
dbName = strings.Replace(dbName, constant.PthSep, "_", -1)
dbName = strings.Replace(dbName, ".", "_", -1)
......
......@@ -2,7 +2,6 @@ package gen
import (
"github.com/easysoft/zendata/src/model"
constant "github.com/easysoft/zendata/src/utils/const"
fileUtils "github.com/easysoft/zendata/src/utils/file"
i118Utils "github.com/easysoft/zendata/src/utils/i118"
logUtils "github.com/easysoft/zendata/src/utils/log"
......@@ -11,7 +10,6 @@ import (
"github.com/jinzhu/copier"
"gopkg.in/yaml.v3"
"io/ioutil"
"strings"
)
func LoadResDef(fieldsToExport []string) map[string]map[string][]string {
......@@ -46,108 +44,17 @@ func loadResField(field *model.DefField, res *map[string]map[string][]string) {
loadResField(&child, res)
}
} else if field.From != "" {
resFile, resType, sheet := getResProp(field.From)
resFile, resType, sheet := fileUtils.GetResProp(field.From)
values, _ := getResValue(resFile, resType, sheet, field)
(*res)[field.From] = values
} else if field.Config != "" {
resFile, resType, _ := getResProp(field.Config)
resFile, resType, _ := fileUtils.GetResProp(field.Config)
values, _ := getResValue(resFile, resType, "", field)
(*res)[field.Config] = values
}
}
func getResProp(from string) (resFile, resType, sheet string) { // from resource
index := strings.LastIndex(from, ".yaml")
if index > -1 { // yaml, ip.v1.yaml
resFile = convertYamlPath(from)
resType = "yaml"
} else { // excel, like address.cn.v1.china
resFile, sheet = convertExcelPath(from)
resType = "excel"
}
if resFile == "" {
resPath := vari.ConfigDir + resFile
if !fileUtils.FileExist(resPath) { // in same folder with passed config file
resPath = vari.WorkDir + resFile
if !fileUtils.FileExist(resPath) { // in res file
resPath = ""
}
}
resFile = resPath
}
return
}
func convertYamlPath(from string) (ret string) {
arr := strings.Split(from, ".")
for i := 0; i < len(arr); i++ {
dir := ""
if i > 0 {
dir = strings.Join(arr[:i], constant.PthSep)
}
file := strings.Join(arr[i:], ".")
if dir != "" {
ret = dir + constant.PthSep + file
} else {
ret = file
}
realPth1 := vari.WorkDir + constant.ResDirYaml + ret
realPth2 := vari.WorkDir + constant.ResDirUsers + ret
if fileUtils.FileExist(realPth1) {
ret = realPth1
break
} else if fileUtils.FileExist(realPth2) {
ret = realPth2
break
}
}
return
}
func convertExcelPath(from string) (ret, sheet string) {
path1 := from // address.cn.v1
index := strings.LastIndex(from, ".")
path2 := from[:index] // address.cn.v1.china
paths := [2]string{path1, path2}
for index, path := range paths {
arr := strings.Split(path, ".")
for i := 0; i < len(arr); i++ {
dir := ""
if i > 0 {
dir = strings.Join(arr[:i], constant.PthSep)
}
file := strings.Join(arr[i:], ".") + ".xlsx"
if dir != "" {
ret = dir + constant.PthSep + file
} else {
ret = file
}
realPth := vari.WorkDir + constant.ResDirData + ret
if fileUtils.FileExist(realPth) {
if index == 1 {
sheet = from[strings.LastIndex(from, ".")+1:]
}
ret = realPth
return
}
}
}
return
}
func getResValue(resFile, resType, sheet string, field *model.DefField) (map[string][]string, string) {
resName := ""
groupedValues := map[string][]string{}
......@@ -243,7 +150,7 @@ func getResForInstances(insts model.ResInsts) map[string][]string {
}
func getRootRangeOrInstant(inst model.DefField) (parentRanges model.ResRanges, parentInsts model.ResInsts) {
resFile, _, _ := getResProp(inst.From)
resFile, _, _ := fileUtils.GetResProp(inst.From)
yamlContent, err := ioutil.ReadFile(resFile)
if err != nil {
......
......@@ -5,6 +5,7 @@ import (
"github.com/360EntSecGroup-Skylar/excelize/v2"
"github.com/easysoft/zendata/src/model"
constant "github.com/easysoft/zendata/src/utils/const"
fileUtils "github.com/easysoft/zendata/src/utils/file"
i118Utils "github.com/easysoft/zendata/src/utils/i118"
logUtils "github.com/easysoft/zendata/src/utils/log"
"github.com/easysoft/zendata/src/utils/vari"
......@@ -20,14 +21,14 @@ const (
)
func ListRes() {
orderedKeys := [2]string{"yaml", "excel"}
orderedKeys := [3]string{constant.ResDirData, constant.ResDirYaml, constant.ResDirUsers}
res := map[string][][size]string{}
GetFilesAndDirs(constant.ResDirData, &res)
GetFilesAndDirs(constant.ResDirYaml, &res)
GetFilesAndDirs(constant.ResDirUsers, &res)
for _, key := range orderedKeys {
GetFilesAndDirs(key, key, &res)
}
names := make([]string, 0)
//names := make([]string, 0)
nameWidth := 0
titleWidth := 0
for _, key := range orderedKeys {
......@@ -35,21 +36,21 @@ func ListRes() {
for index, arr := range arrOfArr {
path := arr[0]
if key == "yaml" {
if key == constant.ResDirYaml || key == constant.ResDirUsers {
arr[2], arr[3] = readYamlInfo(path)
} else if key == "excel" {
} else if key == constant.ResDirData {
arr[2], arr[3] = readExcelInfo(path)
}
res[key][index] = arr
name := pathToName(arr[1])
names = append(names, name)
name := pathToName(arr[1], key)
//names = append(names, name)
lent := runewidth.StringWidth(name)
if lent > nameWidth {
nameWidth = lent
}
if key == "excel" {
if key == constant.ResDirData {
sheets := strings.Split(arr[2], "|")
for _, sheet := range sheets {
lent2 := runewidth.StringWidth(sheet)
......@@ -66,27 +67,22 @@ func ListRes() {
}
}
sysMsg := ""
customMsg := ""
dataMsg := ""
yamlMsg := ""
usersMsg := ""
idx := 0
for _, key := range orderedKeys {
arrOfArr := res[key]
arrOfArr = sortByName(arrOfArr)
for _, arr := range arrOfArr {
name := names[idx]
name := pathToName(arr[1], key)
titleStr := arr[2]
titles := strings.Split(titleStr, "|")
idx2 := 0
isBuildin := false
for _, title := range titles {
if strings.Index(name, "system") > -1 {
isBuildin = true
} else {
isBuildin = false
}
if idx2 > 0 {
name = ""
}
......@@ -95,10 +91,12 @@ func ListRes() {
title = title + strings.Repeat(" ", titleWidth - runewidth.StringWidth(title))
msg := fmt.Sprintf("%s %s %s\n", name, title, arr[3])
if isBuildin {
sysMsg = sysMsg + msg
} else {
customMsg = customMsg + msg
if key == constant.ResDirData {
dataMsg = dataMsg + msg
} else if key == constant.ResDirYaml {
yamlMsg = yamlMsg + msg
} else if key == constant.ResDirUsers {
usersMsg = usersMsg + msg
}
idx2++
......@@ -108,33 +106,35 @@ func ListRes() {
}
}
logUtils.PrintTo(sysMsg + "\n" + customMsg)
logUtils.PrintTo(dataMsg + "\n" + yamlMsg + "\n" + usersMsg)
}
func GetFilesAndDirs(path string, res *map[string][][size]string) {
dir, err := ioutil.ReadDir(vari.WorkDir + path)
func GetFilesAndDirs(path, typ string, res *map[string][][size]string) {
if !fileUtils.IsAbosutePath(path) {
path = vari.WorkDir + path
}
dir, err := ioutil.ReadDir(path)
if err != nil {
return
}
for _, fi := range dir {
if fi.IsDir() {
GetFilesAndDirs(path + constant.PthSep + fi.Name(), res)
GetFilesAndDirs(path + constant.PthSep + fi.Name(), typ, res)
} else {
name := fi.Name()
arr := [size]string{}
if strings.HasSuffix(name, ".yaml") {
arr[0] = path + constant.PthSep + name
arr[1] = path[strings.LastIndex(path, path):] + constant.PthSep + name
arr[1] = strings.Trim(arr[1], "data"+constant.PthSep)
arr[1] = arr[0]
(*res)["yaml"] = append((*res)["yaml"], arr)
(*res)[typ] = append((*res)[typ], arr)
} else if strings.HasSuffix(name, ".xlsx") {
arr[0] = path + constant.PthSep + name
arr[1] = path[strings.LastIndex(path, "data"):] + constant.PthSep + name
arr[1] = strings.Trim(arr[1], "data"+constant.PthSep)
arr[1] = arr[0]
(*res)["excel"] = append((*res)["excel"], arr)
(*res)[typ] = append((*res)[typ], arr)
}
}
......@@ -178,9 +178,12 @@ func readExcelInfo(path string) (title string, desc string) {
return
}
func pathToName(path string) string {
func pathToName(path, key string) string {
name := strings.ReplaceAll(path, constant.PthSep,".")
name = name[:strings.LastIndex(name, ".")]
name = strings.Split(name, "." + key + ".")[1]
if key == constant.ResDirData {
name = name[:strings.LastIndex(name, ".")]
}
return name
}
......
......@@ -14,7 +14,7 @@ import (
)
func ViewRes(res string) {
resType, resPath, sheet := fileUtils.ConvertResPath(res)
resPath, resType, sheet := fileUtils.GetResProp(res)
if resType == "yaml" {
typ, inst, ranges := readYamlData(resPath)
......
......@@ -50,9 +50,9 @@ var (
DefaultPort = 8848
DefaultRoot = "./"
ResDirData = "data/"
ResDirYaml = "yaml/"
ResDirUsers = "users/"
ResDirData = "data"
ResDirYaml = "yaml"
ResDirUsers = "users"
TmpDir = "tmp/"
......
......@@ -153,33 +153,93 @@ func GetAbsDir(path string) string {
return abs
}
func ConvertResPath(path string) (resType, resFile, sheet string) {
resName := strings.ReplaceAll(path, ".", constant.PthSep)
func GetResProp(from string) (resFile, resType, sheet string) { // from resource
resPath := AddRootPath(resName) + ".yaml"
if FileExist(resPath) {
index := strings.LastIndex(from, ".yaml")
if index > -1 { // yaml, ip.v1.yaml
resFile = convertYamlPath(from)
resType = "yaml"
} else { // excel, like address.cn.v1.china
resFile, sheet = convertExcelPath(from)
resType = "excel"
}
if resFile == "" {
resPath := vari.ConfigDir + resFile
if !FileExist(resPath) { // in same folder with passed config file
resPath = vari.WorkDir + resFile
if !FileExist(resPath) { // in res file
resPath = ""
}
}
resFile = resPath
}
} else {
resType = "excel"
return
}
func convertYamlPath(from string) (ret string) {
arr := strings.Split(from, ".")
for i := 0; i < len(arr); i++ {
dir := ""
if i > 0 {
dir = strings.Join(arr[:i], constant.PthSep)
}
file := strings.Join(arr[i:], ".")
resFile = resName + ".xlsx"
resFile = AddRootPath(resFile)
if FileExist(AddRootPath(resName + ".xlsx")) { // no sheet name
sheet = ""
if dir != "" {
ret = dir + constant.PthSep + file
} else {
resFile = resName[:strings.LastIndex(resName, constant.PthSep)] + ".xlsx"
resFile = AddRootPath(resFile)
sheet = path[strings.LastIndex(resName, constant.PthSep)+1:]
ret = file
}
realPth1 := vari.WorkDir + constant.ResDirYaml + constant.PthSep + ret
realPth2 := vari.WorkDir + constant.ResDirUsers + constant.PthSep + ret
if FileExist(realPth1) {
ret = realPth1
break
} else if FileExist(realPth2) {
ret = realPth2
break
}
}
return
}
func AddRootPath(path string) string {
path = vari.WorkDir + "data" + constant.PthSep + path
func convertExcelPath(from string) (ret, sheet string) {
path1 := from // address.cn.v1
index := strings.LastIndex(from, ".")
path2 := from[:index] // address.cn.v1.china
paths := [2]string{path1, path2}
for index, path := range paths {
arr := strings.Split(path, ".")
for i := 0; i < len(arr); i++ {
dir := ""
if i > 0 {
dir = strings.Join(arr[:i], constant.PthSep)
}
file := strings.Join(arr[i:], ".") + ".xlsx"
if dir != "" {
ret = dir + constant.PthSep + file
} else {
ret = file
}
realPth := vari.WorkDir + constant.ResDirData + constant.PthSep + ret
if FileExist(realPth) {
if index == 1 {
sheet = from[strings.LastIndex(from, ".")+1:]
}
ret = realPth
return
}
}
}
return path
return
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册