Skip to content

Commit

Permalink
fix: do not parse single quoted values
Browse files Browse the repository at this point in the history
  • Loading branch information
luisdavim committed May 22, 2022
1 parent 2c6c737 commit 36b11c3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
1 change: 1 addition & 0 deletions fixtures/quoted.env
Expand Up @@ -8,3 +8,4 @@ OPTION_G=""
OPTION_H="\n"
OPTION_I="some multi-line text
with \"escaped quotes\" and ${OPTION_A} variable"
OPTION_J='some$pecial$1$2!*chars=qweq""e$$\$""'
16 changes: 10 additions & 6 deletions gotenv.go
Expand Up @@ -198,17 +198,19 @@ func parseLine(s string, env Env, override bool) error {
key := rm[1]
val := rm[2]

// trim whitespace
val = strings.TrimSpace(val)

// determine if string has quote prefix
hdq := strings.HasPrefix(val, `"`)

// determine if string has single quote prefix
hsq := strings.HasPrefix(val, `'`)

// trim whitespace
val = strings.Trim(val, " ")

// remove quotes '' or ""
val = strings.Trim(val, `'"`)
if l := len(val); (hsq || hdq) && l >= 2 {
val = val[1 : l-1]
}

if hdq {
val = strings.ReplaceAll(val, `\n`, "\n")
Expand All @@ -222,8 +224,10 @@ func parseLine(s string, env Env, override bool) error {
return varReplacement(s, hsq, env, override)
}

val = varRgx.ReplaceAllStringFunc(val, fv)
val = parseVal(val, env, hdq, override)
if !hsq {
val = varRgx.ReplaceAllStringFunc(val, fv)
val = parseVal(val, env, hdq, override)
}

env[key] = val
return nil
Expand Down
1 change: 1 addition & 0 deletions gotenv_test.go
Expand Up @@ -199,6 +199,7 @@ THE SOFTWARE.`,
"OPTION_H": "\n",
"OPTION_I": `some multi-line text
with "escaped quotes" and 1 variable`,
"OPTION_J": `some$pecial$1$2!*chars=qweq""e$$\$""`,
},
},
{
Expand Down

0 comments on commit 36b11c3

Please sign in to comment.