A dotenv (.env) parser and serializer implemented as an mq module.
KEY=value,KEY="value", andKEY='value'- Optional leading
exportper line (shell-style) - Blank lines and full-line
#comments - Inline
# commentafter unquoted values (a#only starts a comment when it's the first character of the value or preceded by whitespace, soURL=http://example.com/#fragis preserved) \n,\t,\r,\",\\escapes and literal embedded newlines inside double-quoted values- Single-quoted values are taken literally — no escape processing
- Later definitions of the same key overwrite earlier ones
dotenv_stringifyto serialize a dict back to.envformat
Copy dotenv.mq to your mq module directory, or place it anywhere and reference it with -L.
cp dotenv.mq ~/.local/mq/config/mq -I raw 'import "github.com/harehare/dotenv.mq" | dotenv::dotenv_parse(.)' .env| Function | Description |
|---|---|
dotenv_parse(input) |
Parses the full contents of a .env file into a dict |
dotenv_parse_line(input) |
Parses a single KEY=VALUE line, returns [key, value] |
dotenv_stringify(dict) |
Serializes a dict into .env format |
Raises an error if a line is missing = after the key, or if a quoted value is unterminated.
Given .env:
# app config
export APP_NAME=myapp
PORT=8080
DEBUG=false
DB_URL="postgres://user:pass@localhost/db?sslmode=disable"
GREETING="line1\nline2"
URL=http://example.com/path#fragment
INLINE=foo # trailing commentmq -I raw 'import "dotenv" | dotenv::dotenv_parse(.)' -F json .env
# => {
# => "APP_NAME": "myapp",
# => "PORT": "8080",
# => "DEBUG": "false",
# => "DB_URL": "postgres://user:pass@localhost/db?sslmode=disable",
# => "GREETING": "line1\nline2",
# => "URL": "http://example.com/path#fragment",
# => "INLINE": "foo"
# => }
# Extract a single value
mq -I raw 'import "dotenv" | dotenv::dotenv_parse(.) | ."PORT"' .env
# => "8080"
# Parse a single line
mq -I null 'import "dotenv" | dotenv::dotenv_parse_line("export KEY=value")'
# => ["KEY", "value"]
# Round-trip: parse then re-stringify
mq -I raw 'import "dotenv" | dotenv::dotenv_stringify(dotenv::dotenv_parse(.))' .envRequires mq v0.6 or later.
MIT