Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cristalhq/aconfig
Simple, useful and opinionated config loader.
https://github.com/cristalhq/aconfig
command-line config configuration configuration-files dependency-free environment environment-variables go golang json toml yaml
Last synced: 2 days ago
JSON representation
Simple, useful and opinionated config loader.
- Host: GitHub
- URL: https://github.com/cristalhq/aconfig
- Owner: cristalhq
- License: mit
- Created: 2020-06-26T19:43:20.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-03-08T10:29:05.000Z (8 months ago)
- Last Synced: 2024-08-06T22:14:46.738Z (3 months ago)
- Topics: command-line, config, configuration, configuration-files, dependency-free, environment, environment-variables, go, golang, json, toml, yaml
- Language: Go
- Homepage:
- Size: 256 KB
- Stars: 532
- Watchers: 9
- Forks: 31
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - aconfig - Simple, useful and opinionated config loader. (Configuration / Standard CLI)
- fucking-awesome-go - aconfig - Simple, useful and opinionated config loader. (Configuration / Standard CLI)
- awesome-go - aconfig - Simple, useful and opinionated config loader. (Configuration / Standard CLI)
- awesome-go - aconfig - Simple, useful and opinionated config loader. (Configuration / Standard CLI)
- awesome-go-extra - aconfig - 06-26T19:43:20Z|2022-07-21T21:51:20Z| (Configuration / Advanced Console UIs)
- awesome-go-with-stars - aconfig - Simple, useful and opinionated config loader. (Configuration / Standard CLI)
- awesome-go-plus - aconfig - Simple, useful and opinionated config loader. (Configuration / Standard CLI)
- awesome-go-plus - aconfig - Simple, useful and opinionated config loader. ![stars](https://img.shields.io/badge/stars-549-blue) ![forks](https://img.shields.io/badge/forks-32-blue) (Configuration / Standard CLI)
- awesome-ccamel - cristalhq/aconfig - Simple, useful and opinionated config loader. (Go)
README
# aconfig
[![build-img]][build-url]
[![pkg-img]][pkg-url]
[![version-img]][version-url]Simple, useful and opinionated config loader.
## Rationale
There are many solutions regarding configuration loading in Go. I was looking for a simple loader that is as easy to use and understand as possible. The goal was to load config from 4 places: defaults (in the code), files, environment variables, command-line flags. This library works with all of these sources.
## Features
* Simple API.
* Clean and tested code.
* Automatic fields mapping.
* Supports different sources:
* defaults in the code
* files (JSON, YAML, TOML, DotENV, HCL)
* environment variables
* command-line flags
* Dependency-free (file parsers are optional).
* Ability to walk over configuration fields.## Install
Go version 1.14+
```
go get github.com/cristalhq/aconfig
```## Example
```go
type MyConfig struct {
Port int `default:"1111" usage:"just give a number"`
Auth struct {
User string `required:"true"`
Pass string `required:"true"`
}
Pass string `default:"" env:"SECRET" flag:"sec_ret"`
}var cfg MyConfig
loader := aconfig.LoaderFor(&cfg, aconfig.Config{
// feel free to skip some steps :)
// SkipDefaults: true,
// SkipFiles: true,
// SkipEnv: true,
// SkipFlags: true,
EnvPrefix: "APP",
FlagPrefix: "app",
Files: []string{"/var/opt/myapp/config.json", "ouch.yaml"},
FileDecoders: map[string]aconfig.FileDecoder{
// from `aconfigyaml` submodule
// see submodules in repo for more formats
".yaml": aconfigyaml.New(),
},
})// IMPORTANT: define your own flags with `flagSet`
flagSet := loader.Flags()if err := loader.Load(); err != nil {
panic(err)
}// configuration fields will be loaded from (in order):
//
// 1. defaults set in structure tags (see MyConfig defenition)
// 2. loaded from files `file.json` if not `ouch.yaml` will be used
// 3. from corresponding environment variables with the prefix `APP_`
// 4. command-line flags with the prefix `app.` if they are
```Also see examples: [examples_test.go](https://github.com/cristalhq/aconfig/blob/master/example_test.go).
Integration with `spf13/cobra` [playground](https://play.golang.org/p/OsCR8qTCN0H).
## Documentation
See [these docs][pkg-url].
## License
[MIT License](LICENSE).
[build-img]: https://github.com/cristalhq/aconfig/workflows/build/badge.svg
[build-url]: https://github.com/cristalhq/aconfig/actions
[pkg-img]: https://pkg.go.dev/badge/cristalhq/aconfig
[pkg-url]: https://pkg.go.dev/github.com/cristalhq/aconfig
[version-img]: https://img.shields.io/github/v/release/cristalhq/aconfig
[version-url]: https://github.com/cristalhq/aconfig/releases