✨ Parse config overrides on cli
This commit is contained in:
parent
40f9518611
commit
25f5c37243
8 changed files with 169 additions and 32 deletions
44
tools/parse.go
Normal file
44
tools/parse.go
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
package tools
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func ParseBytes(in []byte) any {
|
||||
return ParseString(string(in))
|
||||
}
|
||||
|
||||
func ParseString(in string) any {
|
||||
s := strings.TrimSpace(in)
|
||||
if s == "" {
|
||||
return s
|
||||
}
|
||||
|
||||
yesno := regexp.MustCompile("^(y|Y|yes|Yes|YES|n|N|no|No|NO|true|True|TRUE|false|False|FALSE|on|On|ON|off|Off|OFF)$")
|
||||
yes := regexp.MustCompile("^(y|Y|yes|Yes|YES|true|True|TRUE|on|On|ON)$")
|
||||
null := regexp.MustCompile("^(~|null|Null|NULL|none|None|NONE|nil|Nil|NIL)$")
|
||||
var j json.RawMessage
|
||||
if null.MatchString(s) {
|
||||
return nil
|
||||
} else if yesno.MatchString(s) {
|
||||
if yes.MatchString(s) {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
} else if i, err := strconv.Atoi(s); err == nil {
|
||||
return i
|
||||
} else if f, err := strconv.ParseFloat(s, 64); err == nil {
|
||||
return f
|
||||
} else if t, err := time.Parse(time.RFC3339, s); err == nil {
|
||||
return t
|
||||
} else if err := json.Unmarshal([]byte(s), &j); err == nil {
|
||||
return j
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue