61 lines
1.2 KiB
Go
61 lines
1.2 KiB
Go
package envconf
|
|
|
|
import ("strings"
|
|
"unicode"
|
|
"os")
|
|
|
|
type DataType int
|
|
|
|
const (
|
|
TypeNone DataType = iota
|
|
TypeUnset DataType = iota
|
|
TypeInt DataType = iota
|
|
TypeDuration DataType = iota
|
|
TypeString DataType = iota
|
|
)
|
|
|
|
type cEntry struct {
|
|
value string
|
|
dtype DataType
|
|
}
|
|
|
|
type Config struct {
|
|
env map[string]cEntry
|
|
}
|
|
|
|
func getFirstRune(str string)(rune) {
|
|
for _,v := range str {
|
|
return v
|
|
}
|
|
return rune(0)
|
|
}
|
|
|
|
func NewConfig()(*Config) {
|
|
config := new(Config)
|
|
config.env = make(map[string]cEntry)
|
|
for _,v := range os.Environ() {
|
|
splitted := strings.SplitN(v, "=", 2)
|
|
if len(splitted) == 2 {
|
|
key := strings.TrimSpace(strings.ToLower(splitted[0]))
|
|
if unicode.IsLetter(getFirstRune(key)) {
|
|
var entry cEntry
|
|
entry.value = splitted[1]
|
|
entry.dtype = TypeNone
|
|
config.env[key] = entry
|
|
}
|
|
}
|
|
}
|
|
return config
|
|
}
|
|
|
|
func (c *Config) Define(key string, dtype DataType) {
|
|
entry, ok := c.env[key]
|
|
if ok {
|
|
entry.dtype = dtype
|
|
} else {
|
|
var entry cEntry
|
|
entry.dtype = TypeUnset
|
|
c.env[key] = entry
|
|
}
|
|
}
|