forked from coop-cloud-mirrors/godotenv
Handle quoted hashes "properly"
Read: not prettily or robustly.
This commit is contained in:
parent
aa6e870b57
commit
74ec3a085f
23
godotenv.go
23
godotenv.go
@ -78,14 +78,27 @@ func parseLine(line string) (key string, value string, err error) {
|
|||||||
|
|
||||||
value = splitString[1]
|
value = splitString[1]
|
||||||
|
|
||||||
// ditch the comments
|
// ditch the comments (but keep quoted hashes)
|
||||||
if strings.Contains(value, "#") {
|
if strings.Contains(value, "#") {
|
||||||
segmentsBetweenHashes := strings.Split(value, "#")
|
segmentsBetweenHashes := strings.Split(value, "#")
|
||||||
value = segmentsBetweenHashes[0]
|
quotesAreOpen := false
|
||||||
// open quote in leftmost segment
|
segmentsToKeep := make([]string, 0)
|
||||||
if strings.Count(value, "\"") == 1 || strings.Count(value, "'") == 1 {
|
for _, segment := range segmentsBetweenHashes {
|
||||||
value = value + "#" + segmentsBetweenHashes[1]
|
if strings.Count(segment, "\"") == 1 || strings.Count(segment, "'") == 1 {
|
||||||
|
if quotesAreOpen {
|
||||||
|
quotesAreOpen = false
|
||||||
|
segmentsToKeep = append(segmentsToKeep, segment)
|
||||||
|
} else {
|
||||||
|
quotesAreOpen = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(segmentsToKeep) == 0 || quotesAreOpen {
|
||||||
|
segmentsToKeep = append(segmentsToKeep, segment)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
value = strings.Join(segmentsToKeep, "#")
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if we've got quoted values
|
// check if we've got quoted values
|
||||||
|
@ -90,6 +90,7 @@ func TestParsing(t *testing.T) {
|
|||||||
// expect(env('foo="bar#baz" # comment')).to eql('foo' => 'bar#baz')
|
// expect(env('foo="bar#baz" # comment')).to eql('foo' => 'bar#baz')
|
||||||
parseAndCompare(t, "FOO=\"bar#baz\" # comment", "FOO", "bar#baz")
|
parseAndCompare(t, "FOO=\"bar#baz\" # comment", "FOO", "bar#baz")
|
||||||
parseAndCompare(t, "FOO='bar#baz' # comment", "FOO", "bar#baz")
|
parseAndCompare(t, "FOO='bar#baz' # comment", "FOO", "bar#baz")
|
||||||
|
parseAndCompare(t, "FOO=\"bar#baz#bang\" # comment", "FOO", "bar#baz#bang")
|
||||||
|
|
||||||
// it 'ignores comment lines' do
|
// it 'ignores comment lines' do
|
||||||
// expect(env("\n\n\n # HERE GOES FOO \nfoo=bar")).to eql('foo' => 'bar')
|
// expect(env("\n\n\n # HERE GOES FOO \nfoo=bar")).to eql('foo' => 'bar')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user