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.Context, 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 ¶
const ( // ErrInvalidMapItem is thrown when a invalid map item was encountered ErrInvalidMapItem = Error("invalid map item") // ErrLookuperNil is an error stating that the provided `Lookuper` is nil ErrLookuperNil = Error("lookuper cannot be nil") // ErrDeciderNil is an error stating that the `DeciderFunction` is nil ErrDeciderNil = Error("decider function cannot be nil") // ErrMissingKey is returned when missing key ErrMissingKey = Error("missing key") // ErrMissingRequired is returned when a env tag with required and no // corresponding environment variable was found. ErrMissingRequired = Error("missing required value") // ErrNotPtr states that the input must be a pointer ErrNotPtr = Error("input must be a pointer") // ErrNotStruct states that the input must be a struct ErrNotStruct = Error("input must be a struct") // ErrPrefixNotStruct is an error returned when a prefix is bound to a non struct type ErrPrefixNotStruct = Error("prefix is only valid on struct types") // ErrPrivateField is returned when a env tag is set on a private field. ErrPrivateField = Error("cannot parse private fields") // ErrRequiredAndDefault is when field cannot be required and have a default value ErrRequiredAndDefault = Error("field cannot be required and have a default value") // ErrUnknownOption is when supplied an unknown option ErrUnknownOption = Error("unknown option") )
Variables ¶
var DefaultDeciderFunc = func(ctx context.Context, value reflect.Value) bool {
return value.IsZero()
}
DefaultDeciderFunc is a default implementation of DeciderFunc and will only allow sets when nil or zero value for each field.
WriteAll will always write all fields with an `env` tag and has corresponding environment value. This is the opposite of default where it only writes such when a variable is zero or nil.
Functions ¶
func Process ¶
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, d DeciderFunc, fns ...MutatorFunc) error
ProcessWith processes the given interface with the given lookuper. See the package-level documentation for specific examples and behaviors.
Types ¶
type Base64Bytes ¶
type Base64Bytes []byte
Base64Bytes is a slice of bytes where the information is base64-encoded in the environment variable.
func (*Base64Bytes) EnvDecode ¶
func (b *Base64Bytes) EnvDecode(val string) error
EnvDecode implements env.Decoder.
type DeciderFunc ¶
DeciderFunc decides if a set can take place or if it should discard the set of a specific field.
When it returns true the set will be performed, and subsequently false for not allowing the set to be performed.
For example: return value..IsZero() will only set values that has not been initialized (or is zero).
type Decoder ¶
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 HexBytes ¶
type HexBytes []byte
HexBytes is a slice of bytes where the information is hex-encoded in the environment variable.
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 ¶
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 ¶
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 ¶
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_".