Re-add global env variable substitution (#227)

Co-authored-by: Stanislau Arsoba <sarsoba@klika-tech.com>
This commit is contained in:
Andrey Novikov
2024-11-01 07:24:06 +09:00
committed by GitHub
parent 32e64fa834
commit a7f6c4c583
3 changed files with 15 additions and 1 deletions

View File

@ -3,3 +3,4 @@ OPTION_B=${OPTION_A}
OPTION_C=$OPTION_B OPTION_C=$OPTION_B
OPTION_D=${OPTION_A}${OPTION_B} OPTION_D=${OPTION_A}${OPTION_B}
OPTION_E=${OPTION_NOT_DEFINED} OPTION_E=${OPTION_NOT_DEFINED}
OPTION_F=${GLOBAL_OPTION}

View File

@ -207,15 +207,21 @@ func TestLoadQuotedEnv(t *testing.T) {
func TestSubstitutions(t *testing.T) { func TestSubstitutions(t *testing.T) {
envFileName := "fixtures/substitutions.env" envFileName := "fixtures/substitutions.env"
presets := map[string]string{
"GLOBAL_OPTION": "global",
}
expectedValues := map[string]string{ expectedValues := map[string]string{
"OPTION_A": "1", "OPTION_A": "1",
"OPTION_B": "1", "OPTION_B": "1",
"OPTION_C": "1", "OPTION_C": "1",
"OPTION_D": "11", "OPTION_D": "11",
"OPTION_E": "", "OPTION_E": "",
"OPTION_F": "global",
} }
loadEnvAndCompareValues(t, Load, envFileName, expectedValues, noopPresets) loadEnvAndCompareValues(t, Load, envFileName, expectedValues, presets)
} }
func TestExpanding(t *testing.T) { func TestExpanding(t *testing.T) {

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"os"
"regexp" "regexp"
"strings" "strings"
"unicode" "unicode"
@ -264,6 +265,12 @@ func expandVariables(v string, m map[string]string) string {
if submatch[1] == "\\" || submatch[2] == "(" { if submatch[1] == "\\" || submatch[2] == "(" {
return submatch[0][1:] return submatch[0][1:]
} else if submatch[4] != "" { } 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 m[submatch[4]]
} }
return s return s