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
Signed-off-by: Daniel Béreš <[email protected]>
  • Loading branch information
Giluerre committed Oct 12, 2023
1 parent fbc4611 commit 176333c
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions cmd/swctl/app/cmd_manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type ManageOptions struct {
Vars map[string]string
ShowConfig bool
Interactive bool
File string
}

func (opts *ManageOptions) InstallFlags(flagset *pflag.FlagSet) {
Expand All @@ -76,6 +77,7 @@ 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)")
}

func NewManageCmd(cli Cli) *cobra.Command {
Expand Down Expand Up @@ -370,11 +372,32 @@ func runManageCmd(cli Cli, opts ManageOptions, args []string) error {
finalConf = mainConf
}

// format final output
if opts.Format == "" {
opts.Format = "yaml"
}
if err := formatAsTemplate(cli.Out(), opts.Format, finalConf); err != nil {

var fileName string
var writer io.Writer

if strings.HasSuffix(opts.File, opts.Format) {
fileName = opts.File
} else {
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)
}
}

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 {
writer = file
}

// format final output
if err := formatAsTemplate(writer, opts.Format, finalConf); err != nil {
return err
}

Expand Down

0 comments on commit 176333c

Please sign in to comment.