@@ -10,14 +10,18 @@ package compose
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 {}
@@ -49,22 +53,30 @@ func buildContext(metadata score.WorkloadMeta, resources score.ResourcesSpecs) (
4953
5054// Substitute replaces all matching '${...}' templates in a source string
5155func (ctx * templatesContext ) Substitute (src string ) string {
52- return os .Expand (src , ctx .mapVar )
56+ return placeholderRegEx .ReplaceAllStringFunc (src , func (str string ) string {
57+ // WORKAROUND: ReplaceAllStringFunc(..) does not provide match details
58+ // https://github.com/golang/go/issues/5690
59+ var matches = placeholderRegEx .FindStringSubmatch (str )
60+
61+ // SANITY CHECK
62+ if len (matches ) != 3 {
63+ log .Printf ("Error: could not find a proper match in previously captured string fragment" )
64+ return src
65+ }
66+
67+ // EDGE CASE: Captures "$$" sequences and empty templates "${}"
68+ if matches [2 ] == "" {
69+ return matches [1 ]
70+ }
71+
72+ return ctx .mapVar (matches [2 ])
73+ })
5374}
5475
5576// MapVar replaces objects and properties references with corresponding values
5677// Returns an empty string if the reference can't be resolved
5778func (ctx * templatesContext ) mapVar (ref string ) string {
58- if ref == "" {
59- return ""
60- }
61-
62- // NOTE: os.Expand(..) would invoke a callback function with "$" as an argument for escaped sequences.
63- // "$${abc}" is treated as "$$" pattern and "{abc}" static text.
64- // The first segment (pattern) would trigger a callback function call.
65- // By returning "$" value we would ensure that escaped sequences would remain in the source text.
66- // For example "$${abc}" would result in "${abc}" after os.Expand(..) call.
67- if ref == "$" {
79+ if ref == "" || ref == "$" {
6880 return ref
6981 }
7082
0 commit comments