|
| 1 | +// Package cliflagv3 implements a koanf.Provider that reads commandline |
| 2 | +// parameters as conf maps using urfave/cli/v3 flag. |
| 3 | +package cliflagv3 |
| 4 | + |
| 5 | +import ( |
| 6 | + "errors" |
| 7 | + "slices" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/knadh/koanf/maps" |
| 11 | + "github.com/urfave/cli/v3" |
| 12 | +) |
| 13 | + |
| 14 | +// CliFlag implements a cli.Flag command line provider. |
| 15 | +type CliFlag struct { |
| 16 | + cmd *cli.Command |
| 17 | + delim string |
| 18 | + config *Config |
| 19 | +} |
| 20 | + |
| 21 | +type Config struct { |
| 22 | + Defaults []string |
| 23 | +} |
| 24 | + |
| 25 | +// Provider returns a commandline flags provider that returns |
| 26 | +// a nested map[string]interface{} of environment variable where the |
| 27 | +// nesting hierarchy of keys are defined by delim. For instance, the |
| 28 | +// delim "." will convert the key `parent.child.key: 1` |
| 29 | +// to `{parent: {child: {key: 1}}}`. |
| 30 | +func Provider(f *cli.Command, delim string) *CliFlag { |
| 31 | + return &CliFlag{ |
| 32 | + cmd: f, |
| 33 | + delim: delim, |
| 34 | + config: &Config{ |
| 35 | + Defaults: []string{}, |
| 36 | + }, |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// ProviderWithConfig returns a commandline flags provider with a |
| 41 | +// Configuration struct attached. |
| 42 | +func ProviderWithConfig(f *cli.Command, delim string, config *Config) *CliFlag { |
| 43 | + return &CliFlag{ |
| 44 | + cmd: f, |
| 45 | + delim: delim, |
| 46 | + config: config, |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// ReadBytes is not supported by the cliflagv3 provider. |
| 51 | +func (p *CliFlag) ReadBytes() ([]byte, error) { |
| 52 | + return nil, errors.New("cliflagv3 provider does not support this method") |
| 53 | +} |
| 54 | + |
| 55 | +// Read reads the flag variables and returns a nested conf map. |
| 56 | +func (p *CliFlag) Read() (map[string]interface{}, error) { |
| 57 | + out := make(map[string]interface{}) |
| 58 | + |
| 59 | + // Get command lineage (from root to current command) |
| 60 | + lineage := p.cmd.Lineage() |
| 61 | + if len(lineage) > 0 { |
| 62 | + // Build command path and process flags for each level |
| 63 | + var cmdPath []string |
| 64 | + for i := len(lineage) - 1; i >= 0; i-- { |
| 65 | + cmd := lineage[i] |
| 66 | + cmdPath = append(cmdPath, cmd.Name) |
| 67 | + prefix := strings.Join(cmdPath, p.delim) |
| 68 | + p.processFlags(cmd.Flags, prefix, out) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if p.delim == "" { |
| 73 | + return out, nil |
| 74 | + } |
| 75 | + |
| 76 | + return maps.Unflatten(out, p.delim), nil |
| 77 | +} |
| 78 | + |
| 79 | +func (p *CliFlag) processFlags(flags []cli.Flag, prefix string, out map[string]interface{}) { |
| 80 | + for _, flag := range flags { |
| 81 | + name := flag.Names()[0] |
| 82 | + if p.cmd.IsSet(name) || slices.Contains(p.config.Defaults, name) { |
| 83 | + value := p.getFlagValue(name) |
| 84 | + if value != nil { |
| 85 | + // Build the full path for the flag |
| 86 | + fullPath := name |
| 87 | + if prefix != "global" { |
| 88 | + fullPath = prefix + p.delim + name |
| 89 | + } |
| 90 | + |
| 91 | + p.setNestedValue(fullPath, value, out) |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// setNestedValue sets a value in the nested configuration structure |
| 98 | +func (p *CliFlag) setNestedValue(path string, value interface{}, out map[string]interface{}) { |
| 99 | + parts := strings.Split(path, p.delim) |
| 100 | + current := out |
| 101 | + |
| 102 | + // Navigate/create the nested structure |
| 103 | + for i := 0; i < len(parts)-1; i++ { |
| 104 | + if _, exists := current[parts[i]]; !exists { |
| 105 | + current[parts[i]] = make(map[string]interface{}) |
| 106 | + } |
| 107 | + current = current[parts[i]].(map[string]interface{}) |
| 108 | + } |
| 109 | + |
| 110 | + // Set the final value |
| 111 | + current[parts[len(parts)-1]] = value |
| 112 | +} |
| 113 | + |
| 114 | +// getFlagValue extracts the typed value from the flag. |
| 115 | +func (p *CliFlag) getFlagValue(name string) interface{} { |
| 116 | + // Find the flag definition |
| 117 | + flag := p.findFlag(name) |
| 118 | + if flag == nil { |
| 119 | + return nil |
| 120 | + } |
| 121 | + return flag.Get() |
| 122 | +} |
| 123 | + |
| 124 | +// findFlag looks up a flag by name |
| 125 | +func (p *CliFlag) findFlag(name string) cli.Flag { |
| 126 | + // Check global flags |
| 127 | + for _, f := range p.cmd.Flags { |
| 128 | + if slices.Contains(f.Names(), name) { |
| 129 | + return f |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + return nil |
| 134 | +} |
0 commit comments