Loads and strictly parses .env
file contents, sets environment variables.
- Uses a fast recursive descent parser written in V.
- Shows detailed error messages with location context.
import prantlf.dotenv { load_env }
load_env(true)!
You can install this package either from VPM or from GitHub:
v install prantlf.dotenv
v install --git https://github.com/prantlf/v-dotenv
The following types and functions are exported:
The following error will be thrown if a malformed file is loaded:
struct LoadError {
reason string
offset int
line int
column int
}
(e &LoadError) msg() string
(e &LoadError) msg_full() string
Loads and parses the contents of the file .env
from the current directory, sets environment variables. Returns true
if the file was found and processed. Returns false
early, if the file wasn't found. If the argument overwrite
is true
, existing environment variables will be overwritten with values from the .env
file, otherwise they will retain their previous values.
import prantlf.dotenv { load_env }
load_env(true)!
Loads and parses the contents of the file .env
from the user's home directory, sets environment variables. Returns true
if the file was found and processed. Returns false
early, if the file wasn't found. If the argument overwrite
is true
, existing environment variables will be overwritten with values from the .env
file, otherwise they will retain their previous values.
import prantlf.dotenv { load_user_env }
load_user_env(true)!
Loads and parses the contents of the specified file from the current directory, sets environment variables. Returns true
if the file was found and processed. Returns false
early, if the file wasn't found. If the argument overwrite
is true
, existing environment variables will be overwritten with values from the specified file, otherwise they will retain their previous values.
import prantlf.dotenv { load_env }
load_file('.env.local', true)!
For example, loading a file with the following contents:
answer
will fail with the following message, obtainable with msg
:
unexpected end encountered when parsing a property name on line 1, column 7
A longer and colourful message can ve obtained with msg_full
:
unexpected end encountered when parsing a property name:
1 | answer
| ^
The message is formatted using the error fields, for example:
LoadError {
reason string = 'unexpected end encountered when parsing a property name'
offset int = 7
line int = 1
column int = 7
}
In lieu of a formal styleguide, take care to maintain the existing coding style. Lint and test your code.
Copyright (c) 2023-2024 Ferdinand Prantl
Licensed under the MIT license.