Skip to content

Commit 9d9ddad

Browse files
authored
Merge pull request #36 from pda/parse-from-reader
Parse(reader) as alternative to Read(filenames)
2 parents 3ddb279 + 390de37 commit 9d9ddad

File tree

3 files changed

+55
-24
lines changed

3 files changed

+55
-24
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ myEnv, err := godotenv.Read()
9696
s3Bucket := myEnv["S3_BUCKET"]
9797
```
9898

99+
... or from an `io.Reader` instead of a local file
100+
101+
```go
102+
reader := getRemoteFile()
103+
myEnv, err := godotenv.Parse(reader)
104+
```
105+
99106
### Command Mode
100107

101108
Assuming you've installed the command as above and you've got `$GOPATH/bin` in your `$PATH`

godotenv.go

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package godotenv
1616
import (
1717
"bufio"
1818
"errors"
19+
"io"
1920
"os"
2021
"os/exec"
2122
"strings"
@@ -89,6 +90,34 @@ func Read(filenames ...string) (envMap map[string]string, err error) {
8990
return
9091
}
9192

93+
// Parse reads an env file from io.Reader, returning a map of keys and values.
94+
func Parse(r io.Reader) (envMap map[string]string, err error) {
95+
envMap = make(map[string]string)
96+
97+
var lines []string
98+
scanner := bufio.NewScanner(r)
99+
for scanner.Scan() {
100+
lines = append(lines, scanner.Text())
101+
}
102+
103+
if err = scanner.Err(); err != nil {
104+
return
105+
}
106+
107+
for _, fullLine := range lines {
108+
if !isIgnoredLine(fullLine) {
109+
var key, value string
110+
key, value, err = parseLine(fullLine)
111+
112+
if err != nil {
113+
return
114+
}
115+
envMap[key] = value
116+
}
117+
}
118+
return
119+
}
120+
92121
// Exec loads env vars from the specified filenames (empty map falls back to default)
93122
// then executes the cmd specified.
94123
//
@@ -142,30 +171,7 @@ func readFile(filename string) (envMap map[string]string, err error) {
142171
}
143172
defer file.Close()
144173

145-
envMap = make(map[string]string)
146-
147-
var lines []string
148-
scanner := bufio.NewScanner(file)
149-
for scanner.Scan() {
150-
lines = append(lines, scanner.Text())
151-
}
152-
153-
if err = scanner.Err(); err != nil {
154-
return
155-
}
156-
157-
for _, fullLine := range lines {
158-
if !isIgnoredLine(fullLine) {
159-
var key, value string
160-
key, value, err = parseLine(fullLine)
161-
162-
if err != nil {
163-
return
164-
}
165-
envMap[key] = value
166-
}
167-
}
168-
return
174+
return Parse(file)
169175
}
170176

171177
func parseLine(line string) (key string, value string, err error) {

godotenv_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package godotenv
22

33
import (
4+
"bytes"
45
"os"
56
"testing"
67
)
@@ -94,6 +95,23 @@ func TestReadPlainEnv(t *testing.T) {
9495
}
9596
}
9697

98+
func TestParse(t *testing.T) {
99+
envMap, err := Parse(bytes.NewReader([]byte("ONE=1\nTWO='2'\nTHREE = \"3\"")))
100+
expectedValues := map[string]string{
101+
"ONE": "1",
102+
"TWO": "2",
103+
"THREE": "3",
104+
}
105+
if err != nil {
106+
t.Fatalf("error parsing env: %v", err)
107+
}
108+
for key, value := range expectedValues {
109+
if envMap[key] != value {
110+
t.Errorf("expected %s to be %s, got %s", key, value, envMap[key])
111+
}
112+
}
113+
}
114+
97115
func TestLoadDoesNotOverride(t *testing.T) {
98116
envFileName := "fixtures/plain.env"
99117

0 commit comments

Comments
 (0)