Skip to content

Commit

Permalink
swctl: redirect output from command 'manage Entity create' to file
Browse files Browse the repository at this point in the history
- rename file flag to output-file
- added file closing
- handling filename with wrong suffix

Signed-off-by: Daniel Béreš <[email protected]>
  • Loading branch information
Giluerre committed Oct 13, 2023
1 parent 176333c commit 9aaead4
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions cmd/swctl/app/cmd_manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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: <entityName>.yaml)")
flagset.StringVar(&opts.OutputFile, "output-file", "", `Define the output file name. Format from format flag will be used as suffix
(default: <entityName>.<actual format suffix>)`)
}

func NewManageCmd(cli Cli) *cobra.Command {
Expand Down Expand Up @@ -379,20 +380,19 @@ 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)
if err != nil {
logrus.Warnln(fmt.Errorf("%s : the configuration file will be printed to stdout", err))
writer = cli.Out()
} else {
defer file.Close()
writer = file
}

Expand Down

0 comments on commit 9aaead4

Please sign in to comment.