Add support key=value pair for cli config

This commit is contained in:
Alex X
2024-05-13 14:14:28 +03:00
parent e4ff6d224f
commit 8f2bb3f34b
2 changed files with 86 additions and 6 deletions
+32 -6
View File
@@ -79,22 +79,26 @@ func Init() {
}
for _, conf := range confs {
if conf[0] != '{' {
if len(conf) == 0 {
continue
}
if conf[0] == '{' {
// config as raw YAML or JSON
configs = append(configs, []byte(conf))
} else if data := parseConfString(conf); data != nil {
configs = append(configs, data)
} else {
// config as file
if ConfigPath == "" {
ConfigPath = conf
}
data, _ := os.ReadFile(conf)
if data == nil {
if data, _ = os.ReadFile(conf); data == nil {
continue
}
data = []byte(shell.ReplaceEnvVars(string(data)))
configs = append(configs, data)
} else {
// config as raw YAML
configs = append(configs, []byte(conf))
}
}
@@ -190,3 +194,25 @@ func readRevisionTime() (revision, vcsTime string) {
}
return
}
func parseConfString(s string) []byte {
i := strings.IndexByte(s, '=')
if i < 0 {
return nil
}
items := strings.Split(s[:i], ".")
if len(items) < 2 {
return nil
}
// `log.level=trace` => `{log: {level: trace}}`
var pre string
var suf = s[i+1:]
for _, item := range items {
pre += "{" + item + ": "
suf += "}"
}
return []byte(pre + suf)
}