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: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ bin
_dist
.version

.idea/
.vscode/
*~
.*.swp


4 changes: 3 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var verbose bool
var dryRun bool
var clean bool
var tagCleaned string
var prefix string

type valueFilesList []string

Expand Down Expand Up @@ -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")

Expand All @@ -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 {
Expand Down
13 changes: 12 additions & 1 deletion internal/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions internal/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down