Skip to content

Commit

Permalink
fixed bug cannot get value when has =
Browse files Browse the repository at this point in the history
  • Loading branch information
prongbang committed Jun 14, 2020
1 parent 9ea5fb5 commit 8a8e49d
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 11 deletions.
6 changes: 5 additions & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
# Database Connection
DB_USER=root
DB_PASS=1234
DB_PASS=1234

# Basic authentication
BASIC_AUTHORIZATION=Basic dXNlcjpwYXNz==
4 changes: 2 additions & 2 deletions cover.out
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions coverage.html
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@
for scanner.Scan() </span><span class="cov8" title="1">{
text := scanner.Text()
if strings.Contains(text, "=") </span><span class="cov8" title="1">{
keyVal := strings.Split(text, "=")
if len(keyVal) &gt;= 2 </span><span class="cov8" title="1">{
key := keyVal[0]
val := keyVal[1]
i := strings.Index(text, "=")
if i &gt; -1 </span><span class="cov8" title="1">{
key := text[:i]
val := text[i+1:]
env[key] = val
}</span>
}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module github.com/prongbang/goenv

go 1.14
8 changes: 4 additions & 4 deletions goenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
13 changes: 13 additions & 0 deletions goenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 8a8e49d

Please sign in to comment.