From ca978548e16ec343bc93749782d06181dfe5cba8 Mon Sep 17 00:00:00 2001 From: aaron <462826@qq.com> Date: Thu, 20 Aug 2020 13:48:10 +0800 Subject: [PATCH] fix res path issues --- demo/chinese.yaml | 22 ------------- demo/default.yaml | 13 ++++---- src/gen/excel.go | 2 +- src/gen/res.go | 61 ++++++++++++++++++++++------------- src/service/list.go | 10 +++--- src/utils/const/const.go | 5 ++- users/jenkins/ip/private.yaml | 8 ++--- 7 files changed, 57 insertions(+), 64 deletions(-) delete mode 100644 demo/chinese.yaml diff --git a/demo/chinese.yaml b/demo/chinese.yaml deleted file mode 100644 index b4f91e39..00000000 --- a/demo/chinese.yaml +++ /dev/null @@ -1,22 +0,0 @@ -title: zendata数据配置语法说明 -desc: -author: zentao -version: 1.0 -type: lines # lines|media|binary|article - -fields: - - - field: subject - from: chinese.words.v1.cn - select: word - where: ming='y' and dong="n" - - - field: verb - from: chinese.words.v1.cn - select: word - where: dong='y' - - - field: object - from: chinese.words.v1.cn - select: word - where: ming='y' diff --git a/demo/default.yaml b/demo/default.yaml index a8176d85..92cd630a 100644 --- a/demo/default.yaml +++ b/demo/default.yaml @@ -102,25 +102,24 @@ fields: postfix: "\t" - field: field_use_ranges # 引用內置的定义文件,该文件定义了多个range,他们共享了一些field层面的属性。 - from: custom.test.number.v1.yaml # 引用data/custom/number/v1.yaml文件里面的ranges定义。 + from: jenkins.number.v1.yaml # 引用data/custom/number/v1.yaml文件里面的ranges定义。 use: medium # 使用该文件中定义的medium分组。 postfix: "\t" - field: field_use_instance # 引用其他的定义文件,该文件定义了多个实例。 - from: system.ip.v1.yaml # 引用data/system/ip/v1.yaml + from: ip.v1.yaml # 引用data/system/ip/v1.yaml use: privateC,privateB # 使用该文件中定义的privateC和privateB两个实例。 postfix: "\t" - field: field_nested_instant # 引用其他的定义文件,且该文件引用了其他实例。 - from: custom.ip.private.yaml # 引用data/custom/ip/private.yaml + from: jenkins.ip.private.yaml # 引用data/custom/ip/private.yaml use: all # 使用该文件中的所有实例。 - prefix: "{" - postfix: "}" + postfix: "\t" - field: field_use_excel # 从excel数据源里面取数据。 - from: system.address.v1.china # 从data/system/address/v1.xlsx文件中读取名为china的工作簿。 + from: address.cn.v1.china # 从data/system/address/v1.xlsx文件中读取名为china的工作簿。 select: city # 查询city字段。 - where: state like '%山东%' # 条件是省份包含山东。 + where: state like '%山东%' # 条件是省份包含山东。 postfix: "\t" - field: field_with_children # 嵌套字段 diff --git a/src/gen/excel.go b/src/gen/excel.go index f1114775..48ebf36e 100644 --- a/src/gen/excel.go +++ b/src/gen/excel.go @@ -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.ResDir, "", -1) + dbName = strings.Replace(path, vari.WorkDir + constant.ResDirData, "", -1) dbName = strings.Replace(dbName, constant.PthSep, "_", -1) dbName = strings.Replace(dbName, ".", "_", -1) diff --git a/src/gen/res.go b/src/gen/res.go index 87d5cc44..7fc46df0 100644 --- a/src/gen/res.go +++ b/src/gen/res.go @@ -11,7 +11,6 @@ import ( "github.com/jinzhu/copier" "gopkg.in/yaml.v3" "io/ioutil" - "path/filepath" "strings" ) @@ -50,6 +49,7 @@ func loadResField(field *model.DefField, res *map[string]map[string][]string) { resFile, resType, sheet := getResProp(field.From) values, _ := getResValue(resFile, resType, sheet, field) (*res)[field.From] = values + } else if field.Config != "" { resFile, resType, _ := getResProp(field.Config) values, _ := getResValue(resFile, resType, "", field) @@ -60,43 +60,58 @@ func loadResField(field *model.DefField, res *map[string]map[string][]string) { func getResProp(from string) (resFile, resType, sheet string) { // from resource index := strings.LastIndex(from, ".yaml") - if index > -1 { // yaml, system.ip.v1.yaml - left := from[:index] - left = strings.ReplaceAll(left, ".", constant.PthSep) - - resFile = left + ".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 strings.Index(resFile, "yaml") == 0 || strings.Index(resFile, "users") == 0 { // build-in resource - resFile = vari.WorkDir + resFile - } else { - resPath := resFile - if !filepath.IsAbs(resPath) { + if resFile == "" { + resPath := vari.ConfigDir + resFile + if !fileUtils.FileExist(resPath) { // in same folder with passed config file - 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 = "" - } - } - } else { - if !fileUtils.FileExist(resPath) { + 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, ".") @@ -119,7 +134,7 @@ func convertExcelPath(from string) (ret, sheet string) { ret = file } - realPth := vari.WorkDir + constant.ResDir + ret + realPth := vari.WorkDir + constant.ResDirData + ret if fileUtils.FileExist(realPth) { if index == 1 { sheet = from[strings.LastIndex(from, ".")+1:] diff --git a/src/service/list.go b/src/service/list.go index 37547ab5..adcd778d 100644 --- a/src/service/list.go +++ b/src/service/list.go @@ -22,8 +22,10 @@ const ( func ListRes() { orderedKeys := [2]string{"yaml", "excel"} res := map[string][][size]string{} - path := vari.WorkDir + "data" - GetFilesAndDirs(path, &res) + + GetFilesAndDirs(constant.ResDirData, &res) + GetFilesAndDirs(constant.ResDirYaml, &res) + GetFilesAndDirs(constant.ResDirUsers, &res) names := make([]string, 0) nameWidth := 0 @@ -110,7 +112,7 @@ func ListRes() { } func GetFilesAndDirs(path string, res *map[string][][size]string) { - dir, err := ioutil.ReadDir(path) + dir, err := ioutil.ReadDir(vari.WorkDir + path) if err != nil { return } @@ -123,7 +125,7 @@ func GetFilesAndDirs(path string, res *map[string][][size]string) { arr := [size]string{} if strings.HasSuffix(name, ".yaml") { arr[0] = path + constant.PthSep + name - arr[1] = path[strings.LastIndex(path, "data"):] + constant.PthSep + name + arr[1] = path[strings.LastIndex(path, path):] + constant.PthSep + name arr[1] = strings.Trim(arr[1], "data"+constant.PthSep) (*res)["yaml"] = append((*res)["yaml"], arr) diff --git a/src/utils/const/const.go b/src/utils/const/const.go index 26b8a40b..91f749b1 100644 --- a/src/utils/const/const.go +++ b/src/utils/const/const.go @@ -50,7 +50,10 @@ var ( DefaultPort = 8848 DefaultRoot = "./" - ResDir = "data/" + ResDirData = "data/" + ResDirYaml = "yaml/" + ResDirUsers = "users/" + TmpDir = "tmp/" SqliteDriver = "sqlite3" diff --git a/users/jenkins/ip/private.yaml b/users/jenkins/ip/private.yaml index 591de4b6..36b8fb7c 100644 --- a/users/jenkins/ip/private.yaml +++ b/users/jenkins/ip/private.yaml @@ -3,22 +3,18 @@ desc: 引用系统IP定义 author: zentao version: 1.0 -# from: system.ip.v1.yaml - field: privateIP instances: - instance: all note: 引用所有內置IP - # from: system.ip.v1.yaml - fields: - field: part1 - from: system.ip.v1.yaml + from: ip.v1.yaml use: privateA, privateB, privateC prefix: "" postfix: "/" - field: part2 - from: custom.test.number.v1.yaml + from: jenkins.number.v1.yaml use: netmask \ No newline at end of file -- GitLab