Properly create output for config file

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2023-08-23 09:13:38 +02:00
parent f86a5abeb6
commit 5bf2209fdb
2 changed files with 29 additions and 7 deletions

View File

@ -19,7 +19,6 @@ package config
import (
"errors"
"fmt"
"os"
"strconv"
"strings"
@ -111,7 +110,19 @@ func run(c *cli.Context, opts *options) error {
cfgToml.Set(key, value)
}
cfgToml.Save(os.Stdout)
if err := opts.EnsureOutputFolder(); err != nil {
return fmt.Errorf("failed to create output directory: %v", err)
}
output, err := opts.CreateOutput()
if err != nil {
return fmt.Errorf("failed to open output file: %v", err)
}
defer output.Close()
if err != nil {
return err
}
cfgToml.Save(output)
return nil
}

View File

@ -39,13 +39,23 @@ func (o Options) Validate() error {
return nil
}
// GetOutput returns the effective output
func (o Options) GetOutput() string {
if o.InPlace {
return o.Config
}
return o.Output
}
// EnsureOutputFolder creates the output folder if it does not exist.
// If the output folder is not specified (i.e. output to STDOUT), it is ignored.
func (o Options) EnsureOutputFolder() error {
if o.Output == "" {
output := o.GetOutput()
if output == "" {
return nil
}
if dir := filepath.Dir(o.Output); dir != "" {
if dir := filepath.Dir(output); dir != "" {
return os.MkdirAll(dir, 0755)
}
return nil
@ -53,11 +63,12 @@ func (o Options) EnsureOutputFolder() error {
// CreateOutput creates the writer for the output.
func (o Options) CreateOutput() (io.WriteCloser, error) {
if o.Output != "" {
return os.Create(o.Output)
output := o.GetOutput()
if output == "" {
return nullCloser{os.Stdout}, nil
}
return nullCloser{os.Stdout}, nil
return os.Create(output)
}
// nullCloser is a writer that does nothing on Close.