From 610b3d16567438ea6328b5c9c39e5ad3ac06ba74 Mon Sep 17 00:00:00 2001 From: Lucas Oliveira Date: Thu, 16 Feb 2023 15:57:31 +0000 Subject: [PATCH] feature: Add global prefix flag --- .gitignore | 2 ++ cmd/main.go | 4 +++- internal/template.go | 13 ++++++++++++- internal/template_test.go | 8 ++++---- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 8675ad8..67d2cc6 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,9 @@ bin _dist .version +.idea/ .vscode/ *~ .*.swp + diff --git a/cmd/main.go b/cmd/main.go index 26f3429..e5b1209 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -17,6 +17,7 @@ var verbose bool var dryRun bool var clean bool var tagCleaned string +var prefix string type valueFilesList []string @@ -50,6 +51,7 @@ func main() { f.StringVarP(&profile, "profile", "p", "", "aws profile to fetch the ssm parameters") f.BoolVarP(&clean, "clean", "c", false, "clean all template commands from file") f.StringVarP(&tagCleaned, "tag-cleaned", "t", "", "replace cleaned template commands with given string") + f.StringVarP(&prefix, "prefix", "P", "", "prefix for all parameters without affecting the path. ignored if individual prefix is defined") cmd.MarkFlagRequired("values") @@ -60,7 +62,7 @@ func main() { } func run(cmd *cobra.Command, args []string) error { - funcMap := hssm.GetFuncMap(profile, clean, tagCleaned) + funcMap := hssm.GetFuncMap(profile, prefix, clean, tagCleaned) for _, filePath := range valueFiles { content, err := hssm.ExecuteTemplate(filePath, funcMap, verbose) if err != nil { diff --git a/internal/template.go b/internal/template.go index a1d533a..c441db5 100644 --- a/internal/template.go +++ b/internal/template.go @@ -49,7 +49,7 @@ func ExecuteTemplate(sourceFilePath string, funcMap template.FuncMap, verbose bo } // GetFuncMap builds the relevant function map to helm_ssm -func GetFuncMap(profile string, clean bool, tagCleaned string) template.FuncMap { +func GetFuncMap(profile string, prefix string, clean bool, tagCleaned string) template.FuncMap { cleanFunc := func(...interface{}) (string, error) { return tagCleaned, nil @@ -69,6 +69,17 @@ func GetFuncMap(profile string, clean bool, tagCleaned string) template.FuncMap funcMap["ssm"] = cleanFunc } else { funcMap["ssm"] = func(ssmPath string, options ...string) (string, error) { + var hasPrefix = false + for _, s := range options { + if strings.HasPrefix(s, "prefix") { + hasPrefix = true + } + } + + if !hasPrefix { + options = append(options, fmt.Sprintf("prefix=%s", prefix)) + } + optStr, err := resolveSSMParameter(awsSession, ssmPath, options) str := "" if optStr != nil { diff --git a/internal/template_test.go b/internal/template_test.go index 4501ca6..5f8d0f8 100644 --- a/internal/template_test.go +++ b/internal/template_test.go @@ -45,7 +45,7 @@ func TestCleanTemplate(t *testing.T) { } defer syscall.Unlink(templateFilePath) ioutil.WriteFile(templateFilePath, []byte(templateContent), 0644) - cleanFuncMap := GetFuncMap("DUMMY", true, "") + cleanFuncMap := GetFuncMap("DUMMY", "", true, "") content, _ := ExecuteTemplate(templateFilePath, cleanFuncMap, false) if content != expectedOutput { t.Errorf("Expected content \"%s\". Got \"%s\"", expectedOutput, content) @@ -64,7 +64,7 @@ func TestCleanAndTagTemplate(t *testing.T) { } defer syscall.Unlink(templateFilePath) ioutil.WriteFile(templateFilePath, []byte(templateContent), 0644) - cleanFuncMap := GetFuncMap("DUMMY", true, cleanTag) + cleanFuncMap := GetFuncMap("DUMMY", "", true, cleanTag) content, _ := ExecuteTemplate(templateFilePath, cleanFuncMap, false) if content != expectedOutput { t.Errorf("Expected content \"%s\". Got \"%s\"", expectedOutput, content) @@ -100,7 +100,7 @@ func TestFailExecuteTemplate(t *testing.T) { func TestSsmFunctionExistsInFuncMap(t *testing.T) { t.Logf("\"ssm\" function should exist in function map.") - funcMap := GetFuncMap("", false, "") + funcMap := GetFuncMap("", "", false, "") keys := make([]string, len(funcMap)) for k := range funcMap { keys = append(keys, k) @@ -112,7 +112,7 @@ func TestSsmFunctionExistsInFuncMap(t *testing.T) { func TestSprigFunctionsExistInFuncMap(t *testing.T) { t.Logf("\"quote\" function (from sprig) should exist in function map.") - funcMap := GetFuncMap("", false, "") + funcMap := GetFuncMap("", "", false, "") keys := make([]string, len(funcMap)) for k := range funcMap { keys = append(keys, k)