escape some other bash-y special chars ($!)

This commit is contained in:
Alex Quick 2017-07-16 18:31:51 -04:00
parent 88e7c8bd35
commit 5d289f4405
2 changed files with 14 additions and 6 deletions

View File

@ -24,6 +24,8 @@ import (
"strings" "strings"
) )
const doubleQuoteSpecialChars = "\\\n\r\"!$`"
// Load will read your env file(s) and load them into ENV for this process. // 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) // 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 { func doubleQuoteEscape(line string) string {
line = strings.Replace(line, `\`, `\\`, -1) for _, c := range doubleQuoteSpecialChars {
line = strings.Replace(line, "\n", `\n`, -1) toReplace := "\\" + string(c)
line = strings.Replace(line, "\r", `\r`, -1) if c == '\n' {
line = strings.Replace(line, `"`, `\"`, -1) toReplace = `\n`
}
if c == '\r' {
toReplace = `\r`
}
line = strings.Replace(line, string(c), toReplace, -1)
}
return line return line
} }

View File

@ -346,8 +346,8 @@ func TestWrite(t *testing.T) {
writeAndCompare(`key=va"lu"e`, `key="va\"lu\"e"`) writeAndCompare(`key=va"lu"e`, `key="va\"lu\"e"`)
//but single quotes are left alone //but single quotes are left alone
writeAndCompare(`key=va'lu'e`, `key="va'lu'e"`) writeAndCompare(`key=va'lu'e`, `key="va'lu'e"`)
// newlines and backslashes are escaped // newlines, backslashes, and some other special chars are escaped
writeAndCompare(`foo="ba\n\r\\r!"`, `foo="ba\n\r\\r!"`) writeAndCompare(`foo="$ba\n\r\\r!"`, `foo="\$ba\n\r\\r\!"`)
} }
func TestRoundtrip(t *testing.T) { func TestRoundtrip(t *testing.T) {