提交 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: ...@@ -102,25 +102,24 @@ fields:
postfix: "\t" postfix: "\t"
- field: field_use_ranges # 引用內置的定义文件,该文件定义了多个range,他们共享了一些field层面的属性。 - 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分组。 use: medium # 使用该文件中定义的medium分组。
postfix: "\t" postfix: "\t"
- field: field_use_instance # 引用其他的定义文件,该文件定义了多个实例。 - 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两个实例。 use: privateC,privateB # 使用该文件中定义的privateC和privateB两个实例。
postfix: "\t" postfix: "\t"
- field: field_nested_instant # 引用其他的定义文件,且该文件引用了其他实例。 - 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 # 使用该文件中的所有实例。 use: all # 使用该文件中的所有实例。
prefix: "{" postfix: "\t"
postfix: "}"
- field: field_use_excel # 从excel数据源里面取数据。 - 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字段。 select: city # 查询city字段。
where: state like '%山东%' # 条件是省份包含山东。 where: state like '%山东%' # 条件是省份包含山东。
postfix: "\t" postfix: "\t"
- field: field_with_children # 嵌套字段 - field: field_with_children # 嵌套字段
......
...@@ -50,7 +50,7 @@ func GenerateFieldValuesFromExcel(path, sheet string, field *model.DefField) (ma ...@@ -50,7 +50,7 @@ func GenerateFieldValuesFromExcel(path, sheet string, field *model.DefField) (ma
} }
func getDbName(path string) (dbName string) { 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, constant.PthSep, "_", -1)
dbName = strings.Replace(dbName, ".", "_", -1) dbName = strings.Replace(dbName, ".", "_", -1)
......
...@@ -11,7 +11,6 @@ import ( ...@@ -11,7 +11,6 @@ import (
"github.com/jinzhu/copier" "github.com/jinzhu/copier"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"io/ioutil" "io/ioutil"
"path/filepath"
"strings" "strings"
) )
...@@ -50,6 +49,7 @@ func loadResField(field *model.DefField, res *map[string]map[string][]string) { ...@@ -50,6 +49,7 @@ func loadResField(field *model.DefField, res *map[string]map[string][]string) {
resFile, resType, sheet := getResProp(field.From) resFile, resType, sheet := getResProp(field.From)
values, _ := getResValue(resFile, resType, sheet, field) values, _ := getResValue(resFile, resType, sheet, field)
(*res)[field.From] = values (*res)[field.From] = values
} else if field.Config != "" { } else if field.Config != "" {
resFile, resType, _ := getResProp(field.Config) resFile, resType, _ := getResProp(field.Config)
values, _ := getResValue(resFile, resType, "", field) values, _ := getResValue(resFile, resType, "", field)
...@@ -60,43 +60,58 @@ func loadResField(field *model.DefField, res *map[string]map[string][]string) { ...@@ -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 func getResProp(from string) (resFile, resType, sheet string) { // from resource
index := strings.LastIndex(from, ".yaml") index := strings.LastIndex(from, ".yaml")
if index > -1 { // yaml, system.ip.v1.yaml if index > -1 { // yaml, ip.v1.yaml
left := from[:index] resFile = convertYamlPath(from)
left = strings.ReplaceAll(left, ".", constant.PthSep)
resFile = left + ".yaml"
resType = "yaml" resType = "yaml"
} else { // excel, like address.cn.v1.china } else { // excel, like address.cn.v1.china
resFile, sheet = convertExcelPath(from) resFile, sheet = convertExcelPath(from)
resType = "excel" resType = "excel"
} }
if strings.Index(resFile, "yaml") == 0 || strings.Index(resFile, "users") == 0 { // build-in resource if resFile == "" {
resFile = vari.WorkDir + resFile resPath := vari.ConfigDir + resFile
} else { if !fileUtils.FileExist(resPath) { // in same folder with passed config file
resPath := resFile
if !filepath.IsAbs(resPath) {
resPath = vari.ConfigDir + resFile resPath = vari.WorkDir + resFile
if !fileUtils.FileExist(resPath) { // in same folder with passed config file if !fileUtils.FileExist(resPath) { // in res file
resPath = vari.WorkDir + resFile
if !fileUtils.FileExist(resPath) { // in res file
resPath = ""
}
}
} else {
if !fileUtils.FileExist(resPath) {
resPath = "" resPath = ""
} }
} }
resFile = resPath resFile = resPath
} }
return 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) { func convertExcelPath(from string) (ret, sheet string) {
path1 := from // address.cn.v1 path1 := from // address.cn.v1
index := strings.LastIndex(from, ".") index := strings.LastIndex(from, ".")
...@@ -119,7 +134,7 @@ func convertExcelPath(from string) (ret, sheet string) { ...@@ -119,7 +134,7 @@ func convertExcelPath(from string) (ret, sheet string) {
ret = file ret = file
} }
realPth := vari.WorkDir + constant.ResDir + ret realPth := vari.WorkDir + constant.ResDirData + ret
if fileUtils.FileExist(realPth) { if fileUtils.FileExist(realPth) {
if index == 1 { if index == 1 {
sheet = from[strings.LastIndex(from, ".")+1:] sheet = from[strings.LastIndex(from, ".")+1:]
......
...@@ -22,8 +22,10 @@ const ( ...@@ -22,8 +22,10 @@ const (
func ListRes() { func ListRes() {
orderedKeys := [2]string{"yaml", "excel"} orderedKeys := [2]string{"yaml", "excel"}
res := map[string][][size]string{} 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) names := make([]string, 0)
nameWidth := 0 nameWidth := 0
...@@ -110,7 +112,7 @@ func ListRes() { ...@@ -110,7 +112,7 @@ func ListRes() {
} }
func GetFilesAndDirs(path string, res *map[string][][size]string) { func GetFilesAndDirs(path string, res *map[string][][size]string) {
dir, err := ioutil.ReadDir(path) dir, err := ioutil.ReadDir(vari.WorkDir + path)
if err != nil { if err != nil {
return return
} }
...@@ -123,7 +125,7 @@ func GetFilesAndDirs(path string, res *map[string][][size]string) { ...@@ -123,7 +125,7 @@ func GetFilesAndDirs(path string, res *map[string][][size]string) {
arr := [size]string{} arr := [size]string{}
if strings.HasSuffix(name, ".yaml") { if strings.HasSuffix(name, ".yaml") {
arr[0] = path + constant.PthSep + name 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) arr[1] = strings.Trim(arr[1], "data"+constant.PthSep)
(*res)["yaml"] = append((*res)["yaml"], arr) (*res)["yaml"] = append((*res)["yaml"], arr)
......
...@@ -50,7 +50,10 @@ var ( ...@@ -50,7 +50,10 @@ var (
DefaultPort = 8848 DefaultPort = 8848
DefaultRoot = "./" DefaultRoot = "./"
ResDir = "data/" ResDirData = "data/"
ResDirYaml = "yaml/"
ResDirUsers = "users/"
TmpDir = "tmp/" TmpDir = "tmp/"
SqliteDriver = "sqlite3" SqliteDriver = "sqlite3"
......
...@@ -3,22 +3,18 @@ desc: 引用系统IP定义 ...@@ -3,22 +3,18 @@ desc: 引用系统IP定义
author: zentao author: zentao
version: 1.0 version: 1.0
# from: system.ip.v1.yaml
field: privateIP field: privateIP
instances: instances:
- instance: all - instance: all
note: 引用所有內置IP note: 引用所有內置IP
# from: system.ip.v1.yaml
fields: fields:
- field: part1 - field: part1
from: system.ip.v1.yaml from: ip.v1.yaml
use: privateA, privateB, privateC use: privateA, privateB, privateC
prefix: "" prefix: ""
postfix: "/" postfix: "/"
- field: part2 - field: part2
from: custom.test.number.v1.yaml from: jenkins.number.v1.yaml
use: netmask 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.
先完成此消息的编辑!
想要评论请 注册