Golang library for managing configuration from environment variables
Basic example (in examples folder):
package main
import (
"fmt"
"github.com/douglasmakey/envi"
)
type environments struct {
Intent int `env:"INTENT"`
Ports []int `env:"PORTS" envDefault:"3000"`
IsProd bool `env:"PROD,required"`
IsDev bool `env:"DEV"`
Hosts []string `env:"HOSTS" envSeparator:":"`
Sector map[string]int `env:"SECTOR"`
}
func main() {
env := environments{}
err := envi.Parse(&env)
if err != nil {
// You can handle the errors as follows
if e, ok := err.(*envi.EnvError); ok {
switch e.Err {
case envi.IsRequired:
// You can get info details using
// e.KeyName --> Name of key
// e.Value --> Value
panic(e.Error())
}
}
fmt.Println(err)
}
fmt.Printf("%+v\n", env)
}You can run it like this:
$ INTENT=5 PROD=true HOSTS="127.0.0.1:localhost" SECTOR="a:1,b:2,c:4" go run examples/examples.go
Intent:5 Ports:[3 0 0 0] IsProd:true IsDev:false Hosts:[127.0.0.1 localhost] Sector:map[a:1 b:2 c:4]}This library has support for the following types:
stringintuintint64boolfloat32float64Map[string]intMap[string]string[]string[]int[]bool[]float32[]float64
You can use the tag envDefault to add some default value, this value will be used in the
case of absence of it in the environment. If you don't do that AND the
environment variable is also not set, the zero-value
of the type will be used: empty for strings, false for bools
and 0 for ints.
By default, the values on slices type will be splitted by ,; you can change this behavior by setting the envSeparator tag.
The env tag option required for example env:"MyKey,required" can be added
to ensure that some critical environment variable is set.
- Implement errors handler
- Implement httpHandler for list env and change value.