|
| 1 | +/** |
| 2 | +# Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +**/ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "os" |
| 21 | + |
| 22 | + "github.com/sirupsen/logrus" |
| 23 | + |
| 24 | + "github.com/NVIDIA/nvidia-container-toolkit/internal/info" |
| 25 | + |
| 26 | + cli "github.com/urfave/cli/v2" |
| 27 | + |
| 28 | + "github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-cdi-hook/commands" |
| 29 | +) |
| 30 | + |
| 31 | +// options defines the options that can be set for the CLI through config files, |
| 32 | +// environment variables, or command line flags |
| 33 | +type options struct { |
| 34 | + // Debug indicates whether the CLI is started in "debug" mode |
| 35 | + Debug bool |
| 36 | + // Quiet indicates whether the CLI is started in "quiet" mode |
| 37 | + Quiet bool |
| 38 | +} |
| 39 | + |
| 40 | +func main() { |
| 41 | + logger := logrus.New() |
| 42 | + |
| 43 | + // Create a options struct to hold the parsed environment variables or command line flags |
| 44 | + opts := options{} |
| 45 | + |
| 46 | + // Create the top-level CLI |
| 47 | + c := cli.NewApp() |
| 48 | + c.Name = "NVIDIA CDI Hook" |
| 49 | + c.UseShortOptionHandling = true |
| 50 | + c.EnableBashCompletion = true |
| 51 | + c.Usage = "Command to structure files for usage inside a container, called as hooks from a container runtime, defined in a CDI yaml file" |
| 52 | + c.Version = info.GetVersionString() |
| 53 | + |
| 54 | + // Setup the flags for this command |
| 55 | + c.Flags = []cli.Flag{ |
| 56 | + &cli.BoolFlag{ |
| 57 | + Name: "debug", |
| 58 | + Aliases: []string{"d"}, |
| 59 | + Usage: "Enable debug-level logging", |
| 60 | + Destination: &opts.Debug, |
| 61 | + EnvVars: []string{"NVIDIA_CDI_DEBUG"}, |
| 62 | + }, |
| 63 | + &cli.BoolFlag{ |
| 64 | + Name: "quiet", |
| 65 | + Usage: "Suppress all output except for errors; overrides --debug", |
| 66 | + Destination: &opts.Quiet, |
| 67 | + EnvVars: []string{"NVIDIA_CDI_QUIET"}, |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + // Set log-level for all subcommands |
| 72 | + c.Before = func(c *cli.Context) error { |
| 73 | + logLevel := logrus.InfoLevel |
| 74 | + if opts.Debug { |
| 75 | + logLevel = logrus.DebugLevel |
| 76 | + } |
| 77 | + if opts.Quiet { |
| 78 | + logLevel = logrus.ErrorLevel |
| 79 | + } |
| 80 | + logger.SetLevel(logLevel) |
| 81 | + return nil |
| 82 | + } |
| 83 | + |
| 84 | + // Define the subcommands |
| 85 | + c.Commands = commands.New(logger) |
| 86 | + |
| 87 | + // Run the CLI |
| 88 | + err := c.Run(os.Args) |
| 89 | + if err != nil { |
| 90 | + logger.Errorf("%v", err) |
| 91 | + os.Exit(1) |
| 92 | + } |
| 93 | +} |
0 commit comments