Skip to content

Commit 277973b

Browse files
authored
Merge pull request #7 from codacy/target-dir-option
add target-dir flag
2 parents 59fab41 + e238c45 commit 277973b

File tree

5 files changed

+54
-15
lines changed

5 files changed

+54
-15
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ $ helm ssm [flags]
4343
```sh
4444
-d, --dry-run doesn't replace the file content
4545
-h, --help help for ssm
46+
-t, --target-dir string dir to output content
4647
-f, --values valueFilesList specify values in a YAML file (can specify multiple) (default [])
4748
-v, --verbose show the computed YAML values file/s
4849
```

cmd/main.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ package main
33
import (
44
"fmt"
55
"os"
6+
"path/filepath"
67
"strings"
78

89
hssm "github.com/codacy/helm-ssm/internal"
910
"github.com/spf13/cobra"
1011
)
1112

1213
var valueFiles valueFilesList
14+
var targetDir string
1315
var verbose bool
1416
var dryRun bool
1517

@@ -41,8 +43,10 @@ func main() {
4143
f.VarP(&valueFiles, "values", "f", "specify values in a YAML file (can specify multiple)")
4244
f.BoolVarP(&verbose, "verbose", "v", false, "show the computed YAML values file/s")
4345
f.BoolVarP(&dryRun, "dry-run", "d", false, "doesn't replace the file content")
46+
f.StringVarP(&targetDir, "target-dir", "o", "", "dir to output content")
4447

4548
cmd.MarkFlagRequired("values")
49+
4650
if err := cmd.Execute(); err != nil {
4751
fmt.Println(err)
4852
os.Exit(1)
@@ -52,9 +56,21 @@ func main() {
5256
func run(cmd *cobra.Command, args []string) error {
5357
funcMap := hssm.GetFuncMap()
5458
for _, filePath := range valueFiles {
55-
if err := hssm.ExecuteTemplate(filePath, funcMap, verbose, dryRun); err != nil {
59+
content, err := hssm.ExecuteTemplate(filePath, funcMap, verbose)
60+
if err != nil {
5661
return err
5762
}
63+
if !dryRun {
64+
write(filePath, targetDir, content)
65+
}
5866
}
5967
return nil
6068
}
69+
70+
func write(filePath string, targetDir string, content string) error {
71+
if targetDir != "" {
72+
fileName := filepath.Base(filePath)
73+
return hssm.WriteFileD(fileName, targetDir, content)
74+
}
75+
return hssm.WriteFile(filePath, content)
76+
}

internal/template.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"fmt"
66
"io/ioutil"
7+
"os"
78
"strconv"
89
"strings"
910
"text/template"
@@ -13,29 +14,37 @@ import (
1314
"k8s.io/helm/pkg/engine"
1415
)
1516

17+
// WriteFileD dumps a given content on the file with path `targetDir/fileName`.
18+
func WriteFileD(fileName string, targetDir string, content string) error {
19+
targetFilePath := targetDir + "/" + fileName
20+
_ = os.Mkdir(targetDir, os.ModePerm)
21+
return WriteFile(targetFilePath, content)
22+
}
23+
24+
// WriteFile dumps a given content on the file with path `targetFilePath`.
25+
func WriteFile(targetFilePath string, content string) error {
26+
return ioutil.WriteFile(targetFilePath, []byte(content), 0777)
27+
}
28+
1629
// ExecuteTemplate loads a template file, executes is against a given function map and writes the output
17-
func ExecuteTemplate(sourceFilePath string, funcMap template.FuncMap, verbose bool, dryRun bool) error {
30+
func ExecuteTemplate(sourceFilePath string, funcMap template.FuncMap, verbose bool) (string, error) {
1831
fileContent, err := ioutil.ReadFile(sourceFilePath)
1932
if err != nil {
20-
return err
33+
return "", err
2134
}
2235
t := template.New("ssmtpl").Funcs(funcMap)
2336
if _, err := t.Parse(string(fileContent)); err != nil {
24-
return err
37+
return "", err
2538
}
2639
var buf bytes.Buffer
2740
vals := map[string]interface{}{}
2841
if err := t.Execute(&buf, vals); err != nil {
29-
return err
42+
return "", err
3043
}
3144
if verbose {
3245
fmt.Println(string(buf.Bytes()))
3346
}
34-
if !dryRun {
35-
ioutil.WriteFile(sourceFilePath, buf.Bytes(), 0777)
36-
}
37-
38-
return nil
47+
return buf.String(), nil
3948
}
4049

4150
// GetFuncMap builds the relevant function map to helm_ssm

internal/template_test.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,22 @@ func TestExecuteTemplate(t *testing.T) {
2626
}
2727
defer syscall.Unlink(templateFilePath)
2828
ioutil.WriteFile(templateFilePath, []byte(templateContent), 0644)
29-
if err := ExecuteTemplate(templateFilePath, template.FuncMap{}, false, false); err != nil {
30-
t.Error(err)
29+
content, err := ExecuteTemplate(templateFilePath, template.FuncMap{}, false)
30+
if content != expectedOutput {
31+
t.Errorf("Expected content \"%s\". Got \"%s\"", expectedOutput, content)
32+
}
33+
}
34+
35+
func TestWriteFile(t *testing.T) {
36+
templateContent := "write_file_example: true"
37+
expectedOutput := "write_file_example: true"
38+
t.Logf("Template with content: %s , should out put a file with content: %s", templateContent, expectedOutput)
39+
40+
templateFilePath, err := createTempFile()
41+
if err != nil {
42+
panic(err)
3143
}
44+
WriteFile(templateFilePath, templateContent)
3245
fileContent, err := ioutil.ReadFile(templateFilePath)
3346
if err != nil {
3447
panic(err)
@@ -38,10 +51,10 @@ func TestExecuteTemplate(t *testing.T) {
3851
t.Errorf("Expected file with content \"%s\". Got \"%s\"", expectedOutput, content)
3952
}
4053
}
41-
4254
func TestFailExecuteTemplate(t *testing.T) {
4355
t.Logf("Non existing template should return \"no such file or directory\" error.")
44-
if err := ExecuteTemplate("", template.FuncMap{}, false, false); err == nil {
56+
_, err := ExecuteTemplate("", template.FuncMap{}, false)
57+
if err == nil {
4558
t.Error("Should fail with \"no such file or directory\"")
4659
}
4760
}

plugin.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: "ssm"
2-
version: "1.0.1"
2+
version: "1.0.2"
33
usage: "Inject AWS SSM parameters into Helm values files"
44
description: |-
55
Inject AWS SSM parameters into Helm values files

0 commit comments

Comments
 (0)