envconfig

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2020 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package envconfig populates struct fields based on environment variable values (or anything that responds to "Lookup"). Structs declare their environment dependencies using the `env` tag with the key being the name of the environment variable, case sensitive.

type MyStruct struct {
    A string `env:"A"` // resolves A to $A
    B string `env:"B,required"` // resolves B to $B, errors if $B is unset
    C string `env:"C,default=foo"` // resolves C to $C, defaults to "foo"

    D string `env:"D,required,default=foo"` // error, cannot be required and default
    E string `env:""` // error, must specify key
}

All built-in types are supported except Func and Chan. If you need to define a custom decoder, implement Decoder:

type MyStruct struct {
    field string
}

func (v *MyStruct) EnvDecode(val string) error {
    v.field = fmt.Sprintf("PREFIX-%s", val)
    return nil
}

In the environment, slices are specified as comma-separated values:

export MYVAR="a,b,c,d" // []string{"a", "b", "c", "d"}

In the environment, maps are specified as comma-separated key:value pairs:

export MYVAR="a:b,c:d" // map[string]string{"a":"b", "c":"d"}

If you need to modify environment variable values before processing, you can specify a custom mutator:

type Config struct {
    Password `env:"PASSWORD_SECRET"`
}

func resolveSecretFunc(ctx context.Contxt, key, value string) (string, error) {
    if strings.HasPrefix(key, "secret://") {
        return secretmanager.Resolve(ctx, value) // example
    }
    return value, nil
}

var config Config
ProcessWith(&config, OsLookuper(), resolveSecretFunc)

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidMapItem     = fmt.Errorf("invalid map item")
	ErrLookuperNil        = fmt.Errorf("lookuper cannot be nil")
	ErrMissingKey         = fmt.Errorf("missing key")
	ErrMissingRequired    = fmt.Errorf("missing required value")
	ErrNotPtr             = fmt.Errorf("input must be a pointer")
	ErrNotStruct          = fmt.Errorf("input must be a struct")
	ErrPrivateField       = fmt.Errorf("cannot parse private fields")
	ErrRequiredAndDefault = fmt.Errorf("field cannot be required and have a default value")
	ErrUnknownOption      = fmt.Errorf("unknown option")
)

Functions

func Process

func Process(ctx context.Context, i interface{}) error

Process processes the struct using the environment. See ProcessWith for a more customizable version.

func ProcessWith

func ProcessWith(ctx context.Context, i interface{}, l Lookuper, fns ...MutatorFunc) error

ProcessWith processes the given interface with the given lookuper. See the package-level documentation for specific examples and behaviors.

Types

type Decoder

type Decoder interface {
	EnvDecode(val string) error
}

Decoder is an interface that custom types/fields can implement to control how decoding takes place. For example:

type MyType string

func (mt MyType) EnvDecode(val string) error {
    return "CUSTOM-"+val
}

type Lookuper

type Lookuper interface {
	// Lookup searches for the given key and returns the corresponding string
	// value. If a value is found, it returns the value and true. If a value is
	// not found, it returns the empty string and false.
	Lookup(key string) (string, bool)
}

Lookuper is an interface that provides a lookup for a string-based key.

func MapLookuper

func MapLookuper(m map[string]string) Lookuper

MapLookuper looks up environment configuration from a provided map. This is useful for testing, especially in parallel, since it does not require you to mutate the parent environment (which is stateful).

func MultiLookuper

func MultiLookuper(lookupers ...Lookuper) Lookuper

MultiLookuper wraps a collection of lookupers. It does not combine them, and lookups appear in the order in which they are provided to the initializer.

func OsLookuper

func OsLookuper() Lookuper

OsLookuper returns a lookuper that uses the environment (os.LookupEnv) to resolve values.

func PrefixLookuper added in v0.1.2

func PrefixLookuper(prefix string, l Lookuper) Lookuper

PrefixLookuper looks up environment configuration using the specified prefix. This is useful if you want all your variables to start with a particular prefix like "MY_APP_".

type MutatorFunc

type MutatorFunc func(ctx context.Context, k, v string) (string, error)

MutatorFunc is a function that mutates a given value before it is passed along for processing. This is useful if you want to mutate the environment variable value before it's converted to the proper type.

Jump to

Keyboard shortcuts

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