mirror of https://github.com/joho/godotenv.git
Start trying to do comments/quoting properly.
parent
b3b488372f
commit
a37b438d54
21
godotenv.go
21
godotenv.go
|
@ -76,8 +76,25 @@ func parseLine(line string) (key string, value string, err error) {
|
||||||
}
|
}
|
||||||
key = strings.Trim(key, " ")
|
key = strings.Trim(key, " ")
|
||||||
|
|
||||||
value = strings.Trim(splitString[1], " \"'")
|
value = splitString[1]
|
||||||
value = strings.Replace(value, "\\\"", "\"", -1)
|
|
||||||
|
// ditch the comments
|
||||||
|
if strings.Contains(value, "#") {
|
||||||
|
value = strings.Trim(strings.Split(value, "#")[0], " ")
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if we've got quoted values
|
||||||
|
if strings.Count(value, "\"") == 2 || strings.Count(value, "'") == 2 {
|
||||||
|
// pull the quotes off the edge
|
||||||
|
value = strings.Trim(value, "\"'")
|
||||||
|
|
||||||
|
// expand quotes
|
||||||
|
value = strings.Replace(value, "\\\"", "\"", -1)
|
||||||
|
// expand newlines
|
||||||
|
value = strings.Replace(value, "\\n", "\n", -1)
|
||||||
|
}
|
||||||
|
// trim
|
||||||
|
value = strings.Trim(value, " ")
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -70,45 +70,36 @@ func TestParsing(t *testing.T) {
|
||||||
// parses export keyword
|
// parses export keyword
|
||||||
parseAndCompare(t, "export OPTION_A=2", "OPTION_A", "2")
|
parseAndCompare(t, "export OPTION_A=2", "OPTION_A", "2")
|
||||||
|
|
||||||
/*
|
// it 'expands newlines in quoted strings' do
|
||||||
The rest of my TODO list
|
// expect(env('FOO="bar\nbaz"')).to eql('FOO' => "bar\nbaz")
|
||||||
|
parseAndCompare(t, "FOO=\"bar\\nbaz\"", "FOO", "bar\nbaz")
|
||||||
|
|
||||||
it 'expands newlines in quoted strings' do
|
// it 'parses varibales with "." in the name' do
|
||||||
expect(env('FOO="bar\nbaz"')).to eql('FOO' => "bar\nbaz")
|
// expect(env('FOO.BAR=foobar')).to eql('FOO.BAR' => 'foobar')
|
||||||
end
|
parseAndCompare(t, "FOO.BAR=foobar", "FOO.BAR", "foobar")
|
||||||
|
|
||||||
it 'parses varibales with "." in the name' do
|
// it 'strips unquoted values' do
|
||||||
expect(env('FOO.BAR=foobar')).to eql('FOO.BAR' => 'foobar')
|
// expect(env('foo=bar ')).to eql('foo' => 'bar') # not 'bar '
|
||||||
end
|
parseAndCompare(t, "FOO=bar ", "FOO", "bar")
|
||||||
|
|
||||||
it 'strips unquoted values' do
|
// it 'ignores inline comments' do
|
||||||
expect(env('foo=bar ')).to eql('foo' => 'bar') # not 'bar '
|
// expect(env("foo=bar # this is foo")).to eql('foo' => 'bar')
|
||||||
end
|
parseAndCompare(t, "FOO=bar # this is foo", "FOO", "bar")
|
||||||
|
|
||||||
it 'throws an error if line format is incorrect' do
|
// it 'allows # in quoted value' do
|
||||||
expect{env('lol$wut')}.to raise_error(Dotenv::FormatError)
|
// expect(env('foo="bar#baz" # comment')).to eql('foo' => 'bar#baz')
|
||||||
end
|
parseAndCompare(t, "FOO=\"bar#baz\" # comment", "FOO", "bar#baz")
|
||||||
|
|
||||||
it 'ignores empty lines' do
|
// it 'ignores comment lines' do
|
||||||
expect(env("\n \t \nfoo=bar\n \nfizz=buzz")).to eql('foo' => 'bar', 'fizz' => 'buzz')
|
// expect(env("\n\n\n # HERE GOES FOO \nfoo=bar")).to eql('foo' => 'bar')
|
||||||
end
|
|
||||||
|
|
||||||
it 'ignores inline comments' do
|
// it 'parses # in quoted values' do
|
||||||
expect(env("foo=bar # this is foo")).to eql('foo' => 'bar')
|
// expect(env('foo="ba#r"')).to eql('foo' => 'ba#r')
|
||||||
end
|
// expect(env("foo='ba#r'")).to eql('foo' => 'ba#r')
|
||||||
|
|
||||||
it 'allows # in quoted value' do
|
// it 'throws an error if line format is incorrect' do
|
||||||
expect(env('foo="bar#baz" # comment')).to eql('foo' => 'bar#baz')
|
// expect{env('lol$wut')}.to raise_error(Dotenv::FormatError)
|
||||||
end
|
|
||||||
|
|
||||||
it 'ignores comment lines' do
|
// it 'ignores empty lines' do
|
||||||
expect(env("\n\n\n # HERE GOES FOO \nfoo=bar")).to eql('foo' => 'bar')
|
// expect(env("\n \t \nfoo=bar\n \nfizz=buzz")).to eql('foo' => 'bar', 'fizz' => 'buzz')
|
||||||
end
|
|
||||||
|
|
||||||
it 'parses # in quoted values' do
|
|
||||||
expect(env('foo="ba#r"')).to eql('foo' => 'ba#r')
|
|
||||||
expect(env("foo='ba#r'")).to eql('foo' => 'ba#r')
|
|
||||||
end
|
|
||||||
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue