mirror of
				https://github.com/NVIDIA/nvidia-container-toolkit
				synced 2025-06-26 18:18:24 +00:00 
			
		
		
		
	Properly create output for config file
Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
		
							parent
							
								
									f86a5abeb6
								
							
						
					
					
						commit
						5bf2209fdb
					
				| @ -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 | ||||
| } | ||||
| 
 | ||||
|  | ||||
| @ -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.
 | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user