mirror of
https://github.com/NVIDIA/nvidia-container-toolkit
synced 2025-02-16 17:42:20 +00:00
Simplify nvidia-ctk config default command
This chagne simplifies the nvidia-ctk config default command. By default it now outputs the default config to STDOUT, and can optionally output this to file. Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
parent
a69657dde7
commit
149a8d7bd8
@ -33,9 +33,7 @@ type command struct {
|
|||||||
|
|
||||||
// options stores the subcommand options
|
// options stores the subcommand options
|
||||||
type options struct {
|
type options struct {
|
||||||
config string
|
output string
|
||||||
output string
|
|
||||||
inPlace bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewCommand constructs a default command with the specified logger
|
// NewCommand constructs a default command with the specified logger
|
||||||
@ -52,8 +50,8 @@ func (m command) build() *cli.Command {
|
|||||||
|
|
||||||
// Create the 'default' command
|
// Create the 'default' command
|
||||||
c := cli.Command{
|
c := cli.Command{
|
||||||
Name: "generate-default",
|
Name: "default",
|
||||||
Aliases: []string{"default"},
|
Aliases: []string{"create-default", "generate-default"},
|
||||||
Usage: "Generate the default NVIDIA Container Toolkit configuration file",
|
Usage: "Generate the default NVIDIA Container Toolkit configuration file",
|
||||||
Before: func(c *cli.Context) error {
|
Before: func(c *cli.Context) error {
|
||||||
return m.validateFlags(c, &opts)
|
return m.validateFlags(c, &opts)
|
||||||
@ -64,19 +62,9 @@ func (m command) build() *cli.Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.Flags = []cli.Flag{
|
c.Flags = []cli.Flag{
|
||||||
&cli.StringFlag{
|
|
||||||
Name: "config",
|
|
||||||
Usage: "Specify the config file to process; The contents of this file overrides the default config",
|
|
||||||
Destination: &opts.config,
|
|
||||||
},
|
|
||||||
&cli.BoolFlag{
|
|
||||||
Name: "in-place",
|
|
||||||
Aliases: []string{"i"},
|
|
||||||
Usage: "Modify the config file in-place",
|
|
||||||
Destination: &opts.inPlace,
|
|
||||||
},
|
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "output",
|
Name: "output",
|
||||||
|
Aliases: []string{"o"},
|
||||||
Usage: "Specify the output file to write to; If not specified, the output is written to stdout",
|
Usage: "Specify the output file to write to; If not specified, the output is written to stdout",
|
||||||
Destination: &opts.output,
|
Destination: &opts.output,
|
||||||
},
|
},
|
||||||
@ -86,63 +74,56 @@ func (m command) build() *cli.Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m command) validateFlags(c *cli.Context, opts *options) error {
|
func (m command) validateFlags(c *cli.Context, opts *options) error {
|
||||||
if opts.inPlace {
|
|
||||||
if opts.output != "" {
|
|
||||||
return fmt.Errorf("cannot specify both --in-place and --output")
|
|
||||||
}
|
|
||||||
opts.output = opts.config
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m command) run(c *cli.Context, opts *options) error {
|
func (m command) run(c *cli.Context, opts *options) error {
|
||||||
if err := opts.ensureOutputFolder(); err != nil {
|
cfgToml, err := config.New()
|
||||||
return fmt.Errorf("unable to create output directory: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
cfgToml, err := opts.getConfig()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to load config: %v", err)
|
return fmt.Errorf("unable to load or create config: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := opts.Write(cfgToml); err != nil {
|
if err := opts.ensureOutputFolder(); err != nil {
|
||||||
return fmt.Errorf("unable to write to output: %v", err)
|
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()
|
||||||
|
|
||||||
|
_, err = cfgToml.Save(output)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to write output: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// getConfig returns the TOML config for the specified options.
|
// ensureOutputFolder creates the output folder if it does not exist.
|
||||||
func (opts options) getConfig() (*config.Toml, error) {
|
// If the output folder is not specified (i.e. output to STDOUT), it is ignored.
|
||||||
return config.New(
|
func (o options) ensureOutputFolder() error {
|
||||||
config.WithConfigFile(opts.config),
|
if o.output == "" {
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (opts options) ensureOutputFolder() error {
|
|
||||||
if opts.output == "" {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if dir := filepath.Dir(opts.output); dir != "" {
|
if dir := filepath.Dir(o.output); dir != "" {
|
||||||
return os.MkdirAll(dir, 0755)
|
return os.MkdirAll(dir, 0755)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write writes the contents to the output file specified in the options.
|
func (o options) CreateOutput() (io.WriteCloser, error) {
|
||||||
func (opts options) Write(cfg *config.Toml) (int, error) {
|
if o.output != "" {
|
||||||
var output io.Writer
|
return os.Create(o.output)
|
||||||
if opts.output == "" {
|
|
||||||
output = os.Stdout
|
|
||||||
} else {
|
|
||||||
outputFile, err := os.Create(opts.output)
|
|
||||||
if err != nil {
|
|
||||||
return 0, fmt.Errorf("unable to create output file: %v", err)
|
|
||||||
}
|
|
||||||
defer outputFile.Close()
|
|
||||||
output = outputFile
|
|
||||||
}
|
}
|
||||||
|
|
||||||
n, err := cfg.Save(output)
|
return nullCloser{os.Stdout}, nil
|
||||||
return int(n), err
|
}
|
||||||
|
|
||||||
|
type nullCloser struct {
|
||||||
|
io.Writer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d nullCloser) Close() error {
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user