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

support multi line text value #142

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
.idea
6 changes: 6 additions & 0 deletions fixtures/multiline.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
PASSPORT_PUBLIC_KEY="-----BEGIN PUBLIC KEY-----
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
lHRucQ+Fe7L5fExgBtkNqjUCAwEAAQ==
-----END PUBLIC KEY-----"
59 changes: 52 additions & 7 deletions godotenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,65 @@ func Parse(r io.Reader) (envMap map[string]string, err error) {
return
}

for _, fullLine := range lines {
if !isIgnoredLine(fullLine) {
var key, value string
key, value, err = parseLine(fullLine, envMap)
for i:= 0; i < len(lines); i++ {
fullLine := lines[i]
if isIgnoredLine(fullLine) {
continue
}
var key, value string
key, value, err = parseLine(fullLine, envMap)
if err != nil {
return
}

if err != nil {
return
if len(value) > 0 && (value[0] == '"' || value[0] == '\''){
quota := value[0]
value = value[1:]
for j := i+1; j < len(lines); j++ {
nextLine := lines[j]
value += "\n" + nextLine

if isMultiLineEnd(quota, nextLine) {
value = value[:len(value)-1]
i = j
break
}
}
envMap[key] = value
}
envMap[key] = value
}
return
}


func isMultiLineEnd(quota byte, line string)bool{
l := len(line)
lastChar := line[l-1]
if lastChar != quota {
return false
}

// AABBCCDDD\"
// DDEEEFFFGG\\\\"
// 123456789"

if l > 3 && line[l-2] == '\\' && line[l-3] == '\\' {
return true
}

// AABBCCDDD\"
// DDEEEFFFGG"
if l == 2 && line[l-2] != '\\'{
return true
}

if l > 2 && line[l-2] != '\\' {
return true
}

return false
}

//Unmarshal reads an env file from a string, returning a map of keys and values.
func Unmarshal(str string) (envMap map[string]string, err error) {
return Parse(strings.NewReader(str))
Expand Down
30 changes: 30 additions & 0 deletions godotenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package godotenv
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"reflect"
"strings"
Expand Down Expand Up @@ -472,3 +473,32 @@ func TestRoundtrip(t *testing.T) {

}
}



func TestMultiLine(t *testing.T) {
envFileName := "fixtures/multiline.env"
envMap, err := Read(envFileName)

if err != nil {
t.Error(err)
}


assertValue, err := ioutil.ReadFile(envFileName)
if err != nil {
t.Error(err)
}

assertValue = assertValue[21: len(assertValue)-1]

if envMap["PASSPORT_PUBLIC_KEY"] != string(assertValue) {
t.Errorf(
"Expected length %d, Actual length %d\r\n Expected Text:\r\n%s\r\nActual Text:\r\n%s\r\n",
len(assertValue),
len(envMap["PASSPORT_PUBLIC_KEY"]),
string(assertValue),
envMap["PASSPORT_PUBLIC_KEY"],
)
}
}