From 8a8e49d48111c31f46ca0c006dd02a62c76d66af Mon Sep 17 00:00:00 2001 From: prongbang Date: Sun, 14 Jun 2020 18:38:53 +0700 Subject: [PATCH] fixed bug cannot get value when has = --- .env | 6 +++++- cover.out | 4 ++-- coverage.html | 8 ++++---- go.mod | 2 ++ goenv.go | 8 ++++---- goenv_test.go | 13 +++++++++++++ 6 files changed, 30 insertions(+), 11 deletions(-) diff --git a/.env b/.env index 3639467..aec9373 100644 --- a/.env +++ b/.env @@ -1,2 +1,6 @@ +# Database Connection DB_USER=root -DB_PASS=1234 \ No newline at end of file +DB_PASS=1234 + +# Basic authentication +BASIC_AUTHORIZATION=Basic dXNlcjpwYXNz== \ No newline at end of file diff --git a/cover.out b/cover.out index a2dbfad..0313776 100644 --- a/cover.out +++ b/cover.out @@ -12,5 +12,5 @@ github.com/prongbang/goenv/goenv.go:39.37,41.2 1 1 github.com/prongbang/goenv/goenv.go:43.29,47.21 3 1 github.com/prongbang/goenv/goenv.go:59.2,59.12 1 1 github.com/prongbang/goenv/goenv.go:47.21,49.34 2 1 -github.com/prongbang/goenv/goenv.go:49.34,51.24 2 1 -github.com/prongbang/goenv/goenv.go:51.24,55.5 3 1 +github.com/prongbang/goenv/goenv.go:49.34,51.14 2 1 +github.com/prongbang/goenv/goenv.go:51.14,55.5 3 1 diff --git a/coverage.html b/coverage.html index 00aab88..4280b46 100644 --- a/coverage.html +++ b/coverage.html @@ -117,10 +117,10 @@ for scanner.Scan() { text := scanner.Text() if strings.Contains(text, "=") { - keyVal := strings.Split(text, "=") - if len(keyVal) >= 2 { - key := keyVal[0] - val := keyVal[1] + i := strings.Index(text, "=") + if i > -1 { + key := text[:i] + val := text[i+1:] env[key] = val } } diff --git a/go.mod b/go.mod index 7919728..de0712a 100644 --- a/go.mod +++ b/go.mod @@ -1 +1,3 @@ module github.com/prongbang/goenv + +go 1.14 diff --git a/goenv.go b/goenv.go index 42fcb1e..71d8377 100644 --- a/goenv.go +++ b/goenv.go @@ -47,10 +47,10 @@ func parse(r io.Reader) Env { for scanner.Scan() { text := scanner.Text() if strings.Contains(text, "=") { - keyVal := strings.Split(text, "=") - if len(keyVal) >= 2 { - key := keyVal[0] - val := keyVal[1] + i := strings.Index(text, "=") + if i > -1 { + key := text[:i] + val := text[i+1:] env[key] = val } } diff --git a/goenv_test.go b/goenv_test.go index 3c7575d..a638e25 100644 --- a/goenv_test.go +++ b/goenv_test.go @@ -21,6 +21,19 @@ func TestLoadEnvDefault(t *testing.T) { } } +func TestLoadEnvBasicAuthen(t *testing.T) { + err := goenv.LoadEnv() + basic := os.Getenv("BASIC_AUTHORIZATION") + + if err != nil { + t.Error("Read .env file wrong.") + } + + if basic != "Basic dXNlcjpwYXNz==" { + t.Error("Read .env file wrong.") + } +} + func TestLoadEnvByFile(t *testing.T) { err := goenv.LoadEnv(".testenv") dbUser := os.Getenv("DB_USER")