Skip to content

Commit

Permalink
Merge pull request #18 from k1LoW/fix-handling-env-placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW authored Nov 21, 2022
2 parents d172ec0 + 0588820 commit a58db0c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
19 changes: 18 additions & 1 deletion expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,29 @@ multi: |
current.url == 'https://example.com/#about'
`},
{
`key: "hello$"`,
map[string]string{},
`key: "hello$"
`,
},
{
`key: "hello$ ${WORLD}"`,
map[string]string{
"WORLD": "world",
},
`key: "hello$ world"
`,
},
}
for _, tt := range tests {
for k, v := range tt.envs {
t.Setenv(k, v)
}
got := ExpandYAML(tt.in, os.LookupEnv)
mapper := func(in string) (string, bool) {
return os.LookupEnv(in)
}
got := ExpandYAML(tt.in, mapper)
if got != tt.want {
t.Errorf("got\n%v\nwant\n%v", got, tt.want)
}
Expand Down
12 changes: 10 additions & 2 deletions repfn.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,22 @@ import (

type repFn func(in string) (string, error)

var envPlaceholderRe = regexp.MustCompile(`\${.+}`)

func InterpolateRepFn(mapping func(string) (string, bool)) repFn {
mapper := Mapper{mapping: mapping}
return func(in string) (string, error) {
replace, err := interpolate.Interpolate(mapper, in)
if !envPlaceholderRe.MatchString(in) {
return in, nil
}
r := strings.NewReplacer("${", "${", "$", "__NOT_INTERPOLATE_START__")
rr := strings.NewReplacer("__NOT_INTERPOLATE_START__", "$")

replace, err := interpolate.Interpolate(mapper, r.Replace(in))
if err != nil {
return in, err
}
return replace, nil
return rr.Replace(replace), nil
}
}

Expand Down

0 comments on commit a58db0c

Please sign in to comment.