envconf/envconf.go

150 lines
3.4 KiB
Go

package envconf
import ("strings"
"unicode"
"errors"
"time"
"fmt"
"os")
type cEntry struct {
value string
parsed cValue
dtype DataType
unset bool
empty bool
}
type Config struct {
parsed bool
env map[string]cEntry
}
func NewConfig()(*Config) {
config := new(Config)
config.parsed = false
config.env = make(map[string]cEntry)
for _,v := range os.Environ() {
splitted := strings.SplitN(v, "=", 2)
if len(splitted) == 2 {
key := strings.TrimSpace(strings.ToUpper(splitted[0]))
if unicode.IsLetter(getFirstRune(key)) {
var entry cEntry
entry.value = splitted[1]
entry.dtype = TypeNone
entry.unset = false
entry.empty = false
config.env[key] = entry
}
}
}
return config
}
func (c *Config) Define(key string, dtype DataType) {
upper := strings.ToUpper(key)
entry, ok := c.env[upper]
if ok {
entry.dtype = dtype
c.env[upper] = entry
} else {
var entry cEntry
entry.dtype = dtype
entry.unset = true
entry.empty = true
c.env[upper] = entry
}
}
func (c *Config) DefineDefault(key string, val string, dtype DataType) {
upper := strings.ToUpper(key)
entry, ok := c.env[upper]
if ok {
if entry.unset {
entry.value = val
}
entry.dtype = dtype
entry.empty = false
c.env[upper] = entry
} else {
var entry cEntry
entry.dtype = dtype
entry.unset = true
entry.empty = false
entry.value = val
c.env[upper] = entry
}
}
func (c *Config) Parse() {
c.parsed = true
for k,v := range c.env {
if v.empty {
if v.unset {
v.parsed.err = errors.New(fmt.Sprintf(`Environment variable "%s" not found. It should have been of type %s.`, k, v.dtype))
c.env[k] = v
}
} else {
v.parsed = v.dtype.parse(k, v.value)
c.env[k] = v
}
}
}
func (c *Config) Status()(ok bool) {
ok = c.parsed
if ok {
for _,v := range c.env {
err := v.parsed.err
if err != nil {
ok = false
fmt.Println(err)
}
}
}
return
}
func (c *Config) getRaw(key string, dtype DataType)(val cValue) {
val.dtype = TypeNone
if c.parsed {
upper := strings.ToUpper(key)
entry, ok := c.env[upper]
if ok && (entry.dtype == dtype) {
return entry.parsed
}
}
return
}
func (c *Config) GetInt(key string)(int64) {
val := c.getRaw(key, TypeInt)
return val.intval
}
func (c *Config) GetMetric(key string)(int64) {
val := c.getRaw(key, TypeMetric)
return val.intval
}
func (c *Config) GetDirectory(key string)(string) {
val := c.getRaw(key, TypeDirectory)
return val.strval
}
func (c *Config) GetString(key string)(string) {
val := c.getRaw(key, TypeString)
return val.strval
}
func (c *Config) GetDuration(key string)(time.Duration) {
val := c.getRaw(key, TypeDuration)
return val.durval
}
func (c *Config) GetBool(key string)(bool) {
val := c.getRaw(key, TypeBool)
return val.boolval
}
func getFirstRune(str string)(rune) {
for _,v := range str {
return v
}
return rune(0)
}