Skip to content

Commit 849cfe5

Browse files
committed
Fix: Do not substitute strings without brackets
Signed-off-by: Eugene Yarshevich <[email protected]>
1 parent c1ef740 commit 849cfe5

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

internal/compose/templates.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@ package compose
1010
import (
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
2226
type 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
5155
func (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
5778
func (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

internal/compose/templates_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ func TestSubstitute(t *testing.T) {
7171

7272
assert.Equal(t, "", ctx.Substitute(""))
7373
assert.Equal(t, "abc", ctx.Substitute("abc"))
74+
assert.Equal(t, "$abc", ctx.Substitute("$abc"))
7475
assert.Equal(t, "abc $ abc", ctx.Substitute("abc $$ abc"))
7576
assert.Equal(t, "${abc}", ctx.Substitute("$${abc}"))
7677

0 commit comments

Comments
 (0)