38 lines
1.3 KiB
Markdown
38 lines
1.3 KiB
Markdown
# conflib
|
|
*yet another configuration library*
|
|
|
|
conflib is a small library to load configuration from environment variables or disk. Desired configuration is defined in a struct, then populated from a list of potential files, as well as environment variables.
|
|
|
|
Let's look at example usage:
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"log"
|
|
|
|
"git.janky.solutions/finn/conflib"
|
|
)
|
|
|
|
type Config struct {
|
|
Sample string `env:"SAMP"`
|
|
MyExample int `default:"9"`
|
|
Nested NestedConfig
|
|
}
|
|
|
|
type NestedConfig struct {
|
|
SomeVariable string
|
|
}
|
|
|
|
func main() {
|
|
config, err := conflib.Load[Config]("/etc/myapp.json", "myapp.json")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
log.Println("loaded config: ", config)
|
|
}
|
|
```
|
|
|
|
This will attempt to read `/etc/myapp.json` and `myapp.json` and unmarshal them into a `Config` struct. It will also check for environment variables that match the struct field names, unless overwridden. So the `Sample`
|
|
field will be populated from the `SAMP` environment variable due to the `env` tag on that field. `config.MyExample` will be populated from environment variable `MY_EXAMPLE`, and `config.Nested.SomeVariable` will be
|
|
populated by environment variable `NESTED_SOME_VARIABLE`. If none of the listed files are found and no environment variables match any field, an error will be returned.
|