config

package module
v0.0.0-...-31b571d Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2018 License: Apache-2.0 Imports: 5 Imported by: 2

README

config

Go package to provide loosely coupled configuration values to your app.

Go Documentation

Out-of-the-box, config provides a simple passthrough for environment variables. With a little bit of code you can do custom configuration settings based on the deploy env, or arbitrarily complex implementations. In all of these cases, packages that consume configuration only need to import the config package, and not the concrete data provider.

Installation

go get github.com/efixler/config

Usage

import (
	"github.com/efixler/config"
)

cfg := config.Default()
apiHost := cfg.GetOrDefault("API_HOST", "https://api.foobar.com")

See the Godoc for details and more examples.

Documentation

Overview

Package config provides a way to to supply runtime configuration values, loosely coupled. You can use config to read the environment, or to provide an arbitrarily complex configuration mechanism, without binding your application tightly to that mechanism.

As provided, config.Default() will return a Getter that provides access to values from the environment. Implement your own Loader to mutate the environment before other packages consume configuration values, or implement a custom Getter to use some other configuration scheme.

Several packages under efixler are dependent on the config package.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetLoader

func SetLoader(cl Loader)

SetLoader : Pass a Loader that will be utilized to supply the Getter returned by Default(). The Loader function will be called upon the first call to Default() after SetLoader(). Each call to SetLoader will clear the current default Getter.

A Loader can write values into the environment and return an Enviroment() Getter, or it can return anything that implements the Getter interface. The example shows the former case.

If you never call SetLoader() (or if you pass a nil value), the Default() Getter will pass through environment variables.

SetLoader() should be called early, preferably in an init() method as close as possible to the application's entry point, to ensure that consumers get the right configuration as they are initializing.

Example
f := func(ctx context.Context) Getter {
	os.Setenv("mySpecialConfig", "happy")
	return Environment()
}
SetLoader(f)
config := Default()
fmt.Println(config.Get("mySpecialConfig"))
Output:

happy

Types

type Env

type Env struct {
}

Env is a Getter implementation that reads from the environment.

func (*Env) Get

func (e *Env) Get(key string) string

Get : Equivalent to os.Getenv(key). Note that other Get-ish methods in Env call Env.Get() (and not os.Getenv)

func (*Env) GetOrDefault

func (e *Env) GetOrDefault(key string, dflt string) string

GetOrDefault : If the requested key is not present or empty, return the dflt.

func (*Env) GetStrings

func (e *Env) GetStrings(key string) []string

GetStrings will treat a comma-delimited config value as an []string, stripping whitespace around the commas.

func (*Env) MustGet

func (e *Env) MustGet(key string) string

MustGet will panic if the key is not present or empty. Use this only when you really must get.

type Getter

type Getter interface {
	Get(string) string
	GetOrDefault(string, string) string
	GetStrings(string) []string
	MustGet(string) string
}

Getter : Core interface for implementations providing configuration data to consumers.

func Default

func Default() Getter

Default : Return the default configuration.

func Environment

func Environment() Getter

Environment : Return a new Env Getter.

type Loader

type Loader func(context.Context) Getter

Loader is a callback function that used to delegate configuration loading. Loader is expected to return a Getter that will be used by consumers to access configuration values. If the Loader copies configuration values into the environment, it can return a standard environment getter using Environment(). See the SetLoader() example.

The Context argument is defined to provide a hook for per-request mutated configs (which is not yet implemented). Calls to the Loader function can expect to receive a nil Context.

Jump to

Keyboard shortcuts

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