forked from coop-cloud-mirrors/godotenv
escape some other bash-y special chars ($!)
This commit is contained in:
parent
88e7c8bd35
commit
5d289f4405
16
godotenv.go
16
godotenv.go
@ -24,6 +24,8 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
const doubleQuoteSpecialChars = "\\\n\r\"!$`"
|
||||
|
||||
// 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)
|
||||
@ -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
|
||||
}
|
||||
|
@ -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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user