@@ -5,6 +5,7 @@ import (
55 "fmt"
66 "os"
77 "reflect"
8+ "regexp"
89 "strings"
910
1011 "github.com/a8m/envsubst"
@@ -155,7 +156,8 @@ func LoadYAML(configFileName string, into interface{}) error {
155156 return nil
156157}
157158
158−// Performs the heavy lifting of the LoadYaml function
159+// loadAndParseYaml reads the config from the filesystem and
160+// execute the environment variable substitution
159161func loadAndParseYaml(configFileName string) ([]byte, error) {
160162 if configFileName == "" {
161163 return nil, errors.New("no configuration file provided")
@@ -166,12 +168,26 @@ func loadAndParseYaml(configFileName string) ([]byte, error) {
166168 return nil, fmt.Errorf("unable to load config file: %w", err)
167169 }
168170
169− // We now parse over the yaml with env substring, and fill in the ENV's
170− buffer, err := envsubst.Bytes(unparsedBuffer)
171+ modifiedBuffer, err := normalizeSubstitution(unparsedBuffer)
172+ if err != nil {
173+ return nil, fmt.Errorf("error normalizing substitution string : %w", err)
174+ }
175+
176+ buffer, err := envsubst.Bytes(modifiedBuffer)
171177 if err != nil {
172178 return nil, fmt.Errorf("error in substituting env variables : %w", err)
173179 }
174180
175181 return buffer, nil
182+}
183+
184+// normalizeSubstitution normalizes dollar signs ($) with numerals like
185+// $1 or $2 properly by correctly escaping them
186+func normalizeSubstitution(unparsedBuffer []byte) ([]byte, error) {
187+ unparsedString := string(unparsedBuffer)
188+
189+ regexPattern := regexp.MustCompile(`\$(\d+)`)
176190
191+ substitutedString := regexPattern.ReplaceAllString(unparsedString, `$$$$1`)
192+ return []byte(substitutedString), nil
177193}