Skip to content

Commit 6e0cbd8

Browse files
AlinaGoagafoot
andauthored
Write templates to specified path (#2217)
* Write tickets to specified path * Write tickets to specified path - 2 * Implement PR feedback * Update export to stdout * Update cmd/gitops/app/create/templates/cmd.go Co-authored-by: Simon <[email protected]> Co-authored-by: Simon <[email protected]>
1 parent 4c9a9ac commit 6e0cbd8

File tree

1 file changed

+42
-3
lines changed
  • cmd/gitops/app/create/templates

1 file changed

+42
-3
lines changed

cmd/gitops/app/create/templates/cmd.go

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io"
88
"os"
9+
"path/filepath"
910
"strings"
1011

1112
"github.com/go-logr/logr"
@@ -21,6 +22,7 @@ import (
2122
type templateCommandFlags struct {
2223
parameterValues []string
2324
export bool
25+
outputDir string
2426
}
2527

2628
var flags templateCommandFlags
@@ -29,15 +31,19 @@ var CreateCommand = &cobra.Command{
2931
Use: "template",
3032
Short: "Create template resources",
3133
Example: `
32-
export or apply rendered resources of template to cluster or path
33-
gitops create template.yaml --values key1=value1,key2=value2 --export > clusters/management/template.yaml
34+
# export rendered resources of template to stdout
35+
gitops create template.yaml --values key1=value1,key2=value2 --export
36+
37+
# apply rendered resources of template to path
38+
gitops create template.yaml --values key1=value1,key2=value2 --output-dir ./out
3439
`,
3540
RunE: templatesCmdRunE(),
3641
}
3742

3843
func init() {
3944
CreateCommand.Flags().StringSliceVar(&flags.parameterValues, "values", []string{}, "Set parameter values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
4045
CreateCommand.Flags().BoolVar(&flags.export, "export", false, "export in YAML format to stdout")
46+
CreateCommand.Flags().StringVar(&flags.outputDir, "output-dir", "", "write YAML format to file")
4147
}
4248

4349
func templatesCmdRunE() func(*cobra.Command, []string) error {
@@ -81,6 +87,11 @@ func templatesCmdRunE() func(*cobra.Command, []string) error {
8187
}
8288

8389
if flags.export {
90+
renderedTemplate := ""
91+
for _, file := range templateResources.RenderedTemplate {
92+
renderedTemplate += "\n# path: " + *file.Path + "\n---\n" + *file.Content
93+
}
94+
8495
err := export(renderedTemplate, os.Stdout)
8596
if err != nil {
8697
return fmt.Errorf("failed to export rendered template: %w", err)
@@ -89,7 +100,35 @@ func templatesCmdRunE() func(*cobra.Command, []string) error {
89100
return nil
90101
}
91102

92-
return nil
103+
if flags.outputDir != "" {
104+
for _, res := range templateResources.RenderedTemplate {
105+
filePath := filepath.Join(flags.outputDir, *res.Path)
106+
directoryPath := filepath.Dir(filePath)
107+
108+
err := os.MkdirAll(directoryPath, 0755)
109+
110+
if err != nil {
111+
return fmt.Errorf("failed to create directory: %w", err)
112+
}
113+
114+
file, err := os.Create(filePath)
115+
116+
if err != nil {
117+
return fmt.Errorf("failed to create file: %w", err)
118+
}
119+
120+
defer file.Close()
121+
122+
_, err = file.Write([]byte(*res.Content))
123+
124+
if err != nil {
125+
return fmt.Errorf("failed to write to file: %w", err)
126+
}
127+
}
128+
return nil
129+
}
130+
131+
return errors.New("Please provide either --export or --output-dir")
93132
}
94133
}
95134

0 commit comments

Comments
 (0)