Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion configmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func retrieveWithInputReplaced(input string, gv GenerateAPI) (string, error) {
func FindTokens(input string) []string {
tokens := []string{}
for k := range generator.VarPrefix {
matches := regexp.MustCompile(`(?s)`+regexp.QuoteMeta(string(k))+`.(`+TERMINATING_CHAR+`+)`).FindAllString(input, -1)
matches := regexp.MustCompile(regexp.QuoteMeta(string(k))+`.(`+TERMINATING_CHAR+`+)`).FindAllString(input, -1)
tokens = append(tokens, matches...)
}
return tokens
Expand Down
64 changes: 64 additions & 0 deletions configmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"reflect"
"sort"
"testing"

"github.com/dnitsch/configmanager/internal/testutils"
Expand Down Expand Up @@ -519,3 +520,66 @@ func Test_RetrieveMarshalledJson(t *testing.T) {
})
}
}

func TestFindTokens(t *testing.T) {
ttests := map[string]struct {
input string
expect []string
}{
"extract from text correctly": {
`Where does it come from?
Contrary to popular belief,
Lorem Ipsum is AWSPARAMSTR:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj <= in middle of sentencenot simply random text.
It has roots in a piece of classical Latin literature from 45
BC, making it over 2000 years old. Richard McClintock, a Latin professor at
Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, c
onsectetur, from a Lorem Ipsum passage , at the end of line => AWSPARAMSTR:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj
and going through the cites of the word in c
lassical literature, discovered the undoubtable source. Lorem Ipsum comes from secti
ons in singles =>'AWSPARAMSTR:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj'1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil)
in doubles => "AWSPARAMSTR:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj"
by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular
during the :=> embedded in text RenaissanceAWSPARAMSTR:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj[] embedded in text <=:
The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.`,
[]string{
"AWSPARAMSTR:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj",
"AWSPARAMSTR:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj",
"AWSPARAMSTR:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj",
"AWSPARAMSTR:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj",
"AWSPARAMSTR:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj[]"},
},
"unknown implementation not picked up": {
`foo: AWSPARAMSTR:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj
bar: AWSPARAMSTR#bar/djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj[version:123]
unknown: GCPPARAMSTR:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj`,
[]string{
"AWSPARAMSTR:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj",
"AWSPARAMSTR#bar/djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj[version:123]"},
},
"all implementations": {
`param: AWSPARAMSTR:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj
secretsmgr: AWSSECRETS#bar/djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj[version:123]
gcp: GCPSECRETS:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj
vault: VAULT:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj[]
som othere strufsd
azkv: AZKVSECRET:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj`,
[]string{
"GCPSECRETS:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj",
"AWSPARAMSTR:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj",
"AWSSECRETS#bar/djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj[version:123]",
"AZKVSECRET:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj",
"VAULT:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj[]"},
},
}
for name, tt := range ttests {
t.Run(name, func(t *testing.T) {
got := FindTokens(tt.input)
sort.Strings(got)
sort.Strings(tt.expect)

if !reflect.DeepEqual(got, tt.expect) {
t.Errorf("input=(%q)\n\ngot: %v\n\nwant: %v", tt.input, got, tt.expect)
}
})
}
}
4 changes: 2 additions & 2 deletions pkg/generator/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,6 @@ func (rs *retrieveStrategy) stripPrefix(in string, prefix ImplementationPrefix)
// stripPrefix
func stripPrefix(in string, prefix ImplementationPrefix, tokenSeparator, keySeparator string) string {
t := in
b := regexp.MustCompile(fmt.Sprintf(`[%s].*`, keySeparator)).ReplaceAll([]byte(t), []byte(""))
return strings.Replace(string(b), fmt.Sprintf("%s%s", prefix, tokenSeparator), "", 1)
b := regexp.MustCompile(fmt.Sprintf(`[%s].*`, keySeparator)).ReplaceAllString(t, "")
return strings.Replace(b, fmt.Sprintf("%s%s", prefix, tokenSeparator), "", 1)
}