Documentation
¶
Overview ¶
Package enve handles configuration through environment variables. Every primitive type is handled here, and many other types are available in the parse library.
Usage:
// lookup CLIENT_TIMEOUT and parse it as a duration, returning 5*time.Second if it's missing or there's an error. var ClientTimeout = DurationOr("CLIENT_TIMEOUT", 5*time.Second) // get the environment variable "GITHUB_AUTH_TOKEN", panicking if it's missing. var authToken = MustString("GITHUB_AUTH_TOKEN")
Index ¶
- func BoolOr(key string, backup bool) bool
- func DurationOr(key string, backup time.Duration) time.Duration
- func FromTextOr[T any, PT interface{ ... }](key string, backup T) T
- func Int16Or(key string, fallback int16) int16
- func Int32Or(key string, fallback int32) int32
- func Int64Or(key string, fallback int64) int64
- func Int8Or(key string, fallback int8) int8
- func IntOr(key string, fallback int) int
- func LookupParse[T any](parse func(string) (T, error), key string) (T, error)
- func MustBool(key string) bool
- func MustDuration(key string) time.Duration
- func MustInt(key string) int
- func MustInt16(key string) int16
- func MustInt32(key string) int32
- func MustInt64(key string) int64
- func MustInt8(key string) int8
- func MustParse[T any](parse func(string) (T, error), key string) T
- func MustString(key string) string
- func MustTimeRFC3339(key string) time.Time
- func MustUInt(key string) uint
- func MustUInt16(key string) uint16
- func MustUInt32(key string) uint32
- func MustUInt64(key string) uint64
- func MustUInt8(key string) uint8
- func MustUint(key string) uint
- func MustUint16(key string) uint16
- func MustUint32(key string) uint32
- func MustUint64(key string) uint64
- func MustUint8(key string) uint8
- func ParseOr[T any](parse func(string) (T, error), key string, backup T) T
- func PortOr[T constraints.Signed](key string, backup uint16) uint16
- func SetFromText(key string, t encoding.TextMarshaler) error
- func SetInt[T constraints.Integer](key string, t T) error
- func StringOr(key, backup string) string
- func TimeRFC3339OrDefault(key string, backup time.Time) time.Time
- type MissingKeyError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BoolOr ¶
BoolOr returns the boolean value represented by the environment variable. It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False. Any other value will return backup.
func DurationOr ¶
MustTimeRFC3339 looks up and parses the environment var key as though using
func FromTextOr ¶
func FromTextOr[T any, PT interface { *T encoding.TextUnmarshaler }](key string, backup T, ) T
GetFromText gets the encoding.TextUnmarshaler if present and unmarshable.
func LookupParse ¶
LookupParse gets the env var specified by key and parses it as a T using parse. A missing value will return a MissingKeyError. A failure to parse will be a ParseError. It will Unwrap() to the error that parse returned.
func MustBool ¶
MustBool looks up the envvar, parsing as with parse.Bool, panicking on a missing value or error parsing.
func MustDuration ¶
MustDuration looks up the envvar, parsing as with parse.Duration, panicking on a missing value or error parsing.
func MustParse ¶
MustParse looks up and parses a [T] using the provided function, panicking on a missing value or error parsing.
func MustString ¶
MustParse looks up the envvar, panicking on a missing value.
func MustTimeRFC3339 ¶
MustTimeRFC3339 looks up and parses the environment var key as a RFC3339 datetime, panicking on a missing value or failed parse.
func MustUInt16 ¶ added in v0.1.2
MustUint is an alias for MustParse(parse.Uint, key).
func MustUInt32 ¶ added in v0.1.2
func MustUInt64 ¶ added in v0.1.2
func MustUint16 ¶
func MustUint32 ¶
func MustUint64 ¶
func ParseOr ¶
ParseOr looks up the environment var key and parses the value using parse(). An error or missing value returns backup.
func SetFromText ¶
func SetFromText(key string, t encoding.TextMarshaler) error
Set the environment variable key to the value of t.MarshalText().
func SetInt ¶
func SetInt[T constraints.Integer](key string, t T) error
SetInt sets the environment variable 'key' to the value of fmt.Sprint(t)
Types ¶
type MissingKeyError ¶
type MissingKeyError string
MissingKeyError is returned from the Lookup series of functions (e.g, LookupParse) on a missing environment variable.
func (MissingKeyError) Error ¶
func (err MissingKeyError) Error() string