
FIXED: * `envs/funcs.go:78:3: unknown field IgnoreWhiteSpace in struct literal of type EnvErrNoVal, but does have IgnoreWhitespace` * `envs/funcs_enverrnoval.go:15:8: sb.WasFound undefined (type *strings.Builder has no field or method WasFound)`
88 lines
1.5 KiB
Go
88 lines
1.5 KiB
Go
package envs
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
|
|
"r00t2.io/sysutils/internal"
|
|
)
|
|
|
|
// envListToMap splits a []string of env var keypairs to a map.
|
|
func envListToMap(envs []string) (envMap map[string]string) {
|
|
|
|
var kv []string
|
|
var k, v string
|
|
|
|
envMap = make(map[string]string)
|
|
|
|
for _, ev := range envs {
|
|
kv = strings.SplitN(ev, "=", 2)
|
|
// I *think* SplitN does this for me, but...
|
|
if len(kv) == 1 {
|
|
kv = append(kv, "")
|
|
}
|
|
k = kv[0]
|
|
v = kv[1]
|
|
envMap[k] = v
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// nativizeEnvMap returns a native-typed env map from a string version.
|
|
func nativizeEnvMap(stringMap map[string]string) (envMap map[string]interface{}) {
|
|
|
|
var pathVar string = internal.GetPathEnvName()
|
|
var err error
|
|
|
|
envMap = make(map[string]interface{})
|
|
|
|
for k, v := range stringMap {
|
|
|
|
// Check for PATH/Path - we handle this uniquely.
|
|
if k == pathVar {
|
|
if envMap[k], err = GetPathEnv(); err != nil {
|
|
envMap[k] = v
|
|
err = nil
|
|
}
|
|
continue
|
|
}
|
|
|
|
// It might be...
|
|
// a float
|
|
if reMaybeFloat.MatchString(v) {
|
|
if envMap[k], err = strconv.ParseFloat(v, 64); err == nil {
|
|
continue
|
|
}
|
|
err = nil
|
|
}
|
|
|
|
// an int
|
|
if reMaybeInt.MatchString(v) {
|
|
if envMap[k], err = strconv.Atoi(v); err == nil {
|
|
continue
|
|
}
|
|
err = nil
|
|
}
|
|
|
|
// a uint
|
|
if envMap[k], err = strconv.ParseUint(v, 10, 64); err == nil {
|
|
continue
|
|
} else {
|
|
err = nil
|
|
}
|
|
|
|
// a boolean
|
|
if envMap[k], err = strconv.ParseBool(v); err == nil {
|
|
continue
|
|
} else {
|
|
err = nil
|
|
}
|
|
|
|
// ok so... guess it's a string, then.
|
|
envMap[k] = v
|
|
}
|
|
|
|
return
|
|
}
|