diff --git a/cmd/nvidia-ctk/config/config.go b/cmd/nvidia-ctk/config/config.go index 9fc1a107..ee5832b6 100644 --- a/cmd/nvidia-ctk/config/config.go +++ b/cmd/nvidia-ctk/config/config.go @@ -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 } diff --git a/cmd/nvidia-ctk/config/flags/options.go b/cmd/nvidia-ctk/config/flags/options.go index 95cdc4c2..ad0278e9 100644 --- a/cmd/nvidia-ctk/config/flags/options.go +++ b/cmd/nvidia-ctk/config/flags/options.go @@ -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.