diff --git a/cmd/main.go b/cmd/main.go index 062e99e..aa7ace8 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -13,6 +13,7 @@ import ( var valueFiles valueFilesList var targetDir string var profile string +var prefix string var verbose bool var dryRun bool @@ -46,6 +47,7 @@ func main() { f.BoolVarP(&dryRun, "dry-run", "d", false, "doesn't replace the file content") f.StringVarP(&targetDir, "target-dir", "o", "", "dir to output content") f.StringVarP(&profile, "profile", "p", "", "aws profile to fetch the ssm parameters") + f.StringVarP(&prefix, "prefix", "", "", "prefix to apply by default to parameters") cmd.MarkFlagRequired("values") @@ -56,7 +58,7 @@ func main() { } func run(cmd *cobra.Command, args []string) error { - funcMap := hssm.GetFuncMap(profile) + funcMap := hssm.GetFuncMap(profile, prefix) 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 86fa4b5..6ff5142 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) template.FuncMap { +func GetFuncMap(profile string, prefix string) template.FuncMap { // Clone the func map because we are adding context-specific functions. var funcMap template.FuncMap = map[string]interface{}{} for k, v := range sprig.GenericFuncMap() { @@ -58,7 +58,7 @@ func GetFuncMap(profile string) template.FuncMap { awsSession := newAWSSession(profile) funcMap["ssm"] = func(ssmPath string, options ...string) (string, error) { - optStr, err := resolveSSMParameter(awsSession, ssmPath, options) + optStr, err := resolveSSMParameter(awsSession, prefix, ssmPath, options) str := "" if optStr != nil { str = *optStr @@ -68,7 +68,7 @@ func GetFuncMap(profile string) template.FuncMap { return funcMap } -func resolveSSMParameter(session *session.Session, ssmPath string, options []string) (*string, error) { +func resolveSSMParameter(session *session.Session, defaultPrefix string, ssmPath string, options []string) (*string, error) { opts, err := handleOptions(options) if err != nil { return nil, err @@ -86,7 +86,14 @@ func resolveSSMParameter(session *session.Session, ssmPath string, options []str svc = ssm.New(session) } - return GetSSMParameter(svc, opts["prefix"]+ssmPath, defaultValue, true) + var ssmFullPath string + if optPrefix, exists := opts["prefix"]; exists { + ssmFullPath = optPrefix + ssmPath + } else { + ssmFullPath = defaultPrefix + ssmPath + } + + return GetSSMParameter(svc, ssmFullPath, defaultValue, true) } func handleOptions(options []string) (map[string]string, error) { @@ -106,9 +113,6 @@ func handleOptions(options []string) (map[string]string, error) { if _, exists := opts["required"]; !exists { opts["required"] = "true" } - if _, exists := opts["prefix"]; !exists { - opts["prefix"] = "" - } return opts, nil } diff --git a/internal/template_test.go b/internal/template_test.go index bcddb01..abbf2ab 100644 --- a/internal/template_test.go +++ b/internal/template_test.go @@ -63,7 +63,7 @@ func TestFailExecuteTemplate(t *testing.T) { func TestSsmFunctionExistsInFuncMap(t *testing.T) { t.Logf("\"ssm\" function should exist in function map.") - funcMap := GetFuncMap("") + funcMap := GetFuncMap("", "") keys := make([]string, len(funcMap)) for k := range funcMap { keys = append(keys, k) @@ -75,7 +75,7 @@ func TestSsmFunctionExistsInFuncMap(t *testing.T) { func TestSprigFunctionsExistInFuncMap(t *testing.T) { t.Logf("\"quote\" function (from sprig) should exist in function map.") - funcMap := GetFuncMap("") + funcMap := GetFuncMap("", "") keys := make([]string, len(funcMap)) for k := range funcMap { keys = append(keys, k)