envconf/main.go

129 lines
2.7 KiB
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 11:57:09 +00:00
"strconv"
"time"
"errors"
"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 08:48:02 +00:00
2021-03-23 11:57:09 +00:00
type cValue struct {
intval int64
durval time.Duration
strval string
err error
2021-03-23 08:48:02 +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 10:08:26 +00:00
key := strings.TrimSpace(strings.ToLower(splitted[0]))
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) {
entry, ok := c.env[key]
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 10:40:25 +00:00
c.env[key] = entry
}
}
2021-03-23 11:57:09 +00:00
func (c *Config) DefineDefault(key string, val string, dtype DataType) {
entry, ok := c.env[key]
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
c.env[key] = entry
}
}
func (c *Config) Parse() {
2021-03-23 12:05:27 +00:00
c.parsed = true
2021-03-23 11:57:09 +00:00
parsers := make(map[DataType](func(string)(cValue)))
parsers[TypeInt] = parseInt
parsers[TypeDuration] = parseDuration
for k,v := range c.env {
parser, ok := parsers[v.dtype]
if ok {
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
}
} else {
v.parsed = parser(v.value)
2021-03-23 12:52:30 +00:00
c.env[k] = v
2021-03-23 11:57:09 +00:00
}
}
}
}
2021-03-23 12:05:27 +00:00
func (c *Config) Show() {
for _,v := range c.env {
fmt.Println(v.parsed.err)
}
}
2021-03-23 11:57:09 +00:00
func parseInt(str string)(ret cValue) {
ret.intval, ret.err = strconv.ParseInt(str, 10, 64)
return
}
func parseDuration(str string)(ret cValue) {
ret.durval, ret.err = time.ParseDuration(str)
return
}
func getFirstRune(str string)(rune) {
for _,v := range str {
return v
}
return rune(0)
}