Skip to content

Commit

Permalink
Merge pull request #68 from codacy/feature/dev-environments
Browse files Browse the repository at this point in the history
feature: Add global prefix flag
  • Loading branch information
loliveiracodacy committed Mar 17, 2023
2 parents d779016 + 610b3d1 commit 7d0a581
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
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

0 comments on commit 7d0a581

Please sign in to comment.