Minimal configuration loader for Go.
Load from file (YAML, JSON), from environment variables or from command-line flags.
# config.yaml
name: value
package main
import (
"fmt"
"github.com/blakelead/lodr"
)
type MyConfig struct {
Name string
}
func main() {
var myConfig MyConfig
lodr.Load(&myConfig).File("config.yaml")
fmt.Println(myConfig.Name)
}
> go run main.go
value
It uses gopkg.in/yaml.v2
so it works exactly the same:
# config.yaml
my_app_name: value
type MyConfig struct {
Name string `yaml:"my_app_name"`
}
func main() {
var myConfig MyConfig
lodr.Load(&myConfig).File("config.yaml")
fmt.Println(myConfig.Name)
}
> go run main.go
value
type MyConfig struct {
Name string
}
func main() {
var myConfig MyConfig
lodr.Load(&myConfig).Env()
fmt.Println(myConfig.Name)
}
> NAME=new_value go run main.go
new_value
type MyConfig struct {
Name string `env:MY_APP_NAME`
}
func main() {
var myConfig MyConfig
lodr.Load(&myConfig).Env()
fmt.Println(myConfig.Name)
}
> MY_APP_NAME=value go run main.go
value
type MyConfig struct {
Name string `env:NAME`
}
func main() {
var myConfig MyConfig
opts := &lodr.EnvOptions{
Prefix: "MY_APP",
ProcessAll: false,
}
lodr.Load(&myConfig).EnvWithOptions(opts)
fmt.Println(myConfig.Name)
}
> MY_APP_NAME=value go run main.go
value
Options:
Prefix
: environment variables are all prefixed by this valueProcessAll
: if true, no need to specify tags. Names will be infered from attributes.
main.go
type MyConfig struct {
Name string `cmd:myapp.name`
}
func main() {
var myConfig MyConfig
lodr.Load(&myConfig).Cmd()
fmt.Println(myConfig.Name)
}
> go run main.go --myapp.name the_value
the_value
# config.yaml
name: mydb
db:
host: localhost
port: 8080
timeout: 10s
tls: true
package main
import (
"fmt"
"time"
"github.com/blakelead/lodr"
)
type MyConfig struct {
Name string `cmd:"name"`
DB struct {
Host string `yaml:"host" env:"DB_HOST" cmd:"db.host"`
Port int `yaml:"port" env:"DB_PORT" cmd:"db.port"`
Password string `env:"DB_PASSWORD"`
Timeout time.Duration `yaml:"timeout" env:"DB_TIMEOUT" cmd:"db.timeout"`
TLS bool `yaml:"tls" env:"DB_TLS" cmd:"db.tls"`
}
}
func main() {
var mc MyConfig
opts := &lodr.EnvOptions{
Prefix: "MY_APP",
}
c := lodr.Load(&mc).File("config.yaml").EnvWithOptions(opts).Cmd()
if c.Error != nil {
panic(c.Error)
}
fmt.Println(mc.Name)
fmt.Println(mc.DB.Password)
fmt.Printf("%s:%d\n", mc.DB.Host, mc.DB.Port)
}
> MY_APP_DB_PASSWORD=pass go run main.go --name the_db
the_db
pass
localhost:8080