config

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2020 License: MIT Imports: 12 Imported by: 2

README

e-TF1 go-config

Package e-TF1 go-config allows you to load your application configuration from multiple sources (dotenv, env, flags...). This package is built on top of confita

The main differences with confita are the following:

  • all the backend matched value is written on a struct (confita will only load the last backend that matched at least one value)
  • the last backend that matches the config key will override the previous match
  • we allow to override by empty string

Quick Usage

Create your own struct with your configuration values

package main

import (
	"context"
	"fmt"

	"github.com/etf1/go-config"
)

type MyConfig struct {
	Debug    bool   `config:"DEBUG"`
	HTTPAddr string `config:"HTTP_ADDR"`
	BDDPassword string `config:"BDD_PASSWORD" print:"-"` // with print:"-" it will be print as "*** Hidden value ***"
}

func New(context context.Context) *MyConfig {
	cfg := &MyConfig{
		HTTPAddr: ":8001",
		BDDPassword: "my_fake_password",
	}

	config.NewDefaultConfigLoader().LoadOrFatal(context, cfg)
	return cfg
}

func main() {
	cfg := New(context.Background())
	
	fmt.Println(config.TableString(cfg))
}

It will print something similar to

-----------------------------------
       Debug|                false|bool `config:"DEBUG"`
    HTTPAddr|                :8001|string `config:"HTTP_ADDR"`
 BDDPassword| *** Hidden value ***|string `config:"BDD_PASSWORD" print:"-"`
-----------------------------------

Loaders

The library provides a DefaultConfigLoader that loads from

  • .env (if file ./.env was found)
  • environment variables
  • flags

You can create your own loader chain

// create your own chain loader
cl := config.NewConfigLoader(
    file.NewBackend("myfile.yml"),
    flags.NewBackend(),
)
// you can even append multiple backends to it
cl.AppendBackends(
    file.NewBackend("myfile.json"),
)
// and even prepend multiple backends to it
f := ".env"
if _, err := os.Stat(f); err == nil {
    cl.PrependBackends(dotenv.NewBackend(f))
}

Custom backends

We use all built-in Confita backends and also:

  • prefix: A backend that allows to prefix configuration keys with a special value (useful when using environment variables on generic projects)

Printer

The library provides you a config table printer. A special tag print:"-" prevents config value to be printed.

-----------------------------------
       Debug|                false|bool   `config:"DEBUG"`
    HTTPAddr|                :8001|string `config:"HTTP_ADDR"`
 BDDPassword| *** Hidden value ***|string `config:"BDD_PASSWORD" print:"-"`
-----------------------------------

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TableString

func TableString(iface interface{}) string

Types

type Loader

type Loader struct {
	// contains filtered or unexported fields
}

func NewConfigLoader

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

func NewDefaultConfigLoader

func NewDefaultConfigLoader() *Loader

* Create Loader preconfigured with: * - .env file loader if file exist * - environment variable loader * - flags loader

func (*Loader) AppendBackends

func (cb *Loader) AppendBackends(backends ...backend.Backend) *Loader

func (*Loader) Load

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

func (*Loader) LoadOrFatal

func (cb *Loader) LoadOrFatal(ctx context.Context, to interface{})

func (*Loader) PrependBackends

func (cb *Loader) PrependBackends(backends ...backend.Backend) *Loader

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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