configura

package module
v0.0.0-...-1410754 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2015 License: Apache-2.0 Imports: 7 Imported by: 0

README

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.

Documentation

Overview

If you already know "The twelve-factor methodology" (see: http://12factor.net/) you will be 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...

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(prefix string, c interface{}) error

Load will go through all the fields defined in your struct and trying to load their config values from environemnt variables.

- The var name to be looked up on the system can be override using struct tags: `configura:"OVERRIDE"`

- The user will also be able to set some defaults int case that the variable was not found on the system: `configura:",defaultvalue"`

- Or both: `configura:"OVERRIDE,defaultvalue"`

Types

This section is empty.

Jump to

Keyboard shortcuts

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