Documentation ¶
Overview ¶
Package envcfg is a simple and opinionated library for parsing environment variables and documenting their usage.
The central type is envcfg.Cfg, which can be constructed with envcfg.New. Cfg has a series of parse methods. Each parse method expects the name of the environment variable and a variety of optional parameters.
The optional parameters can be specified either in the primary string following the comment or with type safe options. For example, the following two invocations are functionally identical:
cfg.Int("MY_EXAMPLE_INT", envcfg.IntDefault(10), envcfg.IntBase(16), envcfg.Comment("A really cool int")) cfg.Int("MY_EXAMPLE_INT default=a base=16 | A really cool int")
After parsing, errors can be checked with `Err`:
if err := cfg.Err(); err != nil { log.Fatalf("failed loading config: %v", err) }
Describe of the config interface can also be printed (e.g. as JSON):
json.NewEncoder(os.Stdout).Encode(cfg.Describe())
Index ¶
- type BoolOpt
- type BytesOpt
- type Cfg
- func (c *Cfg) Bool(docOpts string, opts ...BoolOpt) (v bool)
- func (c *Cfg) Bytes(docOpts string, opts ...BytesOpt) (v []byte)
- func (c *Cfg) Describe() []Description
- func (c *Cfg) Duration(docOpts string, opts ...DurationOpt) (v time.Duration)
- func (c *Cfg) Err() error
- func (c *Cfg) Float(docOpts string, opts ...FloatOpt) (v float64)
- func (c *Cfg) Has(s string) bool
- func (c *Cfg) HasNot(s string) bool
- func (c *Cfg) IP(docOpts string, opts ...IPOpt) (v net.IP)
- func (c *Cfg) Int(docOpts string, opts ...IntOpt) (v int64)
- func (c *Cfg) IntSlice(docOpts string, opts ...IntSliceOpt) (v []int64)
- func (c *Cfg) Result() ([]Description, error)
- func (c *Cfg) String(docOpts string, opts ...StringOpt) (v string)
- func (c *Cfg) StringSlice(docOpts string, opts ...StringSliceOpt) (v []string)
- func (c *Cfg) Time(docOpts string, opts ...TimeOpt) (v time.Time)
- func (c *Cfg) Uint(docOpts string, opts ...UintOpt) (v uint64)
- type DefaultValDescription
- type Description
- type DurationOpt
- type FloatOpt
- type IPOpt
- type IntOpt
- type IntSliceOpt
- type Option
- type StringOpt
- type StringSliceOpt
- type TimeOpt
- type UintOpt
- type UniOpt
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BoolOpt ¶
type BoolOpt interface {
// contains filtered or unexported methods
}
BoolOpt modifies Bool variable configuration.
func BoolDefault ¶
BoolDefault specifies a default value for a Bool variable.
type BytesOpt ¶
type BytesOpt interface {
// contains filtered or unexported methods
}
BytesOpt modifies Bytes variable configuration.
func BytesDefault ¶
BytesDefault specifies a default value for a Bytes variable.
func BytesNoPadding ¶
BytesNoPadding disables padding for a Bytes variable.
func BytesPadding ¶
BytesPadding specifies an alternate padding for a Bytes variable.
func BytesURLSafe ¶
BytesURLSafe specifies the URL safe form of base64 encoding for a Bytes variable.
type Cfg ¶
type Cfg struct {
// contains filtered or unexported fields
}
func (*Cfg) Bool ¶
Bool extracts and parses a bool variable using the options provided.
The first argument must be a string with beginning with the variable name as expected in the process environment. Any other options -- none of which are required -- may either be specified in the remainder of the string or using the type-safe BoolOpts.
Available options:
- "default" or BoolDefault
- "optional" or Optional
func (*Cfg) Bytes ¶
Bytes extracts and parses a []byte variable using the options provided.
The first argument must be a string with beginning with the variable name as expected in the process environment. Any other options -- none of which are required -- may either be specified in the remainder of the string or using the type-safe BytesOpts.
Available options:
- "default" or BytesDefault
- "no_padding" or BytesNoPadding
- "optional" or Optional
- "padding" or BytesPadding
- "url_safe" or BytesURLSafe
func (*Cfg) Describe ¶
func (c *Cfg) Describe() []Description
func (*Cfg) Duration ¶
func (c *Cfg) Duration(docOpts string, opts ...DurationOpt) (v time.Duration)
Duration extracts and parses a time.Duration variable using the options provided.
The first argument must be a string with beginning with the variable name as expected in the process environment. Any other options -- none of which are required -- may either be specified in the remainder of the string or using the type-safe DurationOpts.
Available options:
- "default" or DurationDefault
- "optional" or Optional
func (*Cfg) Float ¶
Float extracts and parses a float64 variable using the options provided.
The first argument must be a string with beginning with the variable name as expected in the process environment. Any other options -- none of which are required -- may either be specified in the remainder of the string or using the type-safe FloatOpts.
Available options:
- "bit_size" or FloatBitSize
- "default" or FloatDefault
- "optional" or Optional
func (*Cfg) IP ¶
IP extracts and parses a net.IP variable using the options provided.
The first argument must be a string with beginning with the variable name as expected in the process environment. Any other options -- none of which are required -- may either be specified in the remainder of the string or using the type-safe IPOpts.
Available options:
- "default" or IPDefault
- "optional" or Optional
func (*Cfg) Int ¶
Int extracts and parses a int64 variable using the options provided.
The first argument must be a string with beginning with the variable name as expected in the process environment. Any other options -- none of which are required -- may either be specified in the remainder of the string or using the type-safe IntOpts.
Available options:
- "base" or IntBase
- "bit_size" or IntBitSize
- "default" or IntDefault
- "optional" or Optional
Example ¶
package main import ( "encoding/json" "fmt" "os" "github.com/jwilner/envcfg" ) func main() { os.Setenv("EXAMPLE_HEX_INT", "abcdef") c := envcfg.New() val := c.Int("EXAMPLE_HEX_INT base=16 default=1f | A hex int configuration value") desc, err := c.Result() fmt.Printf("%v\n", val) fmt.Printf("%v\n", err) enc := json.NewEncoder(os.Stdout) enc.SetIndent("", " ") _ = enc.Encode(desc[0]) }
Output: 11259375 <nil> { "name": "EXAMPLE_HEX_INT", "type": "int64", "optional": false, "default": 31, "params": { "base": 16 }, "comment": "A hex int configuration value" }
func (*Cfg) IntSlice ¶
func (c *Cfg) IntSlice(docOpts string, opts ...IntSliceOpt) (v []int64)
IntSlice extracts and parses a []int64 variable using the options provided.
The first argument must be a string with beginning with the variable name as expected in the process environment. Any other options -- none of which are required -- may either be specified in the remainder of the string or using the type-safe IntSliceOpts.
Available options:
- "base" or IntSliceBase
- "bit_size" or IntSliceBitSize
- "comma" or IntSliceComma
- "default" or IntSliceDefault
- "optional" or Optional
func (*Cfg) Result ¶
func (c *Cfg) Result() ([]Description, error)
func (*Cfg) String ¶
String extracts and parses a string variable using the options provided.
The first argument must be a string with beginning with the variable name as expected in the process environment. Any other options -- none of which are required -- may either be specified in the remainder of the string or using the type-safe StringOpts.
Available options:
- "default" or StringDefault
- "optional" or Optional
func (*Cfg) StringSlice ¶
func (c *Cfg) StringSlice(docOpts string, opts ...StringSliceOpt) (v []string)
StringSlice extracts and parses a []string variable using the options provided.
The first argument must be a string with beginning with the variable name as expected in the process environment. Any other options -- none of which are required -- may either be specified in the remainder of the string or using the type-safe StringSliceOpts.
Available options:
- "comma" or StringSliceComma
- "default" or StringSliceDefault
- "optional" or Optional
func (*Cfg) Time ¶
Time extracts and parses a time.Time variable using the options provided.
The first argument must be a string with beginning with the variable name as expected in the process environment. Any other options -- none of which are required -- may either be specified in the remainder of the string or using the type-safe TimeOpts.
Available options:
- "default" or TimeDefault
- "layout" or TimeLayout
- "optional" or Optional
func (*Cfg) Uint ¶
Uint extracts and parses a uint64 variable using the options provided.
The first argument must be a string with beginning with the variable name as expected in the process environment. Any other options -- none of which are required -- may either be specified in the remainder of the string or using the type-safe UintOpts.
Available options:
- "base" or UintBase
- "bit_size" or UintBitSize
- "default" or UintDefault
- "optional" or Optional
type DefaultValDescription ¶
type DefaultValDescription struct {
Value interface{}
}
func (DefaultValDescription) MarshalJSON ¶
func (d DefaultValDescription) MarshalJSON() ([]byte, error)
type Description ¶
type Description struct { Name string `json:"name"` Type string `json:"type"` Optional bool `json:"optional"` Default *DefaultValDescription `json:"default,omitempty"` Params interface{} `json:"params,omitempty"` Comment string `json:"comment,omitempty"` }
type DurationOpt ¶
type DurationOpt interface {
// contains filtered or unexported methods
}
DurationOpt modifies Duration variable configuration.
func DurationDefault ¶
func DurationDefault(def time.Duration) DurationOpt
DurationDefault specifies a default value for a Duration variable.
type FloatOpt ¶
type FloatOpt interface {
// contains filtered or unexported methods
}
FloatOpt modifies Float variable configuration.
func FloatBitSize ¶
FloatBitSize specifies the bit size to use for a Float variable.
func FloatDefault ¶
FloatDefault specifies a default value for a Float variable.
type IPOpt ¶
type IPOpt interface {
// contains filtered or unexported methods
}
IPOpt modifies IP variable configuration.
type IntOpt ¶
type IntOpt interface {
// contains filtered or unexported methods
}
IntOpt modifies Int variable configuration.
func IntBitSize ¶
IntBitSize specifies the bit size to use for a Int variable.
func IntDefault ¶
IntDefault specifies a default value for a Int variable.
type IntSliceOpt ¶
type IntSliceOpt interface {
// contains filtered or unexported methods
}
IntSliceOpt modifies IntSlice variable configuration.
func IntSliceBase ¶
func IntSliceBase(base int) IntSliceOpt
IntSliceBase specifies the base to use for a IntSlice variable.
func IntSliceBitSize ¶
func IntSliceBitSize(bitSize int) IntSliceOpt
IntSliceBitSize specifies the bit size to use for a IntSlice variable.
func IntSliceComma ¶
func IntSliceComma(comma rune) IntSliceOpt
IntSliceComma specifies the comma to use for a IntSlice variable.
func IntSliceDefault ¶
func IntSliceDefault(def []int64) IntSliceOpt
IntSliceDefault specifies a default value for a IntSlice variable.
type StringOpt ¶
type StringOpt interface {
// contains filtered or unexported methods
}
StringOpt modifies String variable configuration.
func StringDefault ¶
StringDefault specifies a default value for a String variable.
type StringSliceOpt ¶
type StringSliceOpt interface {
// contains filtered or unexported methods
}
StringSliceOpt modifies StringSlice variable configuration.
func StringSliceComma ¶
func StringSliceComma(comma rune) StringSliceOpt
StringSliceComma specifies the comma to use for a StringSlice variable.
func StringSliceDefault ¶
func StringSliceDefault(def []string) StringSliceOpt
StringSliceDefault specifies a default value for a StringSlice variable.
type TimeOpt ¶
type TimeOpt interface {
// contains filtered or unexported methods
}
TimeOpt modifies Time variable configuration.
func TimeDefault ¶
TimeDefault specifies a default value for a Time variable.
func TimeLayout ¶
TimeLayout specifies the layout to use for a Time variable.
type UintOpt ¶
type UintOpt interface {
// contains filtered or unexported methods
}
UintOpt modifies Uint variable configuration.
func UintBitSize ¶
UintBitSize specifies the bit size to use for a Uint variable.
func UintDefault ¶
UintDefault specifies a default value for a Uint variable.