Skip to content
This repository has been archived by the owner on Jan 12, 2022. It is now read-only.

Commit

Permalink
Handle inline comments on unquoted values
Browse files Browse the repository at this point in the history
Signed-off-by: Ulysses Souza <[email protected]>
  • Loading branch information
ulyssessouza committed Jan 11, 2022
1 parent 4122f16 commit 3ac2e93
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 16 deletions.
3 changes: 2 additions & 1 deletion fixtures/plain.env
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ OPTION_C= 3
OPTION_D =4
OPTION_E = 5
OPTION_F =
OPTION_G=
OPTION_G=
OPTION_H = my string # Inline comment
2 changes: 1 addition & 1 deletion fixtures/quoted.env
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ OPTION_E="1"
OPTION_F="2"
OPTION_G=""
OPTION_H="\n"
OPTION_I = "echo 'asd'"
OPTION_I = "echo 'asd'" # Inline comment
OPTION_J = 'first line
second line
third line
Expand Down
15 changes: 8 additions & 7 deletions godotenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func TestReadPlainEnv(t *testing.T) {
"OPTION_E": "5",
"OPTION_F": "",
"OPTION_G": "",
"OPTION_H": "my string",
}

envMap, err := Read(envFileName)
Expand Down Expand Up @@ -350,7 +351,7 @@ func TestParsing(t *testing.T) {
// parses yaml style options
parseAndCompare(t, "OPTION_A: 1", "OPTION_A", "1")

//parses yaml values with equal signs
// parses yaml values with equal signs
parseAndCompare(t, "OPTION_A: Foo=bar", "OPTION_A", "Foo=bar")

// parses non-yaml options with colons
Expand Down Expand Up @@ -399,7 +400,7 @@ func TestParsing(t *testing.T) {
parseAndCompare(t, `FOO="ba#r"`, "FOO", "ba#r")
parseAndCompare(t, "FOO='ba#r'", "FOO", "ba#r")

//newlines and backslashes should be escaped
// newlines and backslashes should be escaped
parseAndCompare(t, `FOO="bar\n\ b\az"`, "FOO", "bar\n baz")
parseAndCompare(t, `FOO="bar\\\n\ b\az"`, "FOO", "bar\\\n baz")
parseAndCompare(t, `FOO="bar\\r\ b\az"`, "FOO", "bar\\r baz")
Expand Down Expand Up @@ -483,14 +484,14 @@ func TestWrite(t *testing.T) {
t.Errorf("Expected '%v' (%v) to write as '%v', got '%v' instead.", env, envMap, expected, actual)
}
}
//just test some single lines to show the general idea
//TestRoundtrip makes most of the good assertions
// just test some single lines to show the general idea
// TestRoundtrip makes most of the good assertions

//values are always double-quoted
// values are always double-quoted
writeAndCompare(`key=value`, `key="value"`)
//double-quotes are escaped
// double-quotes are escaped
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"`)
// newlines, backslashes, and some other special chars are escaped
writeAndCompare(`foo="\n\r\\r!"`, `foo="\n\r\\r\!"`)
Expand Down
11 changes: 4 additions & 7 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,15 @@ func extractVarValue(src []byte, envMap map[string]string, lookupFn LookupFn) (v
if !isQuoted {
// unquoted value - read until new line
end := bytes.IndexFunc(src, isNewLine)
var rest []byte
var value string
if end < 0 {
value := strings.TrimRightFunc(string(src), unicode.IsSpace)
rest = nil
return expandVariables(value, envMap, lookupFn), rest, nil
value := strings.Split(string(src), "#")[0] // Remove inline comments on unquoted lines
value = strings.TrimRightFunc(value, unicode.IsSpace)
return expandVariables(value, envMap, lookupFn), nil, nil
}

value = strings.TrimRightFunc(string(src[0:end]), unicode.IsSpace)
rest = src[end:]
return expandVariables(value, envMap, lookupFn), rest, nil
return expandVariables(value, envMap, lookupFn), src[end:], nil
}

// lookup quoted string terminator
Expand Down Expand Up @@ -228,7 +226,6 @@ func isSpace(r rune) bool {
return false
}


// isNewLine reports whether the rune is a new line character
func isNewLine(r rune) bool {
return r == '\n'
Expand Down

0 comments on commit 3ac2e93

Please sign in to comment.