confita

package module
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 19, 2018 License: MIT Imports: 9 Imported by: 0

README

Confita

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"`
  }

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"),
  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"`
}

License

The library is released under the MIT license. See LICENSE file.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Loader

type Loader struct {

	// Tag specifies the tag name used to parse
	// configuration keys and options.
	// If empty, "config" is used.
	Tag string
	// contains filtered or unexported fields
}

Loader loads configuration keys from backends and stores them is a struct.

func NewLoader

func NewLoader(backends ...backend.Backend) *Loader

NewLoader creates a Loader. If no backend is specified, the loader uses the environment.

func (*Loader) Load

func (l *Loader) Load(ctx context.Context, to interface{}) error

Load analyses all the fields of the given struct for a "config" tag and queries each backend in order for the corresponding key. The given context can be used for timeout and cancelation.

Directories

Path Synopsis
env

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL