A minimalistic approach to spf13/viper.
- Read
ENVvariables into astruct - Read a
.envfile into astruct < 110source lines of code- No dependencies
Only string fields are supported. Allows missing env variables when marked with "omitempty"
package main
import (
"github.com/nobloat/tinyviper"
"fmt"
)
type Config struct {
UserConfig struct {
Email string `env:"MY_APP_EMAIL"`
Password string `env:"MY_APP_PASSWORD"`
someOtherProperty string
}
Endpoint string `env:"MY_APP_ENDPOINT"`
AppUrl string `env:MY_APP_URL,omitempty`
}
func main() {
cfg := Config{Endpoint: "some default endpoint"}
err := tinyviper.LoadFromResolver(&cfg, tinyviper.NewEnvResolver(), tinyviper.NewEnvFileResolver(".env.sample"))
if err != nil {
panic(err)
}
fmt.Println("%+v", cfg)
}