envconf/main.go

42 lines
894 B
Go
Raw Permalink Normal View History

2021-03-23 09:13:25 +00:00
package envconf
2021-03-23 08:48:02 +00:00
2021-03-23 09:28:16 +00:00
import ("strings"
2021-03-23 10:08:26 +00:00
"unicode"
2021-03-23 09:13:25 +00:00
"os")
2021-03-23 08:48:02 +00:00
2021-03-23 09:13:25 +00:00
const TypeNone = (1 << 1)
const TypeInt = (1 << 2)
const TypeDuration = (1 << 3)
const TypeString = (1 << 4)
2021-03-23 08:48:02 +00:00
type Config struct {
2021-03-23 09:13:25 +00:00
env map[string]map[string]int
2021-03-23 08:48:02 +00:00
}
2021-03-23 10:08:26 +00:00
func getFirstRune(str string)(rune) {
for _,v := range str {
return v
}
return rune(0)
}
2021-03-23 09:13:25 +00:00
func NewConfig()(*Config) {
config := new(Config)
2021-03-23 09:16:25 +00:00
config.env = make(map[string]map[string]int)
2021-03-23 09:13:25 +00:00
for _,v := range os.Environ() {
splitted := strings.SplitN(v, "=", 2)
if len(splitted) == 2 {
2021-03-23 10:08:26 +00:00
key := strings.TrimSpace(strings.ToLower(splitted[0]))
if unicode.IsLetter(getFirstRune(key)) {
strval := splitted[1]
val := make(map[string]int)
val[strval] = TypeNone
config.env[key] = val
}
2021-03-23 08:48:02 +00:00
}
}
2021-03-23 09:26:51 +00:00
return config
2021-03-23 08:48:02 +00:00
}