Skip to content

Commit 7d0a581

Browse files
Merge pull request #68 from codacy/feature/dev-environments
feature: Add global prefix flag
2 parents d779016 + 610b3d1 commit 7d0a581

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ bin
33
_dist
44
.version
55

6+
.idea/
67
.vscode/
78
*~
89
.*.swp
910

11+

cmd/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var verbose bool
1717
var dryRun bool
1818
var clean bool
1919
var tagCleaned string
20+
var prefix string
2021

2122
type valueFilesList []string
2223

@@ -50,6 +51,7 @@ func main() {
5051
f.StringVarP(&profile, "profile", "p", "", "aws profile to fetch the ssm parameters")
5152
f.BoolVarP(&clean, "clean", "c", false, "clean all template commands from file")
5253
f.StringVarP(&tagCleaned, "tag-cleaned", "t", "", "replace cleaned template commands with given string")
54+
f.StringVarP(&prefix, "prefix", "P", "", "prefix for all parameters without affecting the path. ignored if individual prefix is defined")
5355

5456
cmd.MarkFlagRequired("values")
5557

@@ -60,7 +62,7 @@ func main() {
6062
}
6163

6264
func run(cmd *cobra.Command, args []string) error {
63-
funcMap := hssm.GetFuncMap(profile, clean, tagCleaned)
65+
funcMap := hssm.GetFuncMap(profile, prefix, clean, tagCleaned)
6466
for _, filePath := range valueFiles {
6567
content, err := hssm.ExecuteTemplate(filePath, funcMap, verbose)
6668
if err != nil {

internal/template.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func ExecuteTemplate(sourceFilePath string, funcMap template.FuncMap, verbose bo
4949
}
5050

5151
// GetFuncMap builds the relevant function map to helm_ssm
52-
func GetFuncMap(profile string, clean bool, tagCleaned string) template.FuncMap {
52+
func GetFuncMap(profile string, prefix string, clean bool, tagCleaned string) template.FuncMap {
5353

5454
cleanFunc := func(...interface{}) (string, error) {
5555
return tagCleaned, nil
@@ -69,6 +69,17 @@ func GetFuncMap(profile string, clean bool, tagCleaned string) template.FuncMap
6969
funcMap["ssm"] = cleanFunc
7070
} else {
7171
funcMap["ssm"] = func(ssmPath string, options ...string) (string, error) {
72+
var hasPrefix = false
73+
for _, s := range options {
74+
if strings.HasPrefix(s, "prefix") {
75+
hasPrefix = true
76+
}
77+
}
78+
79+
if !hasPrefix {
80+
options = append(options, fmt.Sprintf("prefix=%s", prefix))
81+
}
82+
7283
optStr, err := resolveSSMParameter(awsSession, ssmPath, options)
7384
str := ""
7485
if optStr != nil {

internal/template_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TestCleanTemplate(t *testing.T) {
4545
}
4646
defer syscall.Unlink(templateFilePath)
4747
ioutil.WriteFile(templateFilePath, []byte(templateContent), 0644)
48-
cleanFuncMap := GetFuncMap("DUMMY", true, "")
48+
cleanFuncMap := GetFuncMap("DUMMY", "", true, "")
4949
content, _ := ExecuteTemplate(templateFilePath, cleanFuncMap, false)
5050
if content != expectedOutput {
5151
t.Errorf("Expected content \"%s\". Got \"%s\"", expectedOutput, content)
@@ -64,7 +64,7 @@ func TestCleanAndTagTemplate(t *testing.T) {
6464
}
6565
defer syscall.Unlink(templateFilePath)
6666
ioutil.WriteFile(templateFilePath, []byte(templateContent), 0644)
67-
cleanFuncMap := GetFuncMap("DUMMY", true, cleanTag)
67+
cleanFuncMap := GetFuncMap("DUMMY", "", true, cleanTag)
6868
content, _ := ExecuteTemplate(templateFilePath, cleanFuncMap, false)
6969
if content != expectedOutput {
7070
t.Errorf("Expected content \"%s\". Got \"%s\"", expectedOutput, content)
@@ -100,7 +100,7 @@ func TestFailExecuteTemplate(t *testing.T) {
100100

101101
func TestSsmFunctionExistsInFuncMap(t *testing.T) {
102102
t.Logf("\"ssm\" function should exist in function map.")
103-
funcMap := GetFuncMap("", false, "")
103+
funcMap := GetFuncMap("", "", false, "")
104104
keys := make([]string, len(funcMap))
105105
for k := range funcMap {
106106
keys = append(keys, k)
@@ -112,7 +112,7 @@ func TestSsmFunctionExistsInFuncMap(t *testing.T) {
112112

113113
func TestSprigFunctionsExistInFuncMap(t *testing.T) {
114114
t.Logf("\"quote\" function (from sprig) should exist in function map.")
115-
funcMap := GetFuncMap("", false, "")
115+
funcMap := GetFuncMap("", "", false, "")
116116
keys := make([]string, len(funcMap))
117117
for k := range funcMap {
118118
keys = append(keys, k)

0 commit comments

Comments
 (0)