Skip to content

Commit

Permalink
Merge pull request #36 from pda/parse-from-reader
Browse files Browse the repository at this point in the history
Parse(reader) as alternative to Read(filenames)
  • Loading branch information
joho authored Aug 6, 2017
2 parents 3ddb279 + 390de37 commit 9d9ddad
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 24 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ myEnv, err := godotenv.Read()
s3Bucket := myEnv["S3_BUCKET"]
```

... or from an `io.Reader` instead of a local file

```go
reader := getRemoteFile()
myEnv, err := godotenv.Parse(reader)
```

### Command Mode

Assuming you've installed the command as above and you've got `$GOPATH/bin` in your `$PATH`
Expand Down
54 changes: 30 additions & 24 deletions godotenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package godotenv
import (
"bufio"
"errors"
"io"
"os"
"os/exec"
"strings"
Expand Down Expand Up @@ -89,6 +90,34 @@ func Read(filenames ...string) (envMap map[string]string, err error) {
return
}

// Parse reads an env file from io.Reader, returning a map of keys and values.
func Parse(r io.Reader) (envMap map[string]string, err error) {
envMap = make(map[string]string)

var lines []string
scanner := bufio.NewScanner(r)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}

if err = scanner.Err(); err != nil {
return
}

for _, fullLine := range lines {
if !isIgnoredLine(fullLine) {
var key, value string
key, value, err = parseLine(fullLine)

if err != nil {
return
}
envMap[key] = value
}
}
return
}

// Exec loads env vars from the specified filenames (empty map falls back to default)
// then executes the cmd specified.
//
Expand Down Expand Up @@ -142,30 +171,7 @@ func readFile(filename string) (envMap map[string]string, err error) {
}
defer file.Close()

envMap = make(map[string]string)

var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}

if err = scanner.Err(); err != nil {
return
}

for _, fullLine := range lines {
if !isIgnoredLine(fullLine) {
var key, value string
key, value, err = parseLine(fullLine)

if err != nil {
return
}
envMap[key] = value
}
}
return
return Parse(file)
}

func parseLine(line string) (key string, value string, err error) {
Expand Down
18 changes: 18 additions & 0 deletions godotenv_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package godotenv

import (
"bytes"
"os"
"testing"
)
Expand Down Expand Up @@ -94,6 +95,23 @@ func TestReadPlainEnv(t *testing.T) {
}
}

func TestParse(t *testing.T) {
envMap, err := Parse(bytes.NewReader([]byte("ONE=1\nTWO='2'\nTHREE = \"3\"")))
expectedValues := map[string]string{
"ONE": "1",
"TWO": "2",
"THREE": "3",
}
if err != nil {
t.Fatalf("error parsing env: %v", err)
}
for key, value := range expectedValues {
if envMap[key] != value {
t.Errorf("expected %s to be %s, got %s", key, value, envMap[key])
}
}
}

func TestLoadDoesNotOverride(t *testing.T) {
envFileName := "fixtures/plain.env"

Expand Down

0 comments on commit 9d9ddad

Please sign in to comment.