Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support variable substitution in dotenv files #47

Merged
merged 4 commits into from
Jan 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions fixtures/substitutions.env
Original file line number Diff line number Diff line change
@@ -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}
15 changes: 11 additions & 4 deletions godotenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -259,11 +259,11 @@ 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, " ")
Expand Down Expand Up @@ -291,6 +291,13 @@ func parseValue(value string) string {
}
}

// expand variables
value = os.Expand(value, func(key string) string {
if val, ok := envMap[key]; ok {
return val
}
return ""
})
return value
}

Expand Down
19 changes: 16 additions & 3 deletions godotenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
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)
}
Expand Down Expand Up @@ -193,6 +193,19 @@ func TestLoadQuotedEnv(t *testing.T) {
loadEnvAndCompareValues(t, Load, envFileName, expectedValues, noopPresets)
}

func TestSubstituitions(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 TestActualEnvVarsAreLeftAlone(t *testing.T) {
os.Clearenv()
os.Setenv("OPTION_A", "actualenv")
Expand Down Expand Up @@ -280,7 +293,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)
}
Expand Down Expand Up @@ -348,7 +361,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\"")

Expand Down