Skip to content

Commit 3efdec6

Browse files
committed
Add env file quotes handling
Signed-off-by: Phong Tran <[email protected]>
1 parent b75c262 commit 3efdec6

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

opts/envfile_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ func TestParseEnvFileGoodFile(t *testing.T) {
3535
_foobar=foobaz
3636
with.dots=working
3737
and_underscore=working too
38+
single_quotes='quotes working'
39+
double_quotes="quotes working"
3840
`
3941
// Adding a newline + a line with pure whitespace.
4042
// This is being done like this instead of the block above
@@ -55,6 +57,8 @@ and_underscore=working too
5557
"_foobar=foobaz",
5658
"with.dots=working",
5759
"and_underscore=working too",
60+
"single_quotes=quotes working",
61+
"double_quotes=quotes working",
5862
}
5963

6064
if !reflect.DeepEqual(lines, expectedLines) {

opts/file.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import (
1010
"unicode/utf8"
1111
)
1212

13-
const whiteSpaces = " \t"
13+
const (
14+
whiteSpaces = " \t"
15+
quotes = "'\""
16+
)
1417

1518
// ErrBadKey typed error for bad environment variable
1619
type ErrBadKey struct {
@@ -58,8 +61,12 @@ func parseKeyValueFile(filename string, emptyFn func(string) (string, bool)) ([]
5861
}
5962

6063
if len(data) > 1 {
61-
// pass the value through, no trimming
62-
lines = append(lines, fmt.Sprintf("%s=%s", variable, data[1]))
64+
// pass the value through, trimming leading and trailing quotes
65+
value := data[1]
66+
value = strings.TrimLeft(value, quotes)
67+
value = strings.TrimRight(value, quotes)
68+
69+
lines = append(lines, fmt.Sprintf("%s=%s", variable, value))
6370
} else {
6471
var value string
6572
var present bool

0 commit comments

Comments
 (0)