|
| 1 | +/** |
| 2 | +# Copyright (c) 2025, 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 generate |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "sync" |
| 22 | + |
| 23 | + "github.com/NVIDIA/nvidia-container-toolkit/internal/config" |
| 24 | + |
| 25 | + altsrc "github.com/urfave/cli-altsrc/v3" |
| 26 | + "github.com/urfave/cli/v3" |
| 27 | +) |
| 28 | + |
| 29 | +// New sets the source for the config file path. |
| 30 | +// It uses a string pointer to the config file path option. |
| 31 | +func New(config *string) *configAsValueSource { |
| 32 | + return &configAsValueSource{ |
| 33 | + source: altsrc.NewStringPtrSourcer(config), |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// configAsValueSource provides flag values from a TOML configuration file, acting as a factory for specific cli.ValueSource instances. |
| 38 | +// It uses an altsrc.Sourcer to find the configuration file path and loads it on demand. |
| 39 | +type configAsValueSource struct { |
| 40 | + sync.Mutex |
| 41 | + source altsrc.Sourcer |
| 42 | + *config.Toml |
| 43 | +} |
| 44 | + |
| 45 | +// ValueFrom returns a cli.ValueSource for the given key. |
| 46 | +// This can be used in the Sources chain for a cli.Flag. |
| 47 | +func (c *configAsValueSource) ValueFrom(key string) cli.ValueSource { |
| 48 | + return &configValueLookup{ |
| 49 | + key: key, |
| 50 | + from: c, |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func (c *configAsValueSource) Get(key string) interface{} { |
| 55 | + c.Lock() |
| 56 | + defer c.Unlock() |
| 57 | + |
| 58 | + if c.Toml == nil { |
| 59 | + if err := c.loadFromConfig(); err != nil { |
| 60 | + return nil |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + if c.Toml == nil { |
| 65 | + return nil |
| 66 | + } |
| 67 | + |
| 68 | + return c.Toml.Get(key) |
| 69 | +} |
| 70 | + |
| 71 | +// loadFromConfig loads the config file if it has not already been loaded. |
| 72 | +// If the config file path is not specified, it uses the default config file path. |
| 73 | +func (c *configAsValueSource) loadFromConfig() error { |
| 74 | + if c.Toml != nil { |
| 75 | + return nil |
| 76 | + } |
| 77 | + |
| 78 | + configFilePath := c.source.SourceURI() |
| 79 | + if configFilePath == "" { |
| 80 | + configFilePath = config.GetConfigFilePath() |
| 81 | + } |
| 82 | + configToml, err := config.New( |
| 83 | + config.WithConfigFile(configFilePath), |
| 84 | + ) |
| 85 | + if err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + c.Toml = configToml |
| 89 | + |
| 90 | + return nil |
| 91 | +} |
| 92 | + |
| 93 | +// configValueLookup is a cli.ValueSource that looks up values from a configAsValueSource. |
| 94 | +type configValueLookup struct { |
| 95 | + key string |
| 96 | + from *configAsValueSource |
| 97 | +} |
| 98 | + |
| 99 | +// Lookup returns the value for the key from the config file. |
| 100 | +func (c *configValueLookup) Lookup() (string, bool) { |
| 101 | + if c == nil || c.from == nil { |
| 102 | + return "", false |
| 103 | + } |
| 104 | + if value := c.from.Get(c.key); value != nil { |
| 105 | + return fmt.Sprintf("%v", value), true |
| 106 | + } |
| 107 | + |
| 108 | + return "", false |
| 109 | +} |
| 110 | + |
| 111 | +// GoString returns a Go-syntax representation of the configuration source. |
| 112 | +func (c *configValueLookup) GoString() string { |
| 113 | + return c.String() |
| 114 | +} |
| 115 | + |
| 116 | +// String returns a string representation of the configuration source. |
| 117 | +func (c *configValueLookup) String() string { |
| 118 | + if c.from == nil || c.from.source == nil { |
| 119 | + return "config file" |
| 120 | + } |
| 121 | + configFile := c.from.source.SourceURI() |
| 122 | + if configFile == "" { |
| 123 | + configFile = config.GetConfigFilePath() |
| 124 | + } |
| 125 | + if configFile == "" { |
| 126 | + return "config file" |
| 127 | + } |
| 128 | + return fmt.Sprintf("config file %q", configFile) |
| 129 | +} |
0 commit comments