Documentation ¶
Overview ¶
Package envcfg provides helpers for reading values from environment variables (or a map[string]string), converting them to Go types, and setting their values to fields on a user-defined struct.
Example ¶
package main import ( "database/sql" "fmt" "os" "time" _ "github.com/lib/pq" "github.com/nav-inc/envcfg" ) func main() { // In a real app, these would already be set by your environment. os.Setenv("BAR", "321") os.Setenv("DATABASE_URL", "postgres://postgres@/my_app?sslmode=disable") type myAppConfig struct { Foo string `env:"FOO" default:"hey there"` Bar int `env:"BAR"` DB *sql.DB `env:"DATABASE_URL"` RefreshInterval time.Duration `env:"REFRESH_INTERVAL" default:"2h30m"` } // envcfg has built in support for many of Go's built in types, but not *sql.DB, so we'll have to // register our own parser. A parser func takes a string and returns the type matching your // struct field, and an error. err := envcfg.RegisterParser(func(s string) (*sql.DB, error) { db, err := sql.Open("postgres", s) if err != nil { return nil, err } return db, nil }) if err != nil { panic(err) } // to load config we need to instantiate our config struct and pass its pointer to envcfg.Load var conf myAppConfig err = envcfg.Load(&conf) if err != nil { fmt.Println(err) } fmt.Println("Foo", conf.Foo) fmt.Println("Bar", conf.Bar) fmt.Println("Refresh Interval", conf.RefreshInterval) }
Output: Foo hey there Bar 321 Refresh Interval 2h30m0s
Index ¶
- Variables
- func Load(c interface{}) error
- func LoadFromMap(vals map[string]string, c interface{}) error
- func MustRegisterParser(f interface{})
- func ParseBytes(s string) ([]byte, error)
- func ParseFloat32(s string) (float32, error)
- func ParseFloat64(s string) (float64, error)
- func ParseIP(s string) (net.IP, error)
- func ParseInt16(s string) (int16, error)
- func ParseInt32(s string) (int32, error)
- func ParseInt64(s string) (int64, error)
- func ParseInt8(s string) (int8, error)
- func ParseString(s string) (string, error)
- func ParseTime(s string) (time.Time, error)
- func ParseUint(s string) (uint, error)
- func ParseUint16(s string) (uint16, error)
- func ParseUint32(s string) (uint32, error)
- func ParseUint64(s string) (uint64, error)
- func ParseUint8(s string) (uint8, error)
- func RegisterParser(f interface{}) error
- type Loader
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ParseBool = strconv.ParseBool ParseInt = strconv.Atoi ParseDuration = time.ParseDuration ParseURL = url.Parse ParseMAC = net.ParseMAC ParseEmailAddress = mail.ParseAddress ParseEmailAddressList = mail.ParseAddressList ParseTemplate = template.New("").Parse )
var DefaultParsers = []interface{}{ ParseBool, ParseString, ParseInt, ParseFloat32, ParseFloat64, ParseInt8, ParseInt16, ParseInt32, ParseInt64, ParseUint, ParseUint8, ParseUint16, ParseUint32, ParseUint64, ParseDuration, ParseTime, ParseURL, ParseMAC, ParseIP, ParseEmailAddress, ParseEmailAddressList, ParseTemplate, ParseBytes, }
The default parsers are functions for converting strings into basic built-in Go types. These are exported so users can choose to start with a envcfg.Empty() and then pick and choose which of these parsers they want to register. A few types have been omitted: rune (use int32 instead), byte (use uint8 instead), uintptr, complex64, and complex128.
Functions ¶
func Load ¶
func Load(c interface{}) error
Load loads config from the environment into the provided struct.
func LoadFromMap ¶
LoadFromMap loads config from the provided map into the provided struct.
func MustRegisterParser ¶ added in v1.0.0
func MustRegisterParser(f interface{})
MustRegisterParser attempts to register the provided parser func and panics if it gets an error.
func ParseBytes ¶ added in v1.0.2
func ParseFloat32 ¶
func ParseFloat64 ¶
func ParseInt16 ¶
func ParseInt32 ¶
func ParseInt64 ¶
func ParseString ¶
func ParseUint16 ¶
func ParseUint32 ¶
func ParseUint64 ¶
func ParseUint8 ¶
func RegisterParser ¶
func RegisterParser(f interface{}) error
RegisterParser takes a func (string) (<anytype>, error) and registers it on the default loader as the parser for <anytype>.
Types ¶
type Loader ¶
type Loader struct {
// contains filtered or unexported fields
}
Loader is a helper for reading values from environment variables (or a map[string]string), converting them to Go types, and setting their values to fields on a user-provided struct.
func (*Loader) LoadFromMap ¶
LoadFromMap loads config from the provided map into the provided struct.
func (*Loader) MustRegisterParser ¶ added in v1.0.0
func (e *Loader) MustRegisterParser(f interface{})
MustRegisterParser attempts to register the provided parser func and panics if it gets an error.
func (*Loader) RegisterParser ¶
RegisterParser takes a func (string) (<anytype>, error) and registers it on the Loader as the parser for <anytype>