Documentation
¶
Index ¶
- type Env
- func (e Env) GetAddress() string
- func (e Env) GetBool() bool
- func (e Env) GetDirectory() string
- func (e Env) GetDirectoryOrCreate() string
- func (e Env) GetDuration() time.Duration
- func (e Env) GetFile() string
- func (e Env) GetFileOrCreate() string
- func (e Env) GetFloat64() float64
- func (e Env) GetInt() int
- func (e Env) GetPort() int
- func (e Env) GetString() string
- func (e Env) GetURL() string
- func (e Env) GetURLPath() string
- func (e Env) WithDefault(value string) Env
- func (e Env) WithIntInRange(min, max int) Env
- func (e Env) WithOptions(opts ...string) Env
- func (e Env) WithRequired() Env
- func (e Env) WithRequiredIf(key string, values []string) Env
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Env ¶
type Env struct {
// contains filtered or unexported fields
}
Env manages and validates environment variables for an application, providing methods to fetch and parse values such as strings, integers, booleans, IP addresses, URLs, and file paths. It supports setting default values, creating directories for file paths, and enforces constraints like integer ranges or string option lists. Env methods can induce panics for validation failures or missing mandatory variables, facilitating early configuration error detection. The design encourages method chaining for concise configuration expressions.
func NewEnv ¶
NewEnv creates and returns a new instance of Env, initializing it with a given environment variable key. It fetches the corresponding value from the system's environment variables, or an empty string if the variable is not present.
func (Env) GetAddress ¶
GetAddress retrieves the string representation of an IP address from the environment variable associated with the Env instance. If the variable's value is "localhost", this exact value is returned. For other non-empty values, it parses them as IP addresses and returns the string form. If parsing fails or the value is empty, a panic is induced with appropriate error details.
func (Env) GetBool ¶
GetBool interprets the associated environment variable's value as a boolean and returns the result. It returns false if the value is an empty string. If the value cannot be interpreted as a boolean, it panics.
func (Env) GetDirectory ¶
func (Env) GetDirectoryOrCreate ¶
func (Env) GetDuration ¶
func (Env) GetFileOrCreate ¶
func (Env) GetFloat64 ¶
func (Env) GetInt ¶
GetInt retrieves an integer from the environment variable associated with the Env instance. If the variable is unset or empty, it defaults to 0. Conversion errors result in a panic.
func (Env) GetPort ¶
GetPort retrieves the port number from the environment variable associated with the Env instance. It validates that the port is within the acceptable range for TCP/UDP ports (1-65535) and if it is not, or if the value is not a valid integer, the method will panic.
func (Env) GetString ¶
GetString retrieves the string value of the environment variable associated with the Env instance. If the environment variable is not set or its value is empty, an empty string is returned.
func (Env) GetURL ¶
GetURL retrieves the URL from the associated environment variable. It ensures that the value is a valid URL format and returns it as a string. If the environment variable is not set or its value is empty, an empty string is returned. A panic occurs if the value cannot be parsed into a valid URL.
func (Env) GetURLPath ¶
GetURLPath extracts the path component from a URL present in the environment variable. It ensures the environment variable's value is a valid URL with an included path. If the environment variable is unset, empty, or contains an invalid URL, it triggers a panic.
func (Env) WithDefault ¶
WithDefault sets a default value for the environment variable if it is not already set. It allows for specifying a fallback value to be used when an environment variable is absent or empty. The method returns the Env instance with the updated value, supporting further method chaining.
func (Env) WithIntInRange ¶
WithIntInRange ensures that the integer value of the environment variable is within a specified inclusive range. If the value is outside of this range, it panics to signal an invalid configuration. It returns the same Env instance to enable method chaining.
func (Env) WithOptions ¶
WithOptions validates the environment variable's value against a set of predefined acceptable strings. If the value is not within these options and is not empty, it raises an error to signal an invalid value. This method facilitates fluent configuration by returning the same Env instance for potential additional configurations.
func (Env) WithRequired ¶
WithRequired ensures that the environment variable associated with the Env instance has a non-empty value, panicking if this is not the case. It returns the same Env instance to facilitate method chaining.
func (Env) WithRequiredIf ¶
WithRequiredIf enforces a conditional requirement on the environment variable's value associated with the Env instance, based on the value of another specified environment variable. If the specified environment variable's value matches any string within a provided list, then the Env instance must have a non-empty value; otherwise, a panic is triggered to signal that a required environment variable is missing. This method supports method chaining by returning the same Env instance for further configuration.