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

fix res path issues

上级 7e46d91f
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'
......@@ -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 # 嵌套字段
......
......@@ -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)
......
......@@ -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:]
......
......@@ -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)
......
......@@ -50,7 +50,10 @@ var (
DefaultPort = 8848
DefaultRoot = "./"
ResDir = "data/"
ResDirData = "data/"
ResDirYaml = "yaml/"
ResDirUsers = "users/"
TmpDir = "tmp/"
SqliteDriver = "sqlite3"
......
......@@ -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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册