Skip to content
This repository was archived by the owner on Jul 4, 2024. It is now read-only.

Commit edd5b26

Browse files
committed
Fix: Do not substitute strings without brackets
Signed-off-by: Eugene Yarshevich <yarshevich@gmail.com>
1 parent 1ec922c commit edd5b26

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

internal/helm/templates.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@ package helm
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{}
@@ -46,22 +50,30 @@ func buildContext(metadata score.WorkloadMeta, resources score.ResourcesSpecs, v
4650

4751
// Substitute replaces all matching '${...}' templates in a source string
4852
func (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
5475
func (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

internal/helm/templates_test.go

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

9696
assert.Equal(t, "", context.Substitute(""))
9797
assert.Equal(t, "abc", context.Substitute("abc"))
98+
assert.Equal(t, "$abc", context.Substitute("$abc"))
9899
assert.Equal(t, "abc $ abc", context.Substitute("abc $$ abc"))
99100
assert.Equal(t, "${abc}", context.Substitute("$${abc}"))
100101

0 commit comments

Comments
 (0)