All Projects → heetch → Confita

heetch / Confita

Licence: mit
Load configuration in cascade from multiple backends into a struct

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Confita

Pyhcl
HCL is a configuration language. pyhcl is a python parser for it.
Stars: ✭ 260 (-24.42%)
Mutual labels:  configuration
Dotfiles
My configuration. Minimalist, but helps save a few thousand keystrokes a day.
Stars: ✭ 284 (-17.44%)
Mutual labels:  configuration
Fiddler Plus
自定义的Fiddler规则,多环境切换、解决跨域开发、快速调试线上代码必备|高效调试分析利器
Stars: ✭ 325 (-5.52%)
Mutual labels:  configuration
Zformat
The Hoa\Zformat library.
Stars: ✭ 265 (-22.97%)
Mutual labels:  configuration
Cprop
likes properties, environments, configs, profiles..
Stars: ✭ 274 (-20.35%)
Mutual labels:  configuration
Senparc.co2net
支持 .NET Framework & .NET Core 的公共基础扩展库
Stars: ✭ 289 (-15.99%)
Mutual labels:  configuration
Tmux Powerline
A hackable statusbar for tmux consisting of dynamic & beautiful looking segments, inspired by vim-powerlline, written purely in bash.
Stars: ✭ 2,802 (+714.53%)
Mutual labels:  configuration
V2ray Step By Step
This repo is a fork of ToutyRater/v2ray-guide, we aim to provide a new step-by-step guide of v2ray
Stars: ✭ 341 (-0.87%)
Mutual labels:  configuration
Dry Configurable
A simple mixin to make Ruby classes configurable
Stars: ✭ 280 (-18.6%)
Mutual labels:  configuration
Jk
Configuration as Code with ECMAScript
Stars: ✭ 322 (-6.4%)
Mutual labels:  configuration
Tweek
Tweek - an open source feature manager
Stars: ✭ 268 (-22.09%)
Mutual labels:  configuration
Confex
Useful helper to read and use application configuration from environment variables.
Stars: ✭ 272 (-20.93%)
Mutual labels:  configuration
Duic
分布式配置中心,集中化配置管理,应用配置权限管理,配置实时更新等功能
Stars: ✭ 289 (-15.99%)
Mutual labels:  configuration
Typed Immutable
Immutable and structurally typed data
Stars: ✭ 263 (-23.55%)
Mutual labels:  structure
Admiral
Admiral provides automatic configuration generation, syncing and service discovery for multicluster Istio service mesh
Stars: ✭ 323 (-6.1%)
Mutual labels:  configuration
Construct
A PHP project/micro-package generator for PDS compliant projects or micro-packages.
Stars: ✭ 257 (-25.29%)
Mutual labels:  structure
Confuse
painless YAML config files for Python
Stars: ✭ 285 (-17.15%)
Mutual labels:  configuration
Chezmoi
Manage your dotfiles across multiple diverse machines, securely.
Stars: ✭ 5,590 (+1525%)
Mutual labels:  configuration
Config
The Config component helps you find, load, combine, autofill and validate configuration values of any kind, whatever their source may be (YAML, XML, INI files, or for instance a database).
Stars: ✭ 3,671 (+967.15%)
Mutual labels:  configuration
Hoplite
A boilerplate-free library for loading configuration files as data classes in Kotlin
Stars: ✭ 322 (-6.4%)
Mutual labels:  configuration

Build Status GoDoc Go Report Card

Confita is a library that loads configuration from multiple backends and stores it in a struct.

Supported backends

Install

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:

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.

type Config struct {
  Addr        string        `config:"addr,required"`
  Timeout     time.Duration `config:"timeout"`
}

Nested structs are supported too:

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.

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.

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:

loader := confita.NewLoader()

By default, a Confita loader loads all the keys from the environment. A loader can take other configured backends as parameters.

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:

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:

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.


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.

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.

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"`
}
./bin -h

Usage 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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].