env

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrEnvVarNotFound is returned when an environment variable is not found (os.LookupEnv error).
	ErrEnvVarNotFound = errors.New("environment variable not found")
	// ErrParseEnvVar is returned when an environment variable is found but cannot be parsed.
	ErrParseEnvVar = errors.New("error parsing environment variable")
)

Common errors for environment variable loading.

Functions

func Bool

func Bool(name string, def bool) bool

Bool is a helper for Or[bool].

func Int

func Int(name string, def int) int

Int is a helper for Or[int].

func NotEmpty

func NotEmpty(name string) bool

NotEmpty reports if name is set and not empty.

bash: [[ -n "${name-}" ]]

func OneOfOr

func OneOfOr[T any](names []string, def T, parse func(envVal string) (T, error)) T

OneOfOr grabs the first env variable found or the default value. If there is a parsing error, it is logged with slog.Debug.

func OneOfRequired

func OneOfRequired[T any](names []string, parse func(envVal string) (T, error)) (T, error)

OneOfRequired grabs the first env variable found. If none of the variables are found, an error is returned. If any of the parsed variables have a parsing error, it is returned.

func OneOfRequiredString

func OneOfRequiredString(names []string) (string, error)

OneOfRequiredString is a helper for OneOfRequired[string].

func OneOfString

func OneOfString(names []string, def string) string

OneOfString is a helper for OneOfOr[string].

func Or

func Or[T any](name string, def T, parse func(envVal string) (T, error)) T

Or grabs the env variable or the default value. If there is a parsing error, it is logged with slog.Debug.

func PathSlice

func PathSlice(name string, def []string) []string

PathSlice grabs the env variable as an array splitting on the default (OS specific) path list separator.

func Required

func Required[T any](name string, parse func(envVal string) (T, error)) (T, error)

Required grabs a required env variable. If the variable is not found, an error is returned. If there is a parsing error, it is returned.

func RequiredBool

func RequiredBool(name string) (bool, error)

RequiredBool is a helper for Required[bool].

func RequiredInt

func RequiredInt(name string) (int, error)

RequiredInt is a helper for Required[int].

func RequiredSlice

func RequiredSlice[T any](name string, sep string, parse func(envVal string) (T, error)) ([]T, error)

StrictSlice grabs the env variable as a slice or the default value. If there is a parsing error, it is logged with slog.Debug.

func RequiredString

func RequiredString(name string) (string, error)

RequiredString is a helper for Required[string].

func RequiredStringSlice

func RequiredStringSlice(name string, sep string) ([]string, error)

RequiredStringSlice is a helper for RequiredSlice[string].

func Slice

func Slice[T any](name string, def []T, sep string, parse func(envVal string) (T, error)) []T

Slice grabs the env variable as a slice or the default value. If there is a parsing error, it is logged with slog.Debug.

func StrictBool

func StrictBool(name string, def bool) (bool, error)

StrictBool is a helper for StrictOr[bool].

func StrictInt

func StrictInt(name string, def int) (int, error)

StrictInt is a helper for StrictOr[int].

func StrictOneOfOr

func StrictOneOfOr[T any](names []string, def T, parse func(envVal string) (T, error)) (T, error)

StrictOr grabs the first env variable found or the default value. If there is a parsing error, it is returned.

func StrictOr

func StrictOr[T any](name string, def T, parse func(envVal string) (T, error)) (T, error)

StrictOr grabs the env variable or the default value. If there is a parsing error, it is returned.

func StrictSlice

func StrictSlice[T any](name string, def []T, sep string, parse func(envVal string) (T, error)) ([]T, error)

StrictSlice grabs the env variable as a slice or the default value. If there is a parsing error, it is logged with slog.Debug.

func String

func String(name, def string) string

String is a helper for Or[string].

func StringSlice

func StringSlice(name string, def []string, sep string) []string

StringSlice is a helper for Slice[string].

Types

This section is empty.

Jump to

Keyboard shortcuts

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