From 9aaead42deb1807cc9ecaf169abe39a5df6ddb87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20B=C3=A9re=C5=A1?= Date: Fri, 13 Oct 2023 01:48:25 -0700 Subject: [PATCH] swctl: redirect output from command 'manage Entity create' to file - rename file flag to output-file - added file closing - handling filename with wrong suffix MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Daniel Béreš --- cmd/swctl/app/cmd_manage.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cmd/swctl/app/cmd_manage.go b/cmd/swctl/app/cmd_manage.go index 4c1b984..b14e503 100644 --- a/cmd/swctl/app/cmd_manage.go +++ b/cmd/swctl/app/cmd_manage.go @@ -64,7 +64,7 @@ type ManageOptions struct { Vars map[string]string ShowConfig bool Interactive bool - File string + OutputFile string } func (opts *ManageOptions) InstallFlags(flagset *pflag.FlagSet) { @@ -77,7 +77,8 @@ func (opts *ManageOptions) InstallFlags(flagset *pflag.FlagSet) { flagset.BoolVar(&opts.ShowConfig, "show-config", false, "Print config for entity detail") flagset.BoolVarP(&opts.Interactive, "interactive", "i", false, "Enable interactive mode") flagset.StringToStringVar(&opts.Vars, "var", nil, "Override values for variables (--var VAR_NAME=value)") - flagset.StringVar(&opts.File, "file", "", "Define the output file name (default: .yaml)") + flagset.StringVar(&opts.OutputFile, "output-file", "", `Define the output file name. Format from format flag will be used as suffix + (default: .)`) } func NewManageCmd(cli Cli) *cobra.Command { @@ -379,13 +380,11 @@ func runManageCmd(cli Cli, opts ManageOptions, args []string) error { var fileName string var writer io.Writer - if strings.HasSuffix(opts.File, opts.Format) { - fileName = opts.File - } else { + if opts.OutputFile == "" { fileName = fmt.Sprintf("%s.%s", entityName, opts.Format) - if opts.File != "" { - logrus.Warnf("Defined output file name is not in required format. Naming %s will be used.", fileName) - } + } else { + outputFilePrefix := strings.Split(opts.OutputFile, ".") + fileName = fmt.Sprintf("%s.%s", outputFilePrefix[0], opts.Format) } file, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE, 0666) @@ -393,6 +392,7 @@ func runManageCmd(cli Cli, opts ManageOptions, args []string) error { logrus.Warnln(fmt.Errorf("%s : the configuration file will be printed to stdout", err)) writer = cli.Out() } else { + defer file.Close() writer = file }