Skip to content

Commit 6403008

Browse files
authored
fix: escape char (#14)
introduced more TERMINATING_CHARS and fixed #13
1 parent 850f1a6 commit 6403008

File tree

2 files changed

+56
-44
lines changed

2 files changed

+56
-44
lines changed

configmanager.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
const (
16-
TERMINATING_CHAR string = `[^\'\"\s\n]`
16+
TERMINATING_CHAR string = `[^\'\"\s\n\\]`
1717
)
1818

1919
type ConfigManager struct{}
@@ -41,13 +41,8 @@ func (c *ConfigManager) RetrieveWithInputReplaced(input string, config generator
4141
}
4242

4343
func retrieveWithInputReplaced(input string, gv GenerateAPI) (string, error) {
44-
tokens := []string{}
45-
for k := range generator.VarPrefix {
46-
matches := regexp.MustCompile(`(?s)`+regexp.QuoteMeta(string(k))+`.(`+TERMINATING_CHAR+`+)`).FindAllString(input, -1)
47-
tokens = append(tokens, matches...)
48-
}
4944

50-
m, err := retrieve(tokens, gv)
45+
m, err := retrieve(FindTokens(input), gv)
5146

5247
if err != nil {
5348
return "", err
@@ -56,6 +51,17 @@ func retrieveWithInputReplaced(input string, gv GenerateAPI) (string, error) {
5651
return replaceString(m, input), nil
5752
}
5853

54+
// FindTokens extracts all replaceable tokens
55+
// from a given input string
56+
func FindTokens(input string) []string {
57+
tokens := []string{}
58+
for k := range generator.VarPrefix {
59+
matches := regexp.MustCompile(`(?s)`+regexp.QuoteMeta(string(k))+`.(`+TERMINATING_CHAR+`+)`).FindAllString(input, -1)
60+
tokens = append(tokens, matches...)
61+
}
62+
return tokens
63+
}
64+
5965
// replaceString fills tokens in a provided input with their actual secret/config values
6066
func replaceString(inputMap generator.ParsedMap, inputString string) string {
6167

configmanager_test.go

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -76,29 +76,6 @@ func Test_retrieve(t *testing.T) {
7676
}
7777

7878
var (
79-
strT1 = `
80-
space: preserved
81-
indents: preserved
82-
arr: [ "FOO#/test" ]
83-
// comments preserved
84-
arr:
85-
- "FOO#/test"
86-
`
87-
strT2 = `
88-
// TOML
89-
[[somestuff]]
90-
key = "FOO#/test"
91-
`
92-
93-
strT3 = `
94-
// TOML
95-
[[somestuff]]
96-
key = FOO#/test
97-
key2 = FOO#/test
98-
key3 = FOO#/test
99-
key4 = FOO#/test
100-
`
101-
10279
strT4 = `
10380
export FOO='FOO#/test'
10481
export FOO1=FOO#/test
@@ -113,15 +90,21 @@ foo23 = FOO#/test
11390
)
11491

11592
func Test_retrieveWithInputReplaced(t *testing.T) {
116-
tests := []struct {
93+
tests := map[string]struct {
11794
name string
11895
input string
11996
genvar generator.Generatoriface
12097
expect string
12198
}{
122-
{
123-
name: "strYaml",
124-
input: strT1,
99+
"strYaml": {
100+
input: `
101+
space: preserved
102+
indents: preserved
103+
arr: [ "FOO#/test" ]
104+
// comments preserved
105+
arr:
106+
- "FOO#/test"
107+
`,
125108
genvar: &mockGenVars{},
126109
expect: `
127110
space: preserved
@@ -132,19 +115,28 @@ space: preserved
132115
- "val1"
133116
`,
134117
},
135-
{
136-
name: "strToml",
137-
input: strT2,
118+
"strToml": {
119+
input: `
120+
// TOML
121+
[[somestuff]]
122+
key = "FOO#/test"
123+
`,
138124
genvar: &mockGenVars{},
139125
expect: `
140126
// TOML
141127
[[somestuff]]
142128
key = "val1"
143129
`,
144130
},
145-
{
146-
name: "strTomlWithoutQuotes",
147-
input: strT3,
131+
"strTomlWithoutQuotes": {
132+
input: `
133+
// TOML
134+
[[somestuff]]
135+
key = FOO#/test
136+
key2 = FOO#/test
137+
key3 = FOO#/test
138+
key4 = FOO#/test
139+
`,
148140
genvar: &mockGenVars{},
149141
expect: `
150142
// TOML
@@ -155,14 +147,23 @@ key3 = val1
155147
key4 = val1
156148
`,
157149
},
158-
{
159-
name: "strTomlWithoutMultiline",
160-
input: strT4,
150+
"strTomlWithoutMultiline": {
151+
input: `
152+
export FOO='FOO#/test'
153+
export FOO1=FOO#/test
154+
export FOO2="FOO#/test"
155+
export FOO3=FOO#/test
156+
export FOO4=FOO#/test
157+
158+
[[section]]
159+
160+
foo23 = FOO#/test
161+
`,
161162
genvar: &mockGenVars{},
162163
expect: `
163164
export FOO='val1'
164165
export FOO1=val1
165-
export FOO2='val1'
166+
export FOO2="val1"
166167
export FOO3=val1
167168
export FOO4=val1
168169
@@ -171,6 +172,11 @@ export FOO4=val1
171172
foo23 = val1
172173
`,
173174
},
175+
"escaped input": {
176+
input: `"{\"patchPayloadTemplate\":\"{\\\"password\\\":\\\"FOO#/test\\\",\\\"passwordConfirm\\\":\\\"FOO#/test\\\"}\\n\"}"`,
177+
genvar: &mockGenVars{},
178+
expect: `"{\"patchPayloadTemplate\":\"{\\\"password\\\":\\\"val1\\\",\\\"passwordConfirm\\\":\\\"val1\\\"}\\n\"}"`,
179+
},
174180
}
175181

176182
for _, tt := range tests {

0 commit comments

Comments
 (0)