Giter Club home page Giter Club logo

configura's Introduction

configura

circleci

Configura will allow you to store all your configuration in environment variables, but it will also let you set some defaults.

History

If you already know the twelve-factor methodology you will be more than aware of their third point: http://12factor.net/config were they highly recommend to use environment variables to all the configs in your app. I will also add that using docker this way of storing the configuration is quite handy and some times allows you to avoid other techniques like: ansible, puppet, chef...

The idea of configura is keeping all this configuration loading easy-peasy, let me show you that I am not lying:

The most basic thing ever

package main

import (
	"log"

	"github.com/agonzalezro/configura"
)

type Config struct {
	SomeString string
}

func main() {
	c := Config{}
	err := configura.Load("TWAPP_", &c)

	if err != nil {
		log.Fatal(err)
	}
	log.Println(c.SomeString)
}

The example showed above is the simplest one that you can find. You will need to have a env var called TWAPP_SOMESTRING before launching your program or at the moment of loading the conf it will miserably fail (actually, it will just return an error, you can do whatever you want with it :)

Please, bear in mind that TWAPP_ prefix is something that you specify at loading time, but you can use whatever prefix you want, or even don't use anything.

But what else?

It supports:

  • ints
  • strings
  • bools
  • time.Durations
  • float32s & float64s

Of course, to use some of those types, your env variables should have parsable values (aka, don't set it to "Alex" when you are expecting an int) and that's all!

Example

This struct:

type Config struct {
    SomeString   string
    SomeInt      int
    SomeBool     bool
    SomeDuration time.Duration
}

Will be expecting env vars like this one (if we continue using the TWAPP_ prefix):

TWAPP_SOMESTRING=string
TWAPP_SOMEINT=1
TWAPP_SOMEBOOL=y # or true, or yes... whatever accepted by ParseBool
TWAPP_SOMEDURATION=1s # or whatever accepted by ParseDuration

More complex struct

Using struct tags you will be able to change the behaviour of the package:

  • The var name to be looked up on the system can be override using configura:"OVERRIDE".
  • In case that the var was not found on the system you will be able to set some default with: configura:",defaultvalue".
  • And finally, if you want to use both: configura:"OVERRIDE,defaultvalue".

Example:

For doing so the struct will look like this:

type Config struct {
    // Will read the value from TWAPP_SOMESTRING
    SomeString   string

    // Will read it from ANOTHERINT and if it's not there will fail
    SomeInt      int `configura:"ANOTHERINT"`

    // Will default to true reading the value from TWAPP_SOMEBOOL
    SomeBool     bool `configura:",true"`

    // Will read it from ANTOHERDURATION but defaulting to 1s
    SomeDuration time.Duration `configura:"ANOTHERDURATION,1s"`
}

If you need more help, you can check the package documentation or ping me on twitter.

configura's People

Contributors

agonzalezro avatar dahernan avatar pwaller avatar

Watchers

 avatar

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.