appconfig

package module
v0.0.0-...-66072ec Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2017 License: MIT Imports: 9 Imported by: 27

README

AppConfig Build Status Coverage Status Go Report Card GoDoc

import "github.com/dmotylev/appconfig"

Documentation

See GoDoc.

Usage

Set environment variables:

APP_TIMEOUT=1h2m3s
APP_WORKERNAME=Monkey

Write other values to the file:

cat > local.conf << EOF
APP_TIMEOUT=2h2m3s
APP_NUMWORKERS=10
APP_WORKERNAME=Robot
EOF

Write code:

Populate variable from both sources:

package main

import (
	"fmt"
	"time"

	"github.com/dmotylev/appconfig"
)

func main() {
	var conf struct {
		Timeout    time.Duration
		Day        time.Time `time,format:"2006-01-02"`
		WorkerName string
		NumWorkers int
	}

	err := appconfig.Load(&conf, appconfig.FromEnv("APP_"), appconfig.FromFile("local.conf"))

	fmt.Printf("err=%v\n", err)
	fmt.Printf("timeout=%s\n", conf.Timeout)
	fmt.Printf("day=%s\n", conf.Day.Format(time.UnixDate))
	fmt.Printf("worker=%s\n", conf.WorkerName)
	fmt.Printf("workers=%d\n", conf.NumWorkers)
}

Results:

err=<nil>
timeout=1h2m3s
day=Fri Dec 13 00:00:00 UTC 2013
worker=Monkey
workers=10

Get some inspiration from tests.

License

The package available under LICENSE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(v interface{}, s ...Source) error

Load populates given struct from ordered list of sources. First "positive" lookup will be used as a value for the field.

Types

type Error

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

Error describes why it was impossible to assign a value to the struct's field.

func (*Error) Error

func (e *Error) Error() string

type Source

type Source interface {
	// Err returns the first error that was encountered by the source
	Err() error
	// Lookup returns string form value for the field and false if value ws not found
	Lookup(reflect.StructField) (string, bool)
}

Source provides values for the fields

func Err

func Err(e error) Source

Err wraps error in Source interface

func FromEnv

func FromEnv(p string) Source

FromEnv utilizes environment variables for lookup

func FromFile

func FromFile(s string) Source

FromFile uses Reader if the file exists, empty map is the source otherwise.

func FromMap

func FromMap(m map[string]string) Source

FromMap looks values in the map

func FromReader

func FromReader(reader io.Reader) Source

FromReader utilizes reader as a source for lookup. For any line matched to 'key=value' pattern it creates an entry in the internal map. Then map is used as the source for values.

Jump to

Keyboard shortcuts

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