https://github.com/krostar/config
Simple and lightweight yet powerful and modulable configuration package
https://github.com/krostar/config
config-default config-loader config-management
Last synced: 4 months ago
JSON representation
Simple and lightweight yet powerful and modulable configuration package
- Host: GitHub
- URL: https://github.com/krostar/config
- Owner: krostar
- License: mit
- Created: 2019-01-24T18:33:38.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-03-23T10:53:58.000Z (about 6 years ago)
- Last Synced: 2024-06-19T05:45:59.299Z (almost 2 years ago)
- Topics: config-default, config-loader, config-management
- Language: Go
- Homepage:
- Size: 106 KB
- Stars: 8
- Watchers: 3
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# config
[](https://godoc.org/github.com/krostar/config)
[](https://tldrlegal.com/license/mit-license)

[](https://travis-ci.org/krostar/config)
[](https://app.codacy.com/project/krostar/config/dashboard)
[](https://app.codacy.com/project/krostar/config/dashboard)
A simple yet powerful configuration package.
## Motivation
On any project I've made personnally or for a company, except if the project was really
(really) small, I always needed at some point to be able to configure a component in the
project (the http listening port, the database credentials, a business configuration, ...).
I've been using **viper** for some times now, but I was not really happy about it for some
reasons (usage of strings keys to get configuration, globally defined configuration which are
a p.i.t.a. in big project to understand what's used where, and to concurently use and modify in test, ...). I also used **confita** from which this project was inspired.
From my point of view a configuration package should:
- have a **priorization of "sources"** (for example file < env < cli args)
- be **strongly typed** (it should never use string keys to get a value, or return interface{} left for the call to cast)
- be **modulable** (add a new "source" to retrieve
configuration from vauld or consul for example)
- handle **defaults values** (without string keys, and as close as the configuration definition)
- have a **clear and easy to use API**
- be **light**
- encourage and follow the **best practices**
That's what I tried to do in this configuration package which is made of 4 components:
- the default setter which handles defaults
- the sources (anything that implements one of the two sources interfaces) responsible for
the retrieval of the configuration
- the "loader" which is responsible to set the `default` if any and to call each `sources`
- additionally, a validation can be made to make sure configuration is valid
## Usage / examples
```go
// let's define a structure that hold our http configuration, for example
type HTTPConfig struct {
Debug bool
ListenAddress string
RequestTimeout time.Duration
MACSecret []byte
}
// SetDefault sets sane default for http config.
func (c *HTTPConfig) SetDefault() {
c.ListenAddress = ":8080"
c.RequestTimeout = 3 * time.Second
}
// Validate checks whenever the config is properly set.
func (c *HTTPConfig) Validate() error {
if c.RequestTimeout < time.Second {
return errors.New("request timeout is too short (min 1s)")
}
}
func main() {
// export PREFIX_DEBUG="true"
// export PREFIX_MACSECRET="secret"
// echo "{ "listen-address": ":8082" }" > ./conf.json
var cfg HTTPConfig
if err := config.Load(&cfg, config.WithSources(config.Source{
sourcefile.New("./conf.json"),
sourceenv.New("prefix"),
})); err != nil {
panic(err)
}
if err := config.Validate(&cfg); err != nil {
panic(err)
}
// cfg.Debug = "true"
// cfg.ListenAddress = ":8082"
// cfg.RequestTimeout = "3s"
// cfg.MACSecret = "secret"
}
```
More doc and examples in the config's [godoc](https://godoc.org/github.com/krostar/config)
## License
This project is under the MIT licence, please see the LICENCE file.