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

load config yaml to db

上级 3dde5f49
......@@ -26,7 +26,7 @@ func LoadConfigDef(defaultFile, configFile string, fieldsToExport *[]string) mod
}
defaultContent, err := ioutil.ReadFile(pathDefaultFile)
defaultContent = ReplaceSpecialChars(defaultContent)
defaultContent = stringUtils.ReplaceSpecialChars(defaultContent)
if err != nil {
logUtils.PrintToWithColor(i118Utils.I118Prt.Sprintf("fail_to_read_file", pathDefaultFile), color.FgCyan)
return defaultDef
......@@ -45,7 +45,7 @@ func LoadConfigDef(defaultFile, configFile string, fieldsToExport *[]string) mod
}
yamlContent, err := ioutil.ReadFile(pathConfigFile)
yamlContent = ReplaceSpecialChars(yamlContent)
yamlContent = stringUtils.ReplaceSpecialChars(yamlContent)
if err != nil {
logUtils.PrintToWithColor(i118Utils.I118Prt.Sprintf("fail_to_read_file", pathConfigFile), color.FgCyan)
return configDef
......@@ -227,26 +227,3 @@ func CopyField(child model.DefField, parent *model.DefField) {
}
}
func ReplaceSpecialChars(bytes []byte) []byte {
str := string(bytes)
inRanges := false // for ranges yaml only
ret := ""
for _, line := range strings.Split(str, "\n") {
if strings.Index(strings.TrimSpace(line), "ranges") == 0 {
inRanges = true
} else if len(line) > 0 && string(line[0]) != " " { // not begin with space, ranges end
inRanges = false
}
if strings.Index(strings.TrimSpace(line), "range") == 0 || inRanges {
line = strings.ReplaceAll(line,"[", string(constant.LeftBrackets))
line = strings.ReplaceAll(line,"]", string(constant.RightBrackets))
}
ret += line + "\n"
}
return []byte(ret)
}
......@@ -120,7 +120,7 @@ func getResFromYaml(resFile string) (valueMap map[string][]string, resName strin
}
yamlContent, err := ioutil.ReadFile(resFile)
yamlContent = ReplaceSpecialChars(yamlContent)
yamlContent = stringUtils.ReplaceSpecialChars(yamlContent)
if err != nil {
logUtils.PrintTo(i118Utils.I118Prt.Sprintf("fail_to_read_file", resFile))
......@@ -141,7 +141,7 @@ func getResFromYaml(resFile string) (valueMap map[string][]string, resName strin
} else {
configRes := model.DefField{}
err = yaml.Unmarshal(yamlContent, &configRes)
if err == nil { // config
if err == nil { // config
valueMap = getResForConfig(configRes)
resName = configRes.Field
}
......@@ -226,7 +226,7 @@ func getreferencedRangeOrInstant(inst model.DefField) (referencedRanges model.Re
resFile, _, _ := fileUtils.GetResProp(inst.From)
yamlContent, err := ioutil.ReadFile(resFile)
yamlContent = ReplaceSpecialChars(yamlContent)
yamlContent = stringUtils.ReplaceSpecialChars(yamlContent)
if err != nil {
logUtils.PrintTo(i118Utils.I118Prt.Sprintf("fail_to_read_file", resFile))
return
......@@ -280,4 +280,4 @@ func getResForConfig(configRes model.DefField) map[string][]string {
groupedValue["all"] = GenerateForField(&configRes, false)
return groupedValue
}
\ No newline at end of file
}
......@@ -243,9 +243,6 @@ type ZdConfig struct {
Name string `gorm:"column:name" json:"name"`
Desc string `gorm:"column:desc" json:"desc"`
Path string `gorm:"column:path" json:"path" yaml:"-"`
Yaml string `gorm:"yaml" json:"yaml"`
Folder string `gorm:"-" json:"folder" yaml:"-"`
Field string `gorm:"column:field" json:"field"`
Note string `gorm:"column:note" json:"note"`
......@@ -254,6 +251,9 @@ type ZdConfig struct {
Loop string `gorm:"column:loop" json:"loop"`
Loopfix string `gorm:"column:loopfix" json:"loopfix"`
Format string `gorm:"column:format" json:"format"`
Yaml string `gorm:"yaml" json:"yaml"`
Folder string `gorm:"-" json:"folder" yaml:"-"`
}
func (*ZdConfig) TableName() string {
return constant.TablePrefix + "config"
......
......@@ -5,6 +5,8 @@ import (
"github.com/easysoft/zendata/src/server/repo"
logUtils "github.com/easysoft/zendata/src/utils/log"
stringUtils "github.com/easysoft/zendata/src/utils/string"
"gopkg.in/yaml.v3"
"io/ioutil"
)
type ConfigService struct {
......@@ -55,7 +57,17 @@ func (s *ConfigService) saveResToDB(config []model.ResFile, list []*model.ZdConf
for _, item := range config {
if !stringUtils.FindInArrBool(item.Path, names) {
config := model.ZdConfig{Title: item.Title, Name: item.Name, Desc: item.Desc, Field: item.Title, Path: item.Path, Note: item.Desc}
content, _ := ioutil.ReadFile(item.Path)
yamlContent := stringUtils.ReplaceSpecialChars(content)
config := model.ZdConfig{}
err = yaml.Unmarshal(yamlContent, &config)
config.Title = item.Title
config.Name = item.Name
config.Desc = item.Desc
config.Path = item.Path
config.Field = item.Title
config.Note = item.Desc
s.configRepo.Save(&config)
}
}
......
......@@ -245,3 +245,26 @@ func UrlEncode(str string) (ret string) {
return
}
func ReplaceSpecialChars(bytes []byte) []byte {
str := string(bytes)
inRanges := false // for ranges yaml only
ret := ""
for _, line := range strings.Split(str, "\n") {
if strings.Index(strings.TrimSpace(line), "ranges") == 0 {
inRanges = true
} else if len(line) > 0 && string(line[0]) != " " { // not begin with space, ranges end
inRanges = false
}
if strings.Index(strings.TrimSpace(line), "range") == 0 || inRanges {
line = strings.ReplaceAll(line,"[", string(constant.LeftBrackets))
line = strings.ReplaceAll(line,"]", string(constant.RightBrackets))
}
ret += line + "\n"
}
return []byte(ret)
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册