A basic configuration loading system for Go
cfger.go | ||
CHANGELOG | ||
go.mod | ||
go.sum | ||
LICENSE | ||
README.md | ||
types.go |
cfger
A basic configuration loading system
Install
go get go.akpain.net/cfger
Example usage
package config
import (
"cmp"
"go.akpain.net/cfger"
)
type HTTP struct {
Host string
Port int
}
type Database struct {
DSN string
}
type Config struct {
Debug bool
HTTP *HTTP
Database *Database
}
func Load() (*Config, error) {
cl := cfger.New()
if err := cl.Load("config.yml"); err != nil {
return nil, err
}
conf := &Config{
Debug: cmp.Or(cl.Get("debug").AsBool(), cl.GetEnv("DEBUG").AsBool()),
HTTP: &HTTP{
Host: cl.Get("http.host").WithDefault("127.0.0.1").AsString(),
Port: cl.Get("http.port").WithDefault(8080).AsInt(),
},
Database: &Database{
DSN: cl.Get("database.dsn").Required().AsString(),
},
}
return conf, nil
}
License
This project is licensed under the BSD Zero Clause License. See ./LICENSE
for more information.