mirror of
https://github.com/joho/godotenv.git
synced 2025-06-27 13:04:03 +00:00
Compare commits
11 Commits
fix_quotin
...
v1.3.0
Author | SHA1 | Date | |
---|---|---|---|
23d116af35 | |||
2d8b3aab88 | |||
1709ab122c | |||
8ad714e304 | |||
6bb0851667 | |||
06e67b5ef3 | |||
2707e9ff66 | |||
50c29652a0 | |||
33977c2d8d | |||
9be76b3741 | |||
6d367c18ed |
@ -160,4 +160,4 @@ Linux: [](
|
||||
|
||||
## Who?
|
||||
|
||||
The original library [dotenv](https://github.com/bkeepers/dotenv) was written by [Brandon Keepers](http://opensoul.org/), and this port was done by [John Barton](http://whoisjohnbarton.com) based off the tests/fixtures in the original library.
|
||||
The original library [dotenv](https://github.com/bkeepers/dotenv) was written by [Brandon Keepers](http://opensoul.org/), and this port was done by [John Barton](https://johnbarton.co/) based off the tests/fixtures in the original library.
|
||||
|
5
fixtures/substitutions.env
Normal file
5
fixtures/substitutions.env
Normal file
@ -0,0 +1,5 @@
|
||||
OPTION_A=1
|
||||
OPTION_B=${OPTION_A}
|
||||
OPTION_C=$OPTION_B
|
||||
OPTION_D=${OPTION_A}${OPTION_B}
|
||||
OPTION_E=${OPTION_NOT_DEFINED}
|
50
godotenv.go
50
godotenv.go
@ -112,7 +112,7 @@ func Parse(r io.Reader) (envMap map[string]string, err error) {
|
||||
for _, fullLine := range lines {
|
||||
if !isIgnoredLine(fullLine) {
|
||||
var key, value string
|
||||
key, value, err = parseLine(fullLine)
|
||||
key, value, err = parseLine(fullLine, envMap)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
@ -209,7 +209,7 @@ func readFile(filename string) (envMap map[string]string, err error) {
|
||||
return Parse(file)
|
||||
}
|
||||
|
||||
func parseLine(line string) (key string, value string, err error) {
|
||||
func parseLine(line string, envMap map[string]string) (key string, value string, err error) {
|
||||
if len(line) == 0 {
|
||||
err = errors.New("zero length string")
|
||||
return
|
||||
@ -259,23 +259,30 @@ func parseLine(line string) (key string, value string, err error) {
|
||||
key = strings.Trim(key, " ")
|
||||
|
||||
// Parse the value
|
||||
value = parseValue(splitString[1])
|
||||
value = parseValue(splitString[1], envMap)
|
||||
return
|
||||
}
|
||||
|
||||
func parseValue(value string) string {
|
||||
func parseValue(value string, envMap map[string]string) string {
|
||||
|
||||
// trim
|
||||
value = strings.Trim(value, " ")
|
||||
|
||||
// check if we've got quoted values or possible escapes
|
||||
if len(value) > 1 {
|
||||
first := string(value[0:1])
|
||||
last := string(value[len(value)-1:])
|
||||
if first == last && strings.ContainsAny(first, `"'`) {
|
||||
rs := regexp.MustCompile(`\A'(.*)'\z`)
|
||||
singleQuotes := rs.FindStringSubmatch(value)
|
||||
|
||||
rd := regexp.MustCompile(`\A"(.*)"\z`)
|
||||
doubleQuotes := rd.FindStringSubmatch(value)
|
||||
|
||||
if singleQuotes != nil || doubleQuotes != nil {
|
||||
// pull the quotes off the edges
|
||||
value = value[1 : len(value)-1]
|
||||
// handle escapes
|
||||
}
|
||||
|
||||
if doubleQuotes != nil {
|
||||
// expand newlines
|
||||
escapeRegex := regexp.MustCompile(`\\.`)
|
||||
value = escapeRegex.ReplaceAllStringFunc(value, func(match string) string {
|
||||
c := strings.TrimPrefix(match, `\`)
|
||||
@ -285,15 +292,40 @@ func parseValue(value string) string {
|
||||
case "r":
|
||||
return "\r"
|
||||
default:
|
||||
return c
|
||||
return match
|
||||
}
|
||||
})
|
||||
// unescape characters
|
||||
e := regexp.MustCompile(`\\([^$])`)
|
||||
value = e.ReplaceAllString(value, "$1")
|
||||
}
|
||||
|
||||
if singleQuotes == nil {
|
||||
value = expandVariables(value, envMap)
|
||||
}
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
func expandVariables(v string, m map[string]string) string {
|
||||
r := regexp.MustCompile(`(\\)?(\$)(\()?\{?([A-Z0-9_]+)?\}?`)
|
||||
|
||||
return r.ReplaceAllStringFunc(v, func(s string) string {
|
||||
submatch := r.FindStringSubmatch(s)
|
||||
|
||||
if submatch == nil {
|
||||
return s
|
||||
}
|
||||
if submatch[1] == "\\" || submatch[2] == "(" {
|
||||
return submatch[0][1:]
|
||||
} else if submatch[4] != "" {
|
||||
return m[submatch[4]]
|
||||
}
|
||||
return s
|
||||
})
|
||||
}
|
||||
|
||||
func isIgnoredLine(line string) bool {
|
||||
trimmedLine := strings.Trim(line, " \n\t")
|
||||
return len(trimmedLine) == 0 || strings.HasPrefix(trimmedLine, "#")
|
||||
|
@ -6,12 +6,13 @@ import (
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var noopPresets = make(map[string]string)
|
||||
|
||||
func parseAndCompare(t *testing.T, rawEnvLine string, expectedKey string, expectedValue string) {
|
||||
key, value, _ := parseLine(rawEnvLine)
|
||||
key, value, _ := parseLine(rawEnvLine, noopPresets)
|
||||
if key != expectedKey || value != expectedValue {
|
||||
t.Errorf("Expected '%v' to parse as '%v' => '%v', got '%v' => '%v' instead", rawEnvLine, expectedKey, expectedValue, key, value)
|
||||
}
|
||||
@ -161,7 +162,7 @@ func TestLoadExportedEnv(t *testing.T) {
|
||||
envFileName := "fixtures/exported.env"
|
||||
expectedValues := map[string]string{
|
||||
"OPTION_A": "2",
|
||||
"OPTION_B": "\n",
|
||||
"OPTION_B": "\\n",
|
||||
}
|
||||
|
||||
loadEnvAndCompareValues(t, Load, envFileName, expectedValues, noopPresets)
|
||||
@ -182,7 +183,7 @@ func TestLoadQuotedEnv(t *testing.T) {
|
||||
"OPTION_A": "1",
|
||||
"OPTION_B": "2",
|
||||
"OPTION_C": "",
|
||||
"OPTION_D": "\n",
|
||||
"OPTION_D": "\\n",
|
||||
"OPTION_E": "1",
|
||||
"OPTION_F": "2",
|
||||
"OPTION_G": "",
|
||||
@ -193,6 +194,83 @@ func TestLoadQuotedEnv(t *testing.T) {
|
||||
loadEnvAndCompareValues(t, Load, envFileName, expectedValues, noopPresets)
|
||||
}
|
||||
|
||||
func TestSubstitutions(t *testing.T) {
|
||||
envFileName := "fixtures/substitutions.env"
|
||||
expectedValues := map[string]string{
|
||||
"OPTION_A": "1",
|
||||
"OPTION_B": "1",
|
||||
"OPTION_C": "1",
|
||||
"OPTION_D": "11",
|
||||
"OPTION_E": "",
|
||||
}
|
||||
|
||||
loadEnvAndCompareValues(t, Load, envFileName, expectedValues, noopPresets)
|
||||
}
|
||||
|
||||
func TestExpanding(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
expected map[string]string
|
||||
}{
|
||||
{
|
||||
"expands variables found in values",
|
||||
"FOO=test\nBAR=$FOO",
|
||||
map[string]string{"FOO": "test", "BAR": "test"},
|
||||
},
|
||||
{
|
||||
"parses variables wrapped in brackets",
|
||||
"FOO=test\nBAR=${FOO}bar",
|
||||
map[string]string{"FOO": "test", "BAR": "testbar"},
|
||||
},
|
||||
{
|
||||
"expands undefined variables to an empty string",
|
||||
"BAR=$FOO",
|
||||
map[string]string{"BAR": ""},
|
||||
},
|
||||
{
|
||||
"expands variables in double quoted strings",
|
||||
"FOO=test\nBAR=\"quote $FOO\"",
|
||||
map[string]string{"FOO": "test", "BAR": "quote test"},
|
||||
},
|
||||
{
|
||||
"does not expand variables in single quoted strings",
|
||||
"BAR='quote $FOO'",
|
||||
map[string]string{"BAR": "quote $FOO"},
|
||||
},
|
||||
{
|
||||
"does not expand escaped variables",
|
||||
`FOO="foo\$BAR"`,
|
||||
map[string]string{"FOO": "foo$BAR"},
|
||||
},
|
||||
{
|
||||
"does not expand escaped variables",
|
||||
`FOO="foo\${BAR}"`,
|
||||
map[string]string{"FOO": "foo${BAR}"},
|
||||
},
|
||||
{
|
||||
"does not expand escaped variables",
|
||||
"FOO=test\nBAR=\"foo\\${FOO} ${FOO}\"",
|
||||
map[string]string{"FOO": "test", "BAR": "foo${FOO} test"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
env, err := Parse(strings.NewReader(tt.input))
|
||||
if err != nil {
|
||||
t.Errorf("Error: %s", err.Error())
|
||||
}
|
||||
for k, v := range tt.expected {
|
||||
if strings.Compare(env[k], v) != 0 {
|
||||
t.Errorf("Expected: %s, Actual: %s", v, env[k])
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestActualEnvVarsAreLeftAlone(t *testing.T) {
|
||||
os.Clearenv()
|
||||
os.Setenv("OPTION_A", "actualenv")
|
||||
@ -234,7 +312,7 @@ func TestParsing(t *testing.T) {
|
||||
|
||||
// parses export keyword
|
||||
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
|
||||
// expect(env('FOO="bar\nbaz"')).to eql('FOO' => "bar\nbaz")
|
||||
@ -280,7 +358,7 @@ func TestParsing(t *testing.T) {
|
||||
// it 'throws an error if line format is incorrect' do
|
||||
// expect{env('lol$wut')}.to raise_error(Dotenv::FormatError)
|
||||
badlyFormattedLine := "lol$wut"
|
||||
_, _, err := parseLine(badlyFormattedLine)
|
||||
_, _, err := parseLine(badlyFormattedLine, noopPresets)
|
||||
if err == nil {
|
||||
t.Errorf("Expected \"%v\" to return error, but it didn't", badlyFormattedLine)
|
||||
}
|
||||
@ -348,7 +426,7 @@ func TestWrite(t *testing.T) {
|
||||
//but single quotes are left alone
|
||||
writeAndCompare(`key=va'lu'e`, `key="va'lu'e"`)
|
||||
// newlines, backslashes, and some other special chars are escaped
|
||||
writeAndCompare(`foo="$ba\n\r\\r!"`, `foo="\$ba\n\r\\r\!"`)
|
||||
writeAndCompare(`foo="\n\r\\r!"`, `foo="\n\r\\r\!"`)
|
||||
// lines should be sorted
|
||||
writeAndCompare("foo=bar\nbaz=buzz", "baz=\"buzz\"\nfoo=\"bar\"")
|
||||
|
||||
|
Reference in New Issue
Block a user