Skip to content

Commit

Permalink
fix(from env): replace special chars \n, \r, \t
Browse files Browse the repository at this point in the history
Many .env loaders replace `\n`, `\r`, `\t`. Follow the standard of properly
replacing with their literal equivalents when loading the variable for
use.
  • Loading branch information
xav-ie committed Jan 25, 2025
1 parent b42f2bd commit a7968ad
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions modules/formats/from-env.nu
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
# may be used like this: open .env | load-env
# works with quoted and unquoted .env files
def "from env" []: string -> record {
lines
lines
| split column '#' # remove comments
| get column1
| get column1
| parse "{key}={value}"
| str trim value -c '"' # unquote values
| update value {
str trim -c '"' | # unquote values
str replace -a "\\n" "\n" # replace `\n` with newline char
str replace -a "\\r" "\r" # replace `\r` with carriage return
str replace -a "\\t" "\t" # replace `\t` with tab
}
| transpose -r -d
}

0 comments on commit a7968ad

Please sign in to comment.