env

package module
v0.0.0-...-5f5a7de Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2017 License: MIT Imports: 8 Imported by: 38

README

CircleCI Go Report Card GoDoc MIT License

Package env handles environment variables in a structured manner. It uses reflection to set fields on a struct pointer passed to the main public method, Populate.

Example (from the test):

type subEnv struct {
	SubEnvRequiredString        string `env:"SUB_ENV_REQUIRED_STRING,required"`
	SubEnvOptionalString        string `env:"SUB_ENV_OPTIONAL_STRING"`
	SubEnvOptionalUint16Default uint16 `env:"SUB_ENV_OPTIONAL_UINT16_DEFAULT,default=1024"`
}

type testEnv struct {
	RequiredString string `env:"REQUIRED_STRING,required"`
	OptionalString string `env:"OPTIONAL_STRING"`
	OptionalInt    int    `env:"OPTIONAL_INT"`
	OptionalBool   bool   `env:"OPTIONAL_BOOL"`
	SubEnv         subEnv
	Struct         struct {
		StructOptionalInt int `env:"STRUCT_OPTIONAL_INT"`
	}
	OptionalStringDefault string `env:"OPTIONAL_STRING_DEFAULT,default=foo"`
}

To populate this, one would call:

testEnv := &testEnv{}
if err := env.Populate(testEnv); err != nil {
	return err
}

Documentation

Overview

Package env handles environment variables in a structured manner.

See the README at https://github.com/peter-edge/env-go/blob/master/README.md for more details.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Main

func Main(do func(interface{}) error, appEnv interface{}, decoders ...Decoder)

Main runs the common functionality needed in a go main function. appEnv will be populated and passed to do, defaultEnv can be nil if there is an error, os.Exit(1) will be called.

func Populate

func Populate(object interface{}, decoders ...Decoder) error

Populate populates an object with environment variables.

The environment has precedence over the decoders, earlier decoders have precedence over later decoders.

Types

type Decoder

type Decoder interface {
	Decode() (map[string]string, error)
}

Decoder decodes an env file.

func NewEnvFileDecoder

func NewEnvFileDecoder(reader io.Reader) Decoder

NewEnvFileDecoder returns a new Decoder that decodes an env file of the form key=value.

func NewJSONDecoder

func NewJSONDecoder(reader io.Reader) Decoder

NewJSONDecoder returns a new Decoder that decodes a JSON object.

Jump to

Keyboard shortcuts

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