Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/heetch/confita
Load configuration in cascade from multiple backends into a struct
https://github.com/heetch/confita
configuration configuration-loader golang structure team-boost
Last synced: 10 days ago
JSON representation
Load configuration in cascade from multiple backends into a struct
- Host: GitHub
- URL: https://github.com/heetch/confita
- Owner: heetch
- License: mit
- Created: 2017-12-21T10:49:18.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2024-03-10T11:20:38.000Z (8 months ago)
- Last Synced: 2024-07-31T20:44:13.454Z (3 months ago)
- Topics: configuration, configuration-loader, golang, structure, team-boost
- Language: Go
- Homepage:
- Size: 156 KB
- Stars: 492
- Watchers: 24
- Forks: 51
- Open Issues: 22
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - confita - Load configuration in cascade from multiple backends into a struct. (Configuration / Standard CLI)
- fucking-awesome-go - confita - Load configuration in cascade from multiple backends into a struct. (Configuration / Standard CLI)
- awesome-go - confita - Load configuration in cascade from multiple backends into a struct. (Configuration / Standard CLI)
- awesome-go - confita - Load configuration in cascade from multiple backends into a struct. (Configuration / Standard CLI)
- awesome-go - confita - Load configuration in cascade from multiple backends into a struct - ★ 179 (Configuration)
- awesome-go-extra - confita - 12-21T10:49:18Z|2021-07-24T10:21:20Z| (Configuration / Advanced Console UIs)
- awesome-go-with-stars - confita - Load configuration in cascade from multiple backends into a struct. (Configuration / Standard CLI)
- awesome-go-plus - confita - Load configuration in cascade from multiple backends into a struct. (Configuration / Standard CLI)
- awesome-go-plus - confita - Load configuration in cascade from multiple backends into a struct. (Configuration / Standard CLI)
README
![](https://user-images.githubusercontent.com/4570448/40795774-57ad2ae2-6503-11e8-8448-328094f4091f.png)
[![Build Status](https://travis-ci.org/heetch/confita.svg?branch=master)](https://travis-ci.org/heetch/confita)
[![GoDoc](https://godoc.org/github.com/heetch/confita?status.svg)](https://godoc.org/github.com/heetch/confita)
[![Go Report Card](https://goreportcard.com/badge/github.com/heetch/confita)](https://goreportcard.com/report/github.com/heetch/confita)Confita is a library that loads configuration from multiple backends and stores it in a struct.
## Supported backends
- Environment variables
- JSON files
- Yaml files
- Toml files
- Command line flags
- [etcd](https://github.com/coreos/etcd)
- [Consul](https://www.consul.io/)
- [Vault](https://www.vaultproject.io/)
- [Amazon SSM](https://docs.aws.amazon.com/systems-manager/latest/userguide/what-is-systems-manager.html)## Install
```sh
go get -u github.com/heetch/confita
```## Usage
Confita scans a struct for `config` tags and calls all the backends one after another until the key is found.
The value is then converted into the type of the field.### Struct layout
Go primitives are supported:
```go
type Config struct {
Host string `config:"host"`
Port uint32 `config:"port"`
Timeout time.Duration `config:"timeout"`
}
```By default, all fields are optional. With the required option, if a key is not found then Confita will return an error.
```go
type Config struct {
Addr string `config:"addr,required"`
Timeout time.Duration `config:"timeout"`
}
```Nested structs are supported too:
```go
type Config struct {
Host string `config:"host"`
Port uint32 `config:"port"`
Timeout time.Duration `config:"timeout"`
Database struct {
URI string `config:"database-uri,required"`
}
}
```If a field is a slice, Confita will automatically split the config value by commas and fill the slice with each sub value.
```go
type Config struct {
Endpoints []string `config:"endpoints"`
}
```As a special case, if the field tag is "-", the field is always omitted. This is useful if you want to populate this field on your own.
```go
type Config struct {
// Field is ignored by this package.
Field float64 `config:"-"`// Confita scans any structure recursively, the "-" value prevents that.
Client http.Client `config:"-"`
}
```### Loading configuration
Creating a loader:
```go
loader := confita.NewLoader()
```By default, a Confita loader loads all the keys from the environment.
A loader can take other configured backends as parameters.```go
loader := confita.NewLoader(
env.NewBackend(),
file.NewBackend("/path/to/config.json"),
file.NewBackend("/path/to/config.yaml"),
flags.NewBackend(),
etcd.NewBackend(etcdClientv3),
consul.NewBackend(consulClient),
vault.NewBackend(vaultClient),
)
```Loading configuration:
```go
err := loader.Load(context.Background(), &cfg)
```Since loading configuration can take time when used with multiple remote backends, context can be used for timeout and cancelation:
```go
ctx, cancel := context.WithTimeout(context.Background(), 5 * time.Second)
defer cancel()
err := loader.Load(ctx, &cfg)
```### Default values
If a key is not found, Confita won't change the respective struct field. With that in mind, default values can simply be implemented by filling the structure before passing it to Confita.
```go
type Config struct {
Host string `config:"host"`
Port uint32 `config:"port"`
Timeout time.Duration `config:"timeout"`
Password string `config:"password,required"`
}// default values
cfg := Config{
Host: "127.0.0.1",
Port: "5656",
Timeout: 5 * time.Second,
}err := confita.NewLoader().Load(context.Background(), &cfg)
```### Backend option
By default, Confita queries each backend one after another until a key is found. However, in order to avoid some useless processing the `backend` option can be specified to describe in which backend this key is expected to be found.
This is especially useful when the location of the key is known beforehand.```go
type Config struct {
Host string `config:"host,backend=env"`
Port uint32 `config:"port,required,backend=etcd"`
Timeout time.Duration `config:"timeout"`
}
```### Command line flags
The `flags` backend allows to load individual configuration keys from the command line. The default values are extracted from the struct fields values.
A `short` option is also supported.
To update usage message on the command line, provide a `description` to the given field.
```go
type Config struct {
Host string `config:"host,short=h"`
Port uint32 `config:"port,short=p"`
Timeout time.Duration `config:"timeout,description=timeout (in seconds) for failure"`
}
``````sh
./bin -hUsage of ./bin:
-host string
(default "127.0.0.1")
-h string
(default "127.0.0.1")
-port int
(default 5656)
-p int
(default 5656)
-timeout duration
timeout (in seconds) for failure (default 10s)
```## License
The library is released under the MIT license. See LICENSE file.