forked from coop-cloud-mirrors/godotenv
Compare commits
11 Commits
v1.1
...
respect_em
Author | SHA1 | Date | |
---|---|---|---|
034acc2190 | |||
cd1272609d | |||
eaf676fc03 | |||
a42a65518c | |||
b01826f956 | |||
6a1233b2f6 | |||
d10b3fbe00 | |||
0a959c8d8f | |||
bcaccd4f68 | |||
22e45bfff4 | |||
2fc79dff51 |
8
.travis.yml
Normal file
8
.travis.yml
Normal file
@ -0,0 +1,8 @@
|
||||
language: go
|
||||
|
||||
go:
|
||||
- 1.8
|
||||
|
||||
os:
|
||||
- linux
|
||||
- osx
|
@ -1,4 +1,4 @@
|
||||
# GoDotEnv [](https://app.wercker.com/project/bykey/507594c2ec7e60f19403a568dfea0f78)
|
||||
# GoDotEnv [](https://travis-ci.org/joho/godotenv) [](https://ci.appveyor.com/project/joho/godotenv)
|
||||
|
||||
A Go (golang) port of the Ruby dotenv project (which loads env vars from a .env file)
|
||||
|
||||
|
@ -3,3 +3,5 @@ OPTION_B=2
|
||||
OPTION_C= 3
|
||||
OPTION_D =4
|
||||
OPTION_E = 5
|
||||
OPTION_F =
|
||||
OPTION_G=
|
28
godotenv.go
28
godotenv.go
@ -119,8 +119,15 @@ func loadFile(filename string, overload bool) error {
|
||||
return err
|
||||
}
|
||||
|
||||
currentEnv := map[string]bool{}
|
||||
rawEnv := os.Environ()
|
||||
for _, rawEnvLine := range rawEnv {
|
||||
key := strings.Split(rawEnvLine, "=")[0]
|
||||
currentEnv[key] = true
|
||||
}
|
||||
|
||||
for key, value := range envMap {
|
||||
if os.Getenv(key) == "" || overload {
|
||||
if !currentEnv[key] || overload {
|
||||
os.Setenv(key, value)
|
||||
}
|
||||
}
|
||||
@ -212,18 +219,23 @@ func parseLine(line string) (key string, value string, err error) {
|
||||
|
||||
// Parse the value
|
||||
value = splitString[1]
|
||||
|
||||
// trim
|
||||
value = strings.Trim(value, " ")
|
||||
|
||||
// check if we've got quoted values
|
||||
if strings.Count(value, "\"") == 2 || strings.Count(value, "'") == 2 {
|
||||
// pull the quotes off the edges
|
||||
value = strings.Trim(value, "\"'")
|
||||
if value != "" {
|
||||
first := string(value[0:1])
|
||||
last := string(value[len(value)-1:])
|
||||
if first == last && strings.ContainsAny(first, `"'`) {
|
||||
// pull the quotes off the edges
|
||||
value = strings.Trim(value, `"'`)
|
||||
|
||||
// expand quotes
|
||||
value = strings.Replace(value, "\\\"", "\"", -1)
|
||||
// expand newlines
|
||||
value = strings.Replace(value, "\\n", "\n", -1)
|
||||
// expand quotes
|
||||
value = strings.Replace(value, `\"`, `"`, -1)
|
||||
// expand newlines
|
||||
value = strings.Replace(value, `\n`, "\n", -1)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
|
@ -74,6 +74,8 @@ func TestReadPlainEnv(t *testing.T) {
|
||||
"OPTION_C": "3",
|
||||
"OPTION_D": "4",
|
||||
"OPTION_E": "5",
|
||||
"OPTION_F": "",
|
||||
"OPTION_G": "",
|
||||
}
|
||||
|
||||
envMap, err := Read(envFileName)
|
||||
@ -98,10 +100,12 @@ func TestLoadDoesNotOverride(t *testing.T) {
|
||||
// ensure NO overload
|
||||
presets := map[string]string{
|
||||
"OPTION_A": "do_not_override",
|
||||
"OPTION_B": "",
|
||||
}
|
||||
|
||||
expectedValues := map[string]string{
|
||||
"OPTION_A": "do_not_override",
|
||||
"OPTION_B": "",
|
||||
}
|
||||
loadEnvAndCompareValues(t, Load, envFileName, expectedValues, presets)
|
||||
}
|
||||
@ -187,24 +191,24 @@ func TestParsing(t *testing.T) {
|
||||
parseAndCompare(t, "FOO= bar", "FOO", "bar")
|
||||
|
||||
// parses double quoted values
|
||||
parseAndCompare(t, "FOO=\"bar\"", "FOO", "bar")
|
||||
parseAndCompare(t, `FOO="bar"`, "FOO", "bar")
|
||||
|
||||
// parses single quoted values
|
||||
parseAndCompare(t, "FOO='bar'", "FOO", "bar")
|
||||
|
||||
// parses escaped double quotes
|
||||
parseAndCompare(t, "FOO=escaped\\\"bar\"", "FOO", "escaped\"bar")
|
||||
parseAndCompare(t, `FOO="escaped\"bar"`, "FOO", `escaped"bar`)
|
||||
|
||||
// parses yaml style options
|
||||
parseAndCompare(t, "OPTION_A: 1", "OPTION_A", "1")
|
||||
|
||||
// parses export keyword
|
||||
parseAndCompare(t, "export OPTION_A=2", "OPTION_A", "2")
|
||||
parseAndCompare(t, "export OPTION_B='\\n'", "OPTION_B", "\n")
|
||||
parseAndCompare(t, `export OPTION_B='\n'`, "OPTION_B", "\n")
|
||||
|
||||
// it 'expands newlines in quoted strings' do
|
||||
// expect(env('FOO="bar\nbaz"')).to eql('FOO' => "bar\nbaz")
|
||||
parseAndCompare(t, "FOO=\"bar\\nbaz\"", "FOO", "bar\nbaz")
|
||||
parseAndCompare(t, `FOO="bar\nbaz"`, "FOO", "bar\nbaz")
|
||||
|
||||
// it 'parses varibales with "." in the name' do
|
||||
// expect(env('FOO.BAR=foobar')).to eql('FOO.BAR' => 'foobar')
|
||||
@ -224,14 +228,14 @@ func TestParsing(t *testing.T) {
|
||||
|
||||
// it 'allows # in quoted value' do
|
||||
// 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#bang\" # comment", "FOO", "bar#baz#bang")
|
||||
parseAndCompare(t, `FOO="bar#baz#bang" # comment`, "FOO", "bar#baz#bang")
|
||||
|
||||
// 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')
|
||||
parseAndCompare(t, "FOO=\"ba#r\"", "FOO", "ba#r")
|
||||
parseAndCompare(t, `FOO="ba#r"`, "FOO", "ba#r")
|
||||
parseAndCompare(t, "FOO='ba#r'", "FOO", "ba#r")
|
||||
|
||||
// it 'throws an error if line format is incorrect' do
|
||||
@ -265,7 +269,7 @@ func TestLinesToIgnore(t *testing.T) {
|
||||
}
|
||||
|
||||
// make sure we're not getting false positives
|
||||
if isIgnoredLine("export OPTION_B='\\n'") {
|
||||
if isIgnoredLine(`export OPTION_B='\n'`) {
|
||||
t.Error("ignoring a perfectly valid line to parse")
|
||||
}
|
||||
}
|
||||
|
@ -1 +0,0 @@
|
||||
box: pjvds/golang
|
Reference in New Issue
Block a user