Documentation
¶
Overview ¶
Package envparser is a simple no-dependency library for parsing environment variables in Go.
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrName = errors.New("variable name is invalid") ErrNameExists = errors.New("variable name already exists") ErrRequired = errors.New("variable is required") ErrCreateAndRequired = errors.New( "variable can't be marked for creation and required at the same time", ) ErrValidate = errors.New("variable validation failed") ErrAccepted = errors.New("variable value not in accepted values") // ExitOnError ensures that encountered errors are printed to stderr and the // program exits with code 1. If not set, errors are returned to the caller. //nolint:gochecknoglobals ExitOnError = true // Prefix is the prefix used for environment variables. If set, all // environment variables will be prefixed with this value. For example, if // Prefix is set to "MYAPP", the environment variable "MYAPP_FOO" will be // used for the variable "FOO". If not set, no prefix will be used. // This is useful for namespacing environment variables in larger applications. //nolint:gochecknoglobals Prefix = "" )
Functions ¶
Types ¶
type Opts ¶
type Opts[T TypeConstraint] struct { // Name of the environment variable, as expected in the environment. // For example: "LOG_LEVEL". Name string // Description of the environment variable, as shown in the help message. Desc string // Default value of the environment variable, if not set in the environment. Value T // Required indicates if the environment variable is required. A required // variable that can't be found in the environment will cause an error. Required bool // Forces the creation of the variable, *if it does not exist*. This results // in setting the environment variable with `os.SetEnv()`. Create bool // Validates the parsed value. Validate func(T) error // A list of accepted values for the variable. If set, the value must be one // of the accepted values. This is a convenience for the Validate function. // If set, the Validate function is ignored. AcceptedValues []T }
Opts is a struct that defines the options for an environment variable.
type TypeConstraint ¶
TypeConstraint is a type constraint that allows only int, bool, string, and float64 types. This is used to restrict the types of the environment variables that can be parsed.
type Var ¶
type Var[T TypeConstraint] struct { // contains filtered or unexported fields }
Var is a struct that represents an environment variable. The only public method is Value(), which returns the value of the variable.
func Register ¶
func Register[T TypeConstraint](opts *Opts[T]) *Var[T]
Register a variable with the given options. Returns a pointer to the registered variable.
Click to show internal directories.
Click to hide internal directories.