Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiline values #16

Merged
merged 5 commits into from May 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions fixtures/exported.env
@@ -1,2 +1,25 @@
export OPTION_A=2
export OPTION_B='\n'
# This is a comment
export OPTION_C='The MIT License (MIT)

Copyright (c) 2013 Alif Rachmawadi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.'

3 changes: 3 additions & 0 deletions fixtures/quoted.env
Expand Up @@ -6,3 +6,6 @@ OPTION_E="1"
OPTION_F="2"
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$$\$""'
10 changes: 8 additions & 2 deletions go.mod
@@ -1,5 +1,11 @@
module github.com/subosito/gotenv

go 1.13
go 1.18

require github.com/stretchr/testify v1.4.0
require github.com/stretchr/testify v1.7.0

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
8 changes: 4 additions & 4 deletions go.sum
Expand Up @@ -3,9 +3,9 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
92 changes: 67 additions & 25 deletions gotenv.go
Expand Up @@ -16,6 +16,9 @@ const (

// Pattern for detecting valid variable within a value
variablePattern = `(\\)?(\$)(\{?([A-Z0-9_]+)?\}?)`

// Byte order mark character
bom = "\xef\xbb\xbf"
)

// Env holds key/value pair of valid environment variable
Expand Down Expand Up @@ -125,17 +128,50 @@ func strictParse(r io.Reader, override bool) (Env, error) {
env := make(Env)
scanner := bufio.NewScanner(r)

i := 1
bom := string([]byte{239, 187, 191})
firstLine := true

for scanner.Scan() {
line := scanner.Text()
line := strings.TrimSpace(scanner.Text())

if i == 1 {
if firstLine {
line = strings.TrimPrefix(line, bom)
firstLine = false
}

if line == "" || line[0] == '#' {
continue
}

i++
quote := ""
idx := strings.Index(line, "=")
if idx == -1 {
idx = strings.Index(line, ":")
}
if idx > 0 && idx < len(line)-1 {
val := strings.TrimSpace(line[idx+1:])
if val[0] == '"' || val[0] == '\'' {
quote = val[:1]
idx = strings.LastIndex(strings.TrimSpace(val[1:]), quote)
if idx >= 0 && val[idx] != '\\' {
quote = ""
}
}
}
for quote != "" && scanner.Scan() {
l := scanner.Text()
line += "\n" + l
idx := strings.LastIndex(l, quote)
if idx > 0 && l[idx-1] == '\\' {
continue
}
if idx >= 0 {
quote = ""
}
}

if quote != "" {
return env, fmt.Errorf("missing quotes")
}

err := parseLine(line, env, override)
if err != nil {
Expand All @@ -145,9 +181,15 @@ func strictParse(r io.Reader, override bool) (Env, error) {

return env, nil
}

var (
lineRgx = regexp.MustCompile(linePattern)
unescapeRgx = regexp.MustCompile(`\\([^$])`)
varRgx = regexp.MustCompile(variablePattern)
)

func parseLine(s string, env Env, override bool) error {
rl := regexp.MustCompile(linePattern)
rm := rl.FindStringSubmatch(s)
rm := lineRgx.FindStringSubmatch(s)

if len(rm) == 0 {
return checkFormat(s, env)
Expand All @@ -156,35 +198,36 @@ 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 ""
rq := regexp.MustCompile(`\A(['"])(.*)(['"])\z`)
val = rq.ReplaceAllString(val, "$2")
if l := len(val); (hsq || hdq) && l >= 2 {
val = val[1 : l-1]
}

if hdq {
val = strings.Replace(val, `\n`, "\n", -1)
val = strings.Replace(val, `\r`, "\r", -1)
val = strings.ReplaceAll(val, `\n`, "\n")
val = strings.ReplaceAll(val, `\r`, "\r")

// Unescape all characters except $ so variables can be escaped properly
re := regexp.MustCompile(`\\([^$])`)
val = re.ReplaceAllString(val, "$1")
val = unescapeRgx.ReplaceAllString(val, "$1")
}

rv := regexp.MustCompile(variablePattern)
fv := func(s string) string {
return varReplacement(s, hsq, env, override)
}

val = rv.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 All @@ -204,6 +247,8 @@ func parseExport(st string, env Env) error {
return nil
}

var varNameRgx = regexp.MustCompile(`(\$)(\{?([A-Z0-9_]+)\}?)`)

func varReplacement(s string, hsq bool, env Env, override bool) string {
if strings.HasPrefix(s, "\\") {
return strings.TrimPrefix(s, "\\")
Expand All @@ -213,9 +258,7 @@ func varReplacement(s string, hsq bool, env Env, override bool) string {
return s
}

sn := `(\$)(\{?([A-Z0-9_]+)\}?)`
rn := regexp.MustCompile(sn)
mn := rn.FindStringSubmatch(s)
mn := varNameRgx.FindStringSubmatch(s)

if len(mn) == 0 {
return s
Expand Down Expand Up @@ -255,9 +298,8 @@ func parseVal(val string, env Env, ignoreNewlines bool, override bool) string {

if len(kv) > 1 {
val = kv[0]

for i := 1; i < len(kv); i++ {
parseLine(kv[i], env, override)
for _, l := range kv[1:] {
_ = parseLine(l, env, override)
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions gotenv_test.go
Expand Up @@ -153,6 +153,27 @@ var fixtures = []struct {
gotenv.Env{
"OPTION_A": "2",
"OPTION_B": `\n`,
"OPTION_C": `The MIT License (MIT)

Copyright (c) 2013 Alif Rachmawadi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.`,
},
},
{
Expand All @@ -176,6 +197,9 @@ var fixtures = []struct {
"OPTION_F": "2",
"OPTION_G": "",
"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