Created
July 19, 2023 16:11
-
-
Save gwerbin/8b3625b91a448e1b83f6bbc68a35523a to your computer and use it in GitHub Desktop.
Quoting "dotenv" files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dotenv list --format=shell |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env -S sed -Ef | |
# Delete blank and commented-out lines. | |
/^[[:space:]]*(#|$)/ d | |
# Replace single quotes with the "unquote-escape-quote" trick: | |
# 1'2'3 → '1'\''2'\''3' | |
# i.e. the concatenation of: | |
# '1' \' '2' \' '3' | |
s/'/'\\''/g | |
# Wrap the right-hand side of each key=value line with single quotes. | |
s/=(.*)/='\1'/g |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env zsh | |
zmodload zsh/regex | |
setopt err_exit no_unset warn_create_global warn_nested_var re_match_pcre | |
function _quote_dotenv { | |
local MATCH MBEGIN MEND # created by =~ | |
local -a match mbegin mend # created by =~ | |
local line | |
while read -r line; do | |
if [[ ! "$line" =~ '^[[:space:]]*(#|$)' ]]; then | |
print -r -- "${line%%=*}=${(q)${line#*=}}" | |
fi | |
done | |
} | |
if (( $# > 0 )); then | |
for arg in "${@}"; do | |
_quote_dotenv < "$arg" | |
done | |
else | |
_quote_dotenv | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment