|
| 1 | +/** |
| 2 | +# Copyright (c) 2022, 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 root |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "io" |
| 22 | + "os" |
| 23 | + |
| 24 | + "github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/spec" |
| 25 | + "github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/transform" |
| 26 | + "github.com/container-orchestrated-devices/container-device-interface/pkg/cdi" |
| 27 | + "github.com/sirupsen/logrus" |
| 28 | + "github.com/urfave/cli/v2" |
| 29 | +) |
| 30 | + |
| 31 | +type loadSaver interface { |
| 32 | + Load() (spec.Interface, error) |
| 33 | + Save(spec.Interface) error |
| 34 | +} |
| 35 | + |
| 36 | +type command struct { |
| 37 | + logger *logrus.Logger |
| 38 | +} |
| 39 | + |
| 40 | +type transformOptions struct { |
| 41 | + input string |
| 42 | + output string |
| 43 | +} |
| 44 | + |
| 45 | +type options struct { |
| 46 | + transformOptions |
| 47 | + from string |
| 48 | + to string |
| 49 | +} |
| 50 | + |
| 51 | +// NewCommand constructs a generate-cdi command with the specified logger |
| 52 | +func NewCommand(logger *logrus.Logger) *cli.Command { |
| 53 | + c := command{ |
| 54 | + logger: logger, |
| 55 | + } |
| 56 | + return c.build() |
| 57 | +} |
| 58 | + |
| 59 | +// build creates the CLI command |
| 60 | +func (m command) build() *cli.Command { |
| 61 | + opts := options{} |
| 62 | + |
| 63 | + c := cli.Command{ |
| 64 | + Name: "root", |
| 65 | + Usage: "Apply a root transform to a CDI specification", |
| 66 | + Before: func(c *cli.Context) error { |
| 67 | + return m.validateFlags(c, &opts) |
| 68 | + }, |
| 69 | + Action: func(c *cli.Context) error { |
| 70 | + return m.run(c, &opts) |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + c.Flags = []cli.Flag{ |
| 75 | + &cli.StringFlag{ |
| 76 | + Name: "input", |
| 77 | + Usage: "Specify the file to read the CDI specification from. If this is '-' the specification is read from STDIN", |
| 78 | + Value: "-", |
| 79 | + Destination: &opts.input, |
| 80 | + }, |
| 81 | + &cli.StringFlag{ |
| 82 | + Name: "output", |
| 83 | + Usage: "Specify the file to output the generated CDI specification to. If this is '' the specification is output to STDOUT", |
| 84 | + Destination: &opts.output, |
| 85 | + }, |
| 86 | + &cli.StringFlag{ |
| 87 | + Name: "from", |
| 88 | + Usage: "specify the root to be transformed", |
| 89 | + Destination: &opts.from, |
| 90 | + }, |
| 91 | + &cli.StringFlag{ |
| 92 | + Name: "to", |
| 93 | + Usage: "specify the replacement root. If this is the same as the from root, the transform is a no-op.", |
| 94 | + Value: "", |
| 95 | + Destination: &opts.to, |
| 96 | + }, |
| 97 | + } |
| 98 | + |
| 99 | + return &c |
| 100 | +} |
| 101 | + |
| 102 | +func (m command) validateFlags(c *cli.Context, opts *options) error { |
| 103 | + return nil |
| 104 | +} |
| 105 | + |
| 106 | +func (m command) run(c *cli.Context, opts *options) error { |
| 107 | + spec, err := opts.Load() |
| 108 | + if err != nil { |
| 109 | + return fmt.Errorf("failed to load CDI specification: %w", err) |
| 110 | + } |
| 111 | + |
| 112 | + err = transform.NewRootTransformer( |
| 113 | + opts.from, |
| 114 | + opts.to, |
| 115 | + ).Transform(spec.Raw()) |
| 116 | + if err != nil { |
| 117 | + return fmt.Errorf("failed to transform CDI specification: %w", err) |
| 118 | + } |
| 119 | + |
| 120 | + return opts.Save(spec) |
| 121 | +} |
| 122 | + |
| 123 | +// Load lodas the input CDI specification |
| 124 | +func (o transformOptions) Load() (spec.Interface, error) { |
| 125 | + contents, err := o.getContents() |
| 126 | + if err != nil { |
| 127 | + return nil, fmt.Errorf("failed to read spec contents: %v", err) |
| 128 | + } |
| 129 | + |
| 130 | + raw, err := cdi.ParseSpec(contents) |
| 131 | + if err != nil { |
| 132 | + return nil, fmt.Errorf("failed to parse CDI spec: %v", err) |
| 133 | + } |
| 134 | + |
| 135 | + return spec.New( |
| 136 | + spec.WithRawSpec(raw), |
| 137 | + ) |
| 138 | +} |
| 139 | + |
| 140 | +func (o transformOptions) getContents() ([]byte, error) { |
| 141 | + if o.input == "-" { |
| 142 | + return io.ReadAll(os.Stdin) |
| 143 | + } |
| 144 | + |
| 145 | + return os.ReadFile(o.input) |
| 146 | +} |
| 147 | + |
| 148 | +// Save saves the CDI specification to the output file |
| 149 | +func (o transformOptions) Save(s spec.Interface) error { |
| 150 | + if o.output == "" { |
| 151 | + _, err := s.WriteTo(os.Stdout) |
| 152 | + if err != nil { |
| 153 | + return fmt.Errorf("failed to write CDI spec to STDOUT: %v", err) |
| 154 | + } |
| 155 | + return nil |
| 156 | + } |
| 157 | + |
| 158 | + return s.Save(o.output) |
| 159 | +} |
0 commit comments