Skip to content

Commit

Permalink
handle filepath for output-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 13, 2023
1 parent 578a0ae commit d193409
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions cmd/swctl/app/cmd_manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,17 +377,25 @@ func runManageCmd(cli Cli, opts ManageOptions, args []string) error {
opts.Format = "yaml"
}

var fileName string
var filePath string
var writer io.Writer

if opts.OutputFile == "" {
fileName = fmt.Sprintf("%s.%s", entityName, opts.Format)
filePath = fmt.Sprintf("%s.%s", entityName, opts.Format)
} else {
outputFilePrefix := strings.Split(opts.OutputFile, ".")
fileName = fmt.Sprintf("%s.%s", outputFilePrefix[0], opts.Format)
dir := filepath.Dir(opts.OutputFile)
base := filepath.Base(opts.OutputFile)
err = os.MkdirAll(dir, 0777)
if err != nil {
return err
}

outputFilePrefix := strings.Split(base, ".")
filePath = fmt.Sprintf("%s.%s", outputFilePrefix[0], opts.Format)
filePath = filepath.Join(dir, filePath)
}

file, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE, 0666)
file, err := os.OpenFile(filePath, 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()
Expand Down

0 comments on commit d193409

Please sign in to comment.