Skip to content

Commit

Permalink
Merge branch 'fix/expand-existing-var'
Browse files Browse the repository at this point in the history
* fix/expand-existing-var:
  Re-add global env variable substitution
  • Loading branch information
Envek committed Mar 26, 2024
2 parents 7765d9d + 83ef30c commit a563d53
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions fixtures/substitutions.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ OPTION_B=${OPTION_A}
OPTION_C=$OPTION_B
OPTION_D=${OPTION_A}${OPTION_B}
OPTION_E=${OPTION_NOT_DEFINED}
OPTION_F=${GLOBAL_OPTION}
8 changes: 7 additions & 1 deletion godotenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,21 @@ func TestLoadQuotedEnv(t *testing.T) {

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

presets := map[string]string{
"GLOBAL_OPTION": "global",
}

expectedValues := map[string]string{
"OPTION_A": "1",
"OPTION_B": "1",
"OPTION_C": "1",
"OPTION_D": "11",
"OPTION_E": "",
"OPTION_F": "global",
}

loadEnvAndCompareValues(t, Load, envFileName, expectedValues, noopPresets)
loadEnvAndCompareValues(t, Load, envFileName, expectedValues, presets)
}

func TestExpanding(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"fmt"
"os"
"regexp"
"strings"
"unicode"
Expand Down Expand Up @@ -264,6 +265,12 @@ func expandVariables(v string, m map[string]string) string {
if submatch[1] == "\\" || submatch[2] == "(" {
return submatch[0][1:]
} else if submatch[4] != "" {
if val, ok := m[submatch[4]]; ok {
return val
}
if val, ok := os.LookupEnv(submatch[4]); ok {
return val
}
return m[submatch[4]]
}
return s
Expand Down

0 comments on commit a563d53

Please sign in to comment.