enve

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 20, 2022 License: BSD-3-Clause Imports: 9 Imported by: 0

README

enve

parsing and configuration via environment variables

the big idea

If you have a required configuration value, use the Must family of functions:

var publicKey = enve.MustGetString("MY_APP_PUBLIC_KEY")

If you have a sensible default, use the Or family of functions:

// if MY_APP_PORT is set, use that, but otherwise, 8080
var port = enve.PortOr("MY_APP_PORT", 8080)

You can use custom parsing functions easily with MustParse or ParseOr:


var requiredToken []byte = enve.MustParse(base64.StdEncoding.DecodeString, "REQUIRED_TOKEN_THATS_IN_BASE64_FOR_SOME_REASON")
type Point3 struct {X, Y, Z float64}
func pointFromString(s string)(p Point3, err error) {
    _, err := fmt.Sscan(s, &p.X, &p.Y, &p.Z)
    return p, err
}
var startingPoint = enve.ParseOr(pointFromString, "STARTING_POINT", Point3{0, 0, 0})

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func BoolOr

func BoolOr(key string, backup bool) bool

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

func DurationOr(key string, backup time.Duration) time.Duration

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 Int16Or added in v0.1.2

func Int16Or(key string, fallback int16) int16

Int16Or is an alias for MustParse(parse.Int, key).

func Int32Or added in v0.1.2

func Int32Or(key string, fallback int32) int32

Int32Or is an alias for MustParse(parse.Int, key).

func Int64Or added in v0.1.2

func Int64Or(key string, fallback int64) int64

Int64Or is an alias for MustParse(parse.Int, key).

func Int8Or added in v0.1.2

func Int8Or(key string, fallback int8) int8

Int8Or is an alias for MustParse(parse.Int, key).

func IntOr

func IntOr(key string, fallback int) int

IntOr is an alias for MustParse(parse.Int, key).

func LookupParse

func LookupParse[T any](parse func(string) (T, error), key string) (T, error)

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

func MustBool(key string) bool

MustBool looks up the envvar, parsing as with parse.Bool, panicking on a missing value or error parsing.

func MustDuration

func MustDuration(key string) time.Duration

MustDuration looks up the envvar, parsing as with parse.Duration, panicking on a missing value or error parsing.

func MustInt

func MustInt(key string) int

func MustInt16

func MustInt16(key string) int16

MustInt is an alias for MustParse(parse.Int, key).

func MustInt32

func MustInt32(key string) int32

func MustInt64

func MustInt64(key string) int64

func MustInt8

func MustInt8(key string) int8

func MustParse

func MustParse[T any](parse func(string) (T, error), key string) T

MustParse looks up and parses a [T] using the provided function, panicking on a missing value or error parsing.

func MustString

func MustString(key string) string

MustParse looks up the envvar, panicking on a missing value.

func MustTimeRFC3339

func MustTimeRFC3339(key string) time.Time

MustTimeRFC3339 looks up and parses the environment var key as a RFC3339 datetime, panicking on a missing value or failed parse.

func MustUInt

func MustUInt(key string) uint

func MustUInt16 added in v0.1.2

func MustUInt16(key string) uint16

MustUint is an alias for MustParse(parse.Uint, key).

func MustUInt32 added in v0.1.2

func MustUInt32(key string) uint32

func MustUInt64 added in v0.1.2

func MustUInt64(key string) uint64

func MustUInt8 added in v0.1.2

func MustUInt8(key string) uint8

func MustUint

func MustUint(key string) uint

func MustUint16

func MustUint16(key string) uint16

func MustUint32

func MustUint32(key string) uint32

func MustUint64

func MustUint64(key string) uint64

func MustUint8

func MustUint8(key string) uint8

func ParseOr

func ParseOr[T any](parse func(string) (T, error), key string, backup T) T

ParseOr looks up the environment var key and parses the value using parse(). An error or missing value returns backup.

func PortOr

func PortOr[T constraints.Signed](key string, backup uint16) uint16

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)

func StringOr

func StringOr(key, backup string) string

StringOr returns os.Getenv(key) if it's not the empty string, or backup otherwise.

func TimeRFC3339OrDefault

func TimeRFC3339OrDefault(key string, backup time.Time) time.Time

TimeRFC3339Or gets the environment variable as a RFC3339 time.Time.

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

Directories

Path Synopsis
shared logic between stdlog, zerolog, customlog
shared logic between stdlog, zerolog, customlog

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL