Git Product home page Git Product logo

go-config's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.