2023-03-13 12:33:10 +00:00
|
|
|
/**
|
|
|
|
# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
package defaultsubcommand
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2023-12-01 01:10:10 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
|
2023-08-08 11:43:45 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk/config/flags"
|
2023-06-30 10:47:19 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/config"
|
2023-03-22 12:27:43 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
|
2023-03-13 12:33:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type command struct {
|
2023-03-22 12:27:43 +00:00
|
|
|
logger logger.Interface
|
2023-03-13 12:33:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewCommand constructs a default command with the specified logger
|
2023-03-22 12:27:43 +00:00
|
|
|
func NewCommand(logger logger.Interface) *cli.Command {
|
2023-03-13 12:33:10 +00:00
|
|
|
c := command{
|
|
|
|
logger: logger,
|
|
|
|
}
|
|
|
|
return c.build()
|
|
|
|
}
|
|
|
|
|
|
|
|
// build creates the CLI command
|
|
|
|
func (m command) build() *cli.Command {
|
2023-08-08 11:43:45 +00:00
|
|
|
opts := flags.Options{}
|
2023-03-13 12:33:10 +00:00
|
|
|
|
|
|
|
// Create the 'default' command
|
|
|
|
c := cli.Command{
|
2023-08-10 10:07:12 +00:00
|
|
|
Name: "default",
|
|
|
|
Aliases: []string{"create-default", "generate-default"},
|
2023-03-13 12:33:10 +00:00
|
|
|
Usage: "Generate the default NVIDIA Container Toolkit configuration file",
|
|
|
|
Before: func(c *cli.Context) error {
|
|
|
|
return m.validateFlags(c, &opts)
|
|
|
|
},
|
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
return m.run(c, &opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Flags = []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "output",
|
2023-08-10 10:07:12 +00:00
|
|
|
Aliases: []string{"o"},
|
2023-06-30 10:47:19 +00:00
|
|
|
Usage: "Specify the output file to write to; If not specified, the output is written to stdout",
|
2023-08-08 11:43:45 +00:00
|
|
|
Destination: &opts.Output,
|
2023-03-13 12:33:10 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return &c
|
|
|
|
}
|
|
|
|
|
2023-08-08 11:43:45 +00:00
|
|
|
func (m command) validateFlags(c *cli.Context, opts *flags.Options) error {
|
|
|
|
return opts.Validate()
|
2023-03-13 12:33:10 +00:00
|
|
|
}
|
|
|
|
|
2023-08-08 11:43:45 +00:00
|
|
|
func (m command) run(c *cli.Context, opts *flags.Options) error {
|
2023-08-10 10:07:12 +00:00
|
|
|
cfgToml, err := config.New()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to load or create config: %v", err)
|
2023-06-30 10:47:19 +00:00
|
|
|
}
|
|
|
|
|
2023-08-08 11:43:45 +00:00
|
|
|
if err := opts.EnsureOutputFolder(); err != nil {
|
2023-08-10 10:07:12 +00:00
|
|
|
return fmt.Errorf("failed to create output directory: %v", err)
|
|
|
|
}
|
|
|
|
output, err := opts.CreateOutput()
|
2023-06-30 10:47:19 +00:00
|
|
|
if err != nil {
|
2023-08-10 10:07:12 +00:00
|
|
|
return fmt.Errorf("failed to open output file: %v", err)
|
2023-06-30 10:47:19 +00:00
|
|
|
}
|
2023-08-10 10:07:12 +00:00
|
|
|
defer output.Close()
|
2023-06-30 10:47:19 +00:00
|
|
|
|
2023-08-25 14:48:11 +00:00
|
|
|
if _, err = cfgToml.Save(output); err != nil {
|
2023-08-10 10:07:12 +00:00
|
|
|
return fmt.Errorf("failed to write output: %v", err)
|
2023-06-30 10:47:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|