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

Commit eaf676f

Browse files
authored
Merge pull request joho#27 from goenning/empty_var
allow usage of empty var on .env
2 parents b01826f + a42a655 commit eaf676f

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

fixtures/plain.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ OPTION_B=2
33
OPTION_C= 3
44
OPTION_D =4
55
OPTION_E = 5
6+
OPTION_F =
7+
OPTION_G=

godotenv.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -212,20 +212,23 @@ func parseLine(line string) (key string, value string, err error) {
212212

213213
// Parse the value
214214
value = splitString[1]
215+
215216
// trim
216217
value = strings.Trim(value, " ")
217218

218219
// check if we've got quoted values
219-
first := string(value[0:1])
220-
last := string(value[len(value)-1:])
221-
if first == last && strings.ContainsAny(first, `"'`) {
222-
// pull the quotes off the edges
223-
value = strings.Trim(value, `"'`)
224-
225-
// expand quotes
226-
value = strings.Replace(value, `\"`, `"`, -1)
227-
// expand newlines
228-
value = strings.Replace(value, `\n`, "\n", -1)
220+
if value != "" {
221+
first := string(value[0:1])
222+
last := string(value[len(value)-1:])
223+
if first == last && strings.ContainsAny(first, `"'`) {
224+
// pull the quotes off the edges
225+
value = strings.Trim(value, `"'`)
226+
227+
// expand quotes
228+
value = strings.Replace(value, `\"`, `"`, -1)
229+
// expand newlines
230+
value = strings.Replace(value, `\n`, "\n", -1)
231+
}
229232
}
230233

231234
return

godotenv_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ func TestReadPlainEnv(t *testing.T) {
7474
"OPTION_C": "3",
7575
"OPTION_D": "4",
7676
"OPTION_E": "5",
77+
"OPTION_F": "",
78+
"OPTION_G": "",
7779
}
7880

7981
envMap, err := Read(envFileName)

0 commit comments

Comments
 (0)