Documentation
¶
Overview ¶
Package envloader assists in writing executables that load their config from the environment.
Index ¶
- func Optional() bool
- func PrintError(e *Error, w io.Writer)
- func Required() bool
- func WhenFalse(boolVar *bool) func() bool
- func WhenTrue(boolVar *bool) func() bool
- type Bool
- type Duration
- type Error
- type Int
- type Int64
- type InvalidValue
- type String
- type Value
- type Var
- type VarSet
- func (vars VarSet) Parse()
- func (vars VarSet) Print()
- func (vars VarSet) PrintAction() flag.Value
- func (vars VarSet) PrintTo(out io.Writer)
- func (vars VarSet) String() string
- func (vars VarSet) TryParse() *Error
- func (vars VarSet) TryParseFrom(getenv func(string) string) *Error
- func (vars *VarSet) Var(envKey string, required func() bool, value flag.Value, desc string) *Var
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Optional ¶
func Optional() bool
Optional is a convenient value to pass to VarSet.Add for variables that are always optional.
func PrintError ¶
PrintError performs default printing of the given error returned by TryParse.
func Required ¶
func Required() bool
Required is a convenient value to pass to VarSet.Add for variables that are always required.
Types ¶
type Error ¶
type Error struct {
InvalidValues []*InvalidValue
MissingVars VarSet
}
Error describes environment variable problems encountered by TryParse.
type InvalidValue ¶
InvalidValue is an error returned as part of Error struct for environment variable values that failed to parse.
func (*InvalidValue) Error ¶
func (e *InvalidValue) Error() string
func (*InvalidValue) Unwrap ¶
func (e *InvalidValue) Unwrap() error
type Var ¶
type Var struct {
EnvKey string
Required func() bool
Value flag.Value
Desc string
IsSpecified bool
}
Var defines a single environment variable.
type VarSet ¶
type VarSet []*Var
VarSet is a slice of environment variable definitions. The ordering matters, both when printing the values (obviously), and also when parsing, because later variables can refer to the values of prior ones.
func (VarSet) Parse ¶
func (vars VarSet) Parse()
Parse parses the current environment variable values. If parsing fails, prints an error message and exits the program with error code 2.
func (VarSet) Print ¶
func (vars VarSet) Print()
Print prints a shell script that defines all variables in the set to os.Stdout. Variable descriptions are added as comments.
func (VarSet) PrintAction ¶
PrintAction returns flag.Value that can be used with flag.Var to print all environment variables in shell format.
Use like this:
flag.Var(vars.PrintAction(), "print-env", "print all supported environment variables in shell format")
func (VarSet) PrintTo ¶
PrintTo prints a shell script that defines all variables in the set. Variable descriptions are added as comments.
func (VarSet) String ¶
String returns a shell script that defines all variables in the set. Variable descriptions are added as comments.
func (VarSet) TryParse ¶
TryParse parses the current environment variable values. Returns nil when successful, a pointer to Error when not.
func (VarSet) TryParseFrom ¶
TryParseFrom parses environment variable values returned by the given function. Returns nil when successful, a pointer to Error when not.
func (*VarSet) Var ¶
Var adds a given value to the set of environment variable definitions.
Required func specifies the conditions when the value is required. Very often, a feature is enabled by a master on/off variable, and a bunch of configuration parameters are only required when it is on. Use Required, Optional, WhenTrue and WhenFalse helpers, or define your own custom function. These functions can refer to the values of the previously defined variables.
Use StringVar, BoolVar, IntVar & similar helpers defined in this package to make flag.Value for your variables.