envconf/main.go

151 lines
3.3 KiB
Go
Raw 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 11:57:09 +00:00
"errors"
2021-03-23 13:37:00 +00:00
"time"
2021-03-23 11:57:09 +00:00
"fmt"
2021-03-23 09:13:25 +00:00
"os")
2021-03-23 08:48:02 +00:00
2021-03-23 10:40:25 +00:00
type cEntry struct {
value string
2021-03-23 11:57:09 +00:00
parsed cValue
2021-03-23 10:40:25 +00:00
dtype DataType
2021-03-23 11:57:09 +00:00
unset bool
empty bool
2021-03-23 10:40:25 +00:00
}
2021-03-23 11:57:09 +00:00
type Config struct {
parsed bool
env map[string]cEntry
2021-03-23 10:08:26 +00:00
}
2021-03-23 09:13:25 +00:00
func NewConfig()(*Config) {
config := new(Config)
2021-03-23 11:57:09 +00:00
config.parsed = false
2021-03-23 10:40:25 +00:00
config.env = make(map[string]cEntry)
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 18:44:53 +00:00
key := strings.TrimSpace(strings.ToUpper(splitted[0]))
2021-03-23 10:08:26 +00:00
if unicode.IsLetter(getFirstRune(key)) {
2021-03-23 10:40:25 +00:00
var entry cEntry
entry.value = splitted[1]
entry.dtype = TypeNone
2021-03-23 11:57:09 +00:00
entry.unset = false
entry.empty = false
2021-03-23 10:40:25 +00:00
config.env[key] = entry
2021-03-23 10:08:26 +00:00
}
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
}
2021-03-23 10:40:25 +00:00
func (c *Config) Define(key string, dtype DataType) {
2021-03-23 18:44:53 +00:00
upper := strings.ToUpper(key)
entry, ok := c.env[upper]
2021-03-23 10:40:25 +00:00
if ok {
entry.dtype = dtype
} else {
var entry cEntry
2021-03-23 11:57:09 +00:00
entry.dtype = dtype
entry.unset = true
entry.empty = true
2021-03-23 18:44:53 +00:00
c.env[upper] = entry
2021-03-23 10:40:25 +00:00
}
}
2021-03-23 11:57:09 +00:00
func (c *Config) DefineDefault(key string, val string, dtype DataType) {
2021-03-23 18:44:53 +00:00
upper := strings.ToUpper(key)
entry, ok := c.env[upper]
2021-03-23 11:57:09 +00:00
if ok {
if entry.unset {
entry.empty = false
entry.value = val
entry.dtype = dtype
}
} else {
var entry cEntry
entry.dtype = dtype
entry.unset = true
entry.empty = false
2021-03-23 18:44:53 +00:00
c.env[upper] = entry
2021-03-23 11:57:09 +00:00
}
}
func (c *Config) Parse() {
2021-03-23 12:05:27 +00:00
c.parsed = true
2021-03-23 11:57:09 +00:00
for k,v := range c.env {
2021-03-23 13:37:00 +00:00
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))
2021-03-23 12:52:30 +00:00
c.env[k] = v
2021-03-23 11:57:09 +00:00
}
2021-03-23 13:37:00 +00:00
} else {
v.parsed = v.dtype.parse(k, v.value)
c.env[k] = v
2021-03-23 11:57:09 +00:00
}
}
}
2021-03-23 13:37:00 +00:00
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)
}
}
2021-03-23 12:05:27 +00:00
}
2021-03-23 11:57:09 +00:00
return
}
2021-03-23 13:37:00 +00:00
func (c *Config) GetInt(key string)(int64) {
if c.parsed {
2021-03-23 18:44:53 +00:00
upper := strings.ToUpper(key)
entry, ok := c.env[upper]
2021-03-23 13:37:00 +00:00
if ok {
return entry.parsed.intval
}
}
return 0
}
func (c *Config) GetString(key string)(string) {
if c.parsed {
2021-03-23 18:44:53 +00:00
upper := strings.ToUpper(key)
entry, ok := c.env[upper]
2021-03-23 13:37:00 +00:00
if ok {
return entry.parsed.strval
}
}
return ""
}
func (c *Config) GetDuration(key string)(time.Duration) {
if c.parsed {
2021-03-23 18:44:53 +00:00
upper := strings.ToUpper(key)
entry, ok := c.env[upper]
2021-03-23 13:37:00 +00:00
if ok {
return entry.parsed.durval
}
}
return time.Duration(0)
}
func (c *Config) GetBool(key string)(bool) {
if c.parsed {
2021-03-23 18:44:53 +00:00
upper := strings.ToUpper(key)
entry, ok := c.env[upper]
2021-03-23 13:37:00 +00:00
if ok {
return entry.parsed.boolval
}
}
return false
2021-03-23 11:57:09 +00:00
}
func getFirstRune(str string)(rune) {
for _,v := range str {
return v
}
return rune(0)
}