From 6f30f0c011721a03e416dd262d35b2f5eee54820 Mon Sep 17 00:00:00 2001 From: Alex Quick Date: Sat, 20 May 2017 15:06:01 -0400 Subject: [PATCH] support for equals in yaml-style lines --- godotenv.go | 8 ++++---- godotenv_test.go | 6 ++++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/godotenv.go b/godotenv.go index 7cc4d06..172c4f5 100644 --- a/godotenv.go +++ b/godotenv.go @@ -198,11 +198,11 @@ func parseLine(line string) (key string, value string, err error) { line = strings.Join(segmentsToKeep, "#") } - // now split key from value + firstEquals := strings.Index(line, "=") + firstColon := strings.Index(line, ":") splitString := strings.SplitN(line, "=", 2) - - if len(splitString) != 2 { - // try yaml mode! + if firstColon != -1 && (firstColon < firstEquals || firstEquals == -1) { + //this is a yaml-style line splitString = strings.SplitN(line, ":", 2) } diff --git a/godotenv_test.go b/godotenv_test.go index 9d7d884..4099201 100644 --- a/godotenv_test.go +++ b/godotenv_test.go @@ -205,6 +205,12 @@ func TestParsing(t *testing.T) { // parses yaml style options parseAndCompare(t, "OPTION_A: 1", "OPTION_A", "1") + //parses yaml values with equal signs + parseAndCompare(t, "OPTION_A: Foo=bar", "OPTION_A", "Foo=bar") + + // parses non-yaml options with colons + parseAndCompare(t, "OPTION_A=1:B", "OPTION_A", "1:B") + // parses export keyword parseAndCompare(t, "export OPTION_A=2", "OPTION_A", "2") parseAndCompare(t, `export OPTION_B='\n'`, "OPTION_B", "\n")