envvar

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2018 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

package envvar helps you manage environment variables. It maps environment variables to typed fields in a struct, and supports required and optional vars with defaults.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(v interface{}) error

Parse parses environment variables into v, which must be a pointer to a struct. Because of the way envvar uses reflection, the fields in v must be exported, i.e. start with a capital letter. For each field in v, Parse will get the environment variable with the same name as the field and set the field to the value of that environment variable, converting it to the appropriate type if needed.

Parse supports two struct tags, which can be used together or separately. The struct tag `envvar` can be used to specify the name of the environment variable that corresponds to a field. If the `envvar` struct tag is not provided, the default is to look for an environment variable with the same name as the field. The struct tag `default` can be used to set the default value for a field. The default value must be a string, but will be converted to match the type of the field as needed. If the `default` struct tag is not provided, the corresponding environment variable is required, and Parse will return an error if it is not defined. When the `default` struct tag is provided, the environment variable is considered optional, and if set, the value of the environment variable will override the default value.

Parse will return an UnsetVariableError if a required environment variable was not set. It will also return an error if there was a problem converting environment variable values to the proper type or setting the fields of v.

If a field of v implements the encoding.TextUnmarshaler interface, Parse will call the UnmarshalText method on the field in order to set its value.

func ParseWithConfig added in v1.1.0

func ParseWithConfig(v interface{}, config Config) error

ParseWithConfig allows the call to Parse() with custom configurations.

Types

type Config added in v1.1.0

type Config struct {
	// Getenv is a custom function to retrieve envvars with.
	Getenv func(key string) (value string, found bool)
}

Config is used to control the parsing behavior of the go-envvar package.

type ErrorList added in v0.2.0

type ErrorList struct {
	Errors []error
}

ErrorList is list of independent errors raised by Parse

func (ErrorList) Error added in v0.2.0

func (e ErrorList) Error() string

type GetenvFn added in v1.1.0

type GetenvFn func(key string) (value string, found bool)

GetenvFn is a custom function to retrieve envvars.

given a key, it returns (value, true) if a given envvar exists, ("", false) otherwise. syscall.Getenv should satisfy this type signature.

type InvalidArgumentError added in v0.2.0

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

InvalidArgumentError is raised when an invalid argument passed.

func (InvalidArgumentError) Error added in v0.2.0

func (e InvalidArgumentError) Error() string

type InvalidFieldError added in v1.0.0

type InvalidFieldError struct {
	Name    string
	Message string
}

InvalidFieldError is returned by Parse whenever a given struct field is unsupported.

func (InvalidFieldError) Error added in v1.0.0

func (e InvalidFieldError) Error() string

type InvalidVariableError added in v0.2.0

type InvalidVariableError struct {
	VarName  string
	VarValue string
	// contains filtered or unexported fields
}

InvalidVariableError is returned when a given env var cannot be parsed to a given struct field.

func (InvalidVariableError) Error added in v0.2.0

func (e InvalidVariableError) Error() string

Error satisfies the error interface

type UnsetVariableError

type UnsetVariableError struct {
	// VarName is the name of the required environment variable that was not set
	VarName string
}

UnsetVariableError is returned by Parse whenever a required environment variable is not set.

func (UnsetVariableError) Error

func (e UnsetVariableError) Error() string

Error satisfies the error interface

Jump to

Keyboard shortcuts

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