Skip to content

Commit

Permalink
escape some other bash-y special chars ($!)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexquick committed Sep 14, 2017
1 parent 88e7c8b commit 5d289f4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
16 changes: 12 additions & 4 deletions godotenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"strings"
)

const doubleQuoteSpecialChars = "\\\n\r\"!$`"

This comment has been minimized.

Copy link
@fakeyanss

fakeyanss Dec 4, 2023

Why $ is escape here?

when I want to set env_b with env_a like env_b=${env_a}_some_value, it will be escape and not parse correctly.


// Load will read your env file(s) and load them into ENV for this process.
//
// Call this function as close as possible to the start of your program (ideally in main)
Expand Down Expand Up @@ -297,9 +299,15 @@ func isIgnoredLine(line string) bool {
}

func doubleQuoteEscape(line string) string {
line = strings.Replace(line, `\`, `\\`, -1)
line = strings.Replace(line, "\n", `\n`, -1)
line = strings.Replace(line, "\r", `\r`, -1)
line = strings.Replace(line, `"`, `\"`, -1)
for _, c := range doubleQuoteSpecialChars {
toReplace := "\\" + string(c)
if c == '\n' {
toReplace = `\n`
}
if c == '\r' {
toReplace = `\r`
}
line = strings.Replace(line, string(c), toReplace, -1)
}
return line
}
4 changes: 2 additions & 2 deletions godotenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,8 @@ func TestWrite(t *testing.T) {
writeAndCompare(`key=va"lu"e`, `key="va\"lu\"e"`)
//but single quotes are left alone
writeAndCompare(`key=va'lu'e`, `key="va'lu'e"`)
// newlines and backslashes are escaped
writeAndCompare(`foo="ba\n\r\\r!"`, `foo="ba\n\r\\r!"`)
// newlines, backslashes, and some other special chars are escaped
writeAndCompare(`foo="$ba\n\r\\r!"`, `foo="\$ba\n\r\\r\!"`)
}

func TestRoundtrip(t *testing.T) {
Expand Down

0 comments on commit 5d289f4

Please sign in to comment.