cfger/README.md

63 lines
No EOL
1 KiB
Markdown

# cfger
*A basic configuration loading system*
---
[![Go Reference](https://pkg.go.dev/badge/go.akpain.net/cfger.svg)](https://pkg.go.dev/go.akpain.net/cfger)
## Install
```
go get go.akpain.net/cfger
```
## Example usage
```go
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.