goroadie

package module
v0.0.0-...-378f5f6 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2016 License: MIT Imports: 6 Imported by: 1

README

forthebadge forthebadge

goroadie

Yet another envvar parser for Go. It does everything you expect - nothing more, nothing less.

Features

This parser can load structs and nested structs of any complexity along with map[string]string and basic types like uint,int,string and bool.

goroadie does not touch existing struct members if the corresponding envvar wasn't supplied. Even in maps! Existing keys of a map won't be deleted. It is useful if you load the config from file and then try to complete its configuration with overrides from envvars.

Members' names can be overridden using the env tag: type Foo struct { Bar string ``env:"baz"`` }

Installation

Pretty standard, use the go get tool:

go get github.com/utrack/goroadie

Usage

Just make the struct and pass it! Check examples for more.

type Database struct {
    Active bool `env:"enabled"`
    Type uint
    URI string
    Config map[string]string `env:"opts"`
}

type Config struct {
    Primary Database
    Secondary Database
}

conf := Config{}

err := goroadie.Process("YOURAPP",&conf)

Variables scanned in the example:

YOURAPP_PRIMARY_ENABLED
YOURAPP_PRIMARY_TYPE
YOURAPP_PRIMARY_URI
YOURAPP_PRIMARY_OPTS_*

YOURAPP_SECONDARY_ENABLED
YOURAPP_SECONDARY_TYPE
YOURAPP_SECONDARY_URI
YOURAPP_SECONDARY_OPTS_*

If any config map was initialized and loaded before it won't lose any keys (only replaced): check the examples for more.

Caveats and TODOs

  • Currently it does not play nicely with pointer members, they're ignored at the moment.
  • Only map[string]string map type is supported atm; generic map loader is planned.

Documentation

Overview

Package goroadie provides the decoder of environment variables to Go structs.

It can be used on config loading stage to override previously loaded settings (or load the full config altogether).

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Process

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

Process populates the struct with data from environment variables. Accepts this struct's common prefix and the struct itself.

Example

This example shows the basic usage.

It shows how to parse basic structs and use the tag overrides.

package main

import (
	"fmt"
	"os"

	"github.com/utrack/goroadie"
)

func main() {
	type Config struct {
		URI    string
		Type   string
		Active bool `env:"enabled"`
	}

	// Wireup variables
	// You can use prefixes for structs.
	os.Setenv("APP_URI", "blank://")
	os.Setenv("APP_TYPE", "Your Type")

	// Use tag "env" to change the envvar name.
	os.Setenv("APP_ENABLED", "true")

	conf := Config{}

	// Remember to pass the pointer!
	err := goroadie.Process("app", &conf)

	if err != nil {
		panic(err)
	}

	fmt.Println(conf)
}
Output:

{blank:// Your Type true}
Example (Advanced)

This example shows the advanced usage.

It shows how to parse basic structs and nested structs with env tags.

package main

import (
	"fmt"
	"os"

	"github.com/utrack/goroadie"
)

func main() {

	type BarType struct {
		C uint `env:"baz"`
	}

	type FooType struct {
		A string
		B int

		Bar BarType

		BazField string
	}

	// Wireup variables
	// You can use prefixes for structs
	os.Setenv("PREF_A", "VarA")
	os.Setenv("PREF_B", "2")

	// Use tag "env" to change the envvar name.
	// Previous prefixes are applied, so for BarType's C it becomes
	// pref (common prefix) -> bar (field name in FooType) -> baz (field name from tag).
	os.Setenv("PREF_BAR_BAZ", "2")

	os.Setenv("PREF_BAZFIELD", "BazField")

	conf := FooType{}

	// Remember to pass the pointer!
	err := goroadie.Process("pref", &conf)

	if err != nil {
		panic(err)
	}

	fmt.Println(conf)
}
Output:

{VarA 2 {2} BazField}
Example (Map)

This example shows the usage with map[string]string included in the struct.

package main

import (
	"fmt"
	"os"

	"github.com/utrack/goroadie"
)

func main() {
	type T struct {
		A bool              `env:"somebool"`
		B map[string]string `env:"dict"`
	}

	// Wire the vars up
	os.Setenv("SOMEBOOL", "true")
	// Case of the map keys is saved, capitalize as you want.
	os.Setenv("DICT_Foo", "bar")
	os.Setenv("DICT_BaR", "baz")
	os.Setenv("DICT_qux", "whatever")

	t := T{}
	// Init the map and load some values in it - existing values
	// will be saved!
	t.B = map[string]string{"Foo": "prevFoo", "SomeVar": "wow"}

	err := goroadie.Process("", &t)
	if err != nil {
		panic(err)
	}

	// Existing values are saved
	fmt.Printf("Key SomeVar: %v\n", t.B["SomeVar"])

	// ... but replaced if there's an envvar matching it
	fmt.Printf("Key Foo: %v\n", t.B["Foo"])

	// New values are loaded as they should.
	fmt.Printf("Key BaR: %v\n", t.B["BaR"])
	fmt.Printf("Key qux: %v\n", t.B["qux"])

	fmt.Printf("Member A: %v\n", t.A)
}
Output:

Key SomeVar: wow
Key Foo: bar
Key BaR: baz
Key qux: whatever
Member A: true

Types

This section is empty.

Jump to

Keyboard shortcuts

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