vera:env
Environment variables and .env file loading.
import "vera:env"
Overview
Get and set environment variables, and load .env files. File loading parses KEY=VALUE lines, skipping comments (#) and blank lines. Optionally strips surrounding quotes (single or double) from values. Handles both LF and CRLF line endings.
Dependencies: vera:io, vera:memory, vera:string
Functions
env_get
function env_get(name: ptr, name_len: i64, out_buf: ptr, out_size: i64) -> i64
Get the value of an environment variable. If the value is longer than out_size, only out_size bytes are copied but the full length is still returned (the caller can detect truncation by comparing the return value to out_size).
Parameters:
name(ptr) — pointer to the variable namename_len(i64) — length of the variable nameout_buf(ptr) — buffer to write the value intoout_size(i64) — size of the output buffer
Returns: length of the value, or -1 if the variable is not set
env_set
function env_set(name: ptr, name_len: i64, value: ptr, value_len: i64) -> i32
Set an environment variable. Overwrites the value if the variable is already set.
Parameters:
name(ptr) — pointer to the variable namename_len(i64) — length of the variable namevalue(ptr) — pointer to the valuevalue_len(i64) — length of the value
Returns: 0 on success, -1 on error
env_load_file
function env_load_file(path: ptr, path_len: i64) -> i32
Load a .env file. Variables that are already set in the environment are not overwritten. The parser:
- Skips blank lines and lines starting with
# - Splits each line on the first
=character - Strips surrounding single or double quotes from values
Parameters:
path(ptr) — pointer to the file pathpath_len(i64) — length of the file path
Returns: 0 on success, -1 if the file cannot be read
env_load_file_override
function env_load_file_override(path: ptr, path_len: i64) -> i32
Load a .env file. Variables that are already set in the environment are overwritten. Otherwise identical to env_load_file.
Parameters:
path(ptr) — pointer to the file pathpath_len(i64) — length of the file path
Returns: 0 on success, -1 if the file cannot be read