Skip to content

Commit 059eb76

Browse files
authored
Merge pull request #7828 from Rohitrajak1807/private/Rohitrajak1807/output-flag
✨ Add output flag, change printYaml function signature
2 parents 48f268a + 8961830 commit 059eb76

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

cmd/clusterctl/cmd/generate_cluster.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ type generateClusterOptions struct {
4242
configMapDataKey string
4343

4444
listVariables bool
45+
46+
output string
4547
}
4648

4749
var gc = &generateClusterOptions{}
@@ -137,6 +139,7 @@ func init() {
137139
// other flags
138140
generateClusterClusterCmd.Flags().BoolVar(&gc.listVariables, "list-variables", false,
139141
"Returns the list of variables expected by the template instead of the template yaml")
142+
generateClusterClusterCmd.Flags().StringVar(&gc.output, "write-to", "", "Specify the output file to write the template to, defaults to STDOUT if the flag is not set")
140143

141144
generateCmd.AddCommand(generateClusterClusterCmd)
142145
}
@@ -192,5 +195,5 @@ func runGenerateClusterTemplate(cmd *cobra.Command, name string) error {
192195
return printVariablesOutput(template, templateOptions)
193196
}
194197

195-
return printYamlOutput(template)
198+
return printYamlOutput(template, gc.output)
196199
}

cmd/clusterctl/cmd/generate_provider.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type generateProvidersOptions struct {
3434
targetNamespace string
3535
textOutput bool
3636
raw bool
37+
outputFile string
3738
}
3839

3940
var gpo = &generateProvidersOptions{}
@@ -95,6 +96,8 @@ func init() {
9596
generateProviderCmd.Flags().BoolVar(&gpo.raw, "raw", false,
9697
"Generate configuration without variable substitution in a yaml format.")
9798

99+
generateProviderCmd.Flags().StringVar(&gpo.outputFile, "write-to", "", "Specify the output file to write the template to, defaults to STDOUT if the flag is not set")
100+
98101
generateCmd.AddCommand(generateProviderCmd)
99102
}
100103

@@ -122,7 +125,7 @@ func runGenerateProviderComponents() error {
122125
return printComponentsAsText(components)
123126
}
124127

125-
return printYamlOutput(components)
128+
return printYamlOutput(components, gpo.outputFile)
126129
}
127130

128131
// parseProvider parses command line flags and returns the provider name and type.

cmd/clusterctl/cmd/util.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,23 @@ import (
3232
"sigs.k8s.io/cluster-api/cmd/clusterctl/client"
3333
)
3434

35-
// printYamlOutput prints the yaml content of a generated template to stdout.
36-
func printYamlOutput(printer client.YamlPrinter) error {
35+
// printYamlOutput prints the yaml content of a generated template to stdout or to a local file if specified.
36+
func printYamlOutput(printer client.YamlPrinter, outputFile string) error {
3737
yaml, err := printer.Yaml()
3838
if err != nil {
3939
return err
4040
}
41+
outputFile = filepath.Clean(strings.TrimSpace(outputFile))
4142
yaml = append(yaml, '\n')
43+
if outputFile == "" || outputFile == "-" {
44+
if _, err := os.Stdout.Write(yaml); err != nil {
45+
return errors.Wrap(err, "failed to write yaml to Stdout")
46+
}
47+
return nil
48+
}
4249

43-
if _, err := os.Stdout.Write(yaml); err != nil {
44-
return errors.Wrap(err, "failed to write yaml to Stdout")
50+
if err := os.WriteFile(outputFile, yaml, 0600); err != nil {
51+
return errors.Wrap(err, "failed to write to destination file")
4552
}
4653
return nil
4754
}

0 commit comments

Comments
 (0)