mirror of https://github.com/joho/godotenv.git
Get the code running against all fixtures.
parent
9b4bbb98c3
commit
ac58adf673
89
godotenv.go
89
godotenv.go
|
@ -3,6 +3,7 @@ package godotenv
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"errors"
|
"errors"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
@ -23,9 +24,23 @@ func loadFile(filename string) (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
bufferSize := 20
|
lines := readRawLines(file)
|
||||||
lines := make([]string, bufferSize)
|
|
||||||
lineReader := bufio.NewReaderSize(file, bufferSize)
|
for _, fullLine := range lines {
|
||||||
|
if !isIgnoredLine(fullLine) {
|
||||||
|
key, value, err := parseLine(fullLine)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
os.Setenv(key, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func readRawLines(file io.Reader) (lines []string) {
|
||||||
|
lineReader := bufio.NewReader(file)
|
||||||
for line, isPrefix, e := lineReader.ReadLine(); e == nil; line, isPrefix, e = lineReader.ReadLine() {
|
for line, isPrefix, e := lineReader.ReadLine(); e == nil; line, isPrefix, e = lineReader.ReadLine() {
|
||||||
fullLine := string(line)
|
fullLine := string(line)
|
||||||
if isPrefix {
|
if isPrefix {
|
||||||
|
@ -40,19 +55,6 @@ func loadFile(filename string) (err error) {
|
||||||
// add a line to the game/parse
|
// add a line to the game/parse
|
||||||
lines = append(lines, string(line))
|
lines = append(lines, string(line))
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, fullLine := range lines {
|
|
||||||
// fmt.Printf("Line: %v\n", fullLine)
|
|
||||||
if !isIgnoredLine(fullLine) {
|
|
||||||
key, value, err := parseLine(fullLine)
|
|
||||||
// fmt.Printf("Key: %v Value: %v\n", key, value)
|
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
os.Setenv(key, value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,29 +64,9 @@ func parseLine(line string) (key string, value string, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
splitString := strings.Split(line, "=")
|
|
||||||
|
|
||||||
if len(splitString) != 2 {
|
|
||||||
// try yaml mode!
|
|
||||||
splitString = strings.Split(line, ":")
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(splitString) != 2 {
|
|
||||||
err = errors.New("Can't separate key from value")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
key = splitString[0]
|
|
||||||
if strings.HasPrefix(key, "export") {
|
|
||||||
key = strings.TrimPrefix(key, "export")
|
|
||||||
}
|
|
||||||
key = strings.Trim(key, " ")
|
|
||||||
|
|
||||||
value = splitString[1]
|
|
||||||
|
|
||||||
// ditch the comments (but keep quoted hashes)
|
// ditch the comments (but keep quoted hashes)
|
||||||
if strings.Contains(value, "#") {
|
if strings.Contains(line, "#") {
|
||||||
segmentsBetweenHashes := strings.Split(value, "#")
|
segmentsBetweenHashes := strings.Split(line, "#")
|
||||||
quotesAreOpen := false
|
quotesAreOpen := false
|
||||||
segmentsToKeep := make([]string, 0)
|
segmentsToKeep := make([]string, 0)
|
||||||
for _, segment := range segmentsBetweenHashes {
|
for _, segment := range segmentsBetweenHashes {
|
||||||
|
@ -102,21 +84,44 @@ func parseLine(line string) (key string, value string, err error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
value = strings.Join(segmentsToKeep, "#")
|
line = strings.Join(segmentsToKeep, "#")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// now split key from value
|
||||||
|
splitString := strings.Split(line, "=")
|
||||||
|
|
||||||
|
if len(splitString) != 2 {
|
||||||
|
// try yaml mode!
|
||||||
|
splitString = strings.Split(line, ":")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(splitString) != 2 {
|
||||||
|
err = errors.New("Can't separate key from value")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse the key
|
||||||
|
key = splitString[0]
|
||||||
|
if strings.HasPrefix(key, "export") {
|
||||||
|
key = strings.TrimPrefix(key, "export")
|
||||||
|
}
|
||||||
|
key = strings.Trim(key, " ")
|
||||||
|
|
||||||
|
// Parse the value
|
||||||
|
value = splitString[1]
|
||||||
|
// trim
|
||||||
|
value = strings.Trim(value, " ")
|
||||||
|
|
||||||
// check if we've got quoted values
|
// check if we've got quoted values
|
||||||
if strings.Count(value, "\"") == 2 || strings.Count(value, "'") == 2 {
|
if strings.Count(value, "\"") == 2 || strings.Count(value, "'") == 2 {
|
||||||
// pull the quotes off the edges
|
// pull the quotes off the edges
|
||||||
value = strings.Trim(value, "\"' ")
|
value = strings.Trim(value, "\"'")
|
||||||
|
|
||||||
// expand quotes
|
// expand quotes
|
||||||
value = strings.Replace(value, "\\\"", "\"", -1)
|
value = strings.Replace(value, "\\\"", "\"", -1)
|
||||||
// expand newlines
|
// expand newlines
|
||||||
value = strings.Replace(value, "\\n", "\n", -1)
|
value = strings.Replace(value, "\\n", "\n", -1)
|
||||||
}
|
}
|
||||||
// trim
|
|
||||||
value = strings.Trim(value, " ")
|
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -98,7 +98,7 @@ func TestParsing(t *testing.T) {
|
||||||
|
|
||||||
// parses export keyword
|
// parses export keyword
|
||||||
parseAndCompare(t, "export OPTION_A=2", "OPTION_A", "2")
|
parseAndCompare(t, "export OPTION_A=2", "OPTION_A", "2")
|
||||||
parseAndCompare(t, "export OPTION_B='\n'", "OPTION_B", "\n")
|
parseAndCompare(t, "export OPTION_B='\\n'", "OPTION_B", "\n")
|
||||||
|
|
||||||
// it 'expands newlines in quoted strings' do
|
// it 'expands newlines in quoted strings' do
|
||||||
// expect(env('FOO="bar\nbaz"')).to eql('FOO' => "bar\nbaz")
|
// expect(env('FOO="bar\nbaz"')).to eql('FOO' => "bar\nbaz")
|
||||||
|
@ -158,4 +158,8 @@ func TestLinesToIgnore(t *testing.T) {
|
||||||
t.Error("Indented comment wasn't ignored")
|
t.Error("Indented comment wasn't ignored")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// make sure we're not getting false positives
|
||||||
|
if isIgnoredLine("export OPTION_B='\\n'") {
|
||||||
|
t.Error("ignoring a perfectly valid line to parse")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue