@@ -10,14 +10,18 @@ package helm
1010import (
1111 "fmt"
1212 "log"
13- "os "
13+ "regexp "
1414 "strings"
1515
1616 "github.com/mitchellh/mapstructure"
1717
1818 score "github.com/score-spec/score-go/types"
1919)
2020
21+ var (
22+ placeholderRegEx = regexp .MustCompile (`\$(\$|{([a-zA-Z0-9.\-_\[\]"'#]+)})` )
23+ )
24+
2125// templatesContext ia an utility type that provides a context for '${...}' templates substitution
2226type templatesContext struct {
2327 meta map [string ]interface {}
@@ -46,22 +50,30 @@ func buildContext(metadata score.WorkloadMeta, resources score.ResourcesSpecs, v
4650
4751// Substitute replaces all matching '${...}' templates in a source string
4852func (ctx * templatesContext ) Substitute (src string ) string {
49- return os .Expand (src , ctx .mapVar )
53+ return placeholderRegEx .ReplaceAllStringFunc (src , func (str string ) string {
54+ // WORKAROUND: ReplaceAllStringFunc(..) does not provide match details
55+ // https://github.com/golang/go/issues/5690
56+ var matches = placeholderRegEx .FindStringSubmatch (str )
57+
58+ // SANITY CHECK
59+ if len (matches ) != 3 {
60+ log .Printf ("Error: could not find a proper match in previously captured string fragment" )
61+ return src
62+ }
63+
64+ // EDGE CASE: Captures "$$" sequences and empty templates "${}"
65+ if matches [2 ] == "" {
66+ return matches [1 ]
67+ }
68+
69+ return ctx .mapVar (matches [2 ])
70+ })
5071}
5172
5273// MapVar replaces objects and properties references with corresponding values
5374// Returns an empty string if the reference can't be resolved
5475func (ctx * templatesContext ) mapVar (ref string ) string {
55- if ref == "" {
56- return ""
57- }
58-
59- // NOTE: os.Expand(..) would invoke a callback function with "$" as an argument for escaped sequences.
60- // "$${abc}" is treated as "$$" pattern and "{abc}" static text.
61- // The first segment (pattern) would trigger a callback function call.
62- // By returning "$" value we would ensure that escaped sequences would remain in the source text.
63- // For example "$${abc}" would result in "${abc}" after os.Expand(..) call.
64- if ref == "$" {
76+ if ref == "" || ref == "$" {
6577 return ref
6678 }
6779
0 commit comments