| 1 |
// Package cliflagv2 implements a koanf.Provider that reads commandline |
| 2 |
// parameters as conf maps using urfave/cli/v2 flag. |
| 3 |
package cliflagv2 |
| 4 |
|
| 5 |
import ( |
| 6 |
"errors" |
| 7 |
"slices" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"github.com/knadh/koanf/maps" |
| 11 |
"github.com/urfave/cli/v2" |
| 12 |
) |
| 13 |
|
| 14 |
// CliFlag implements a cli.Flag command line provider. |
| 15 |
type CliFlag struct { |
| 16 |
ctx *cli.Context |
| 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]any 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.Context, delim string) *CliFlag { |
| 31 |
return &CliFlag{ |
| 32 |
ctx: 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.Context, delim string, config *Config) *CliFlag { |
| 43 |
return &CliFlag{ |
| 44 |
ctx: f, |
| 45 |
delim: delim, |
| 46 |
config: config, |
| 47 |
} |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
// ReadBytes is not supported by the cliflagv2 provider. |
| 52 |
func (p *CliFlag) ReadBytes() ([]byte, error) { |
| 53 |
return nil, errors.New("cliflagv2 provider does not support this method") |
| 54 |
} |
| 55 |
|
| 56 |
// Read reads the flag variables and returns a nested conf map. |
| 57 |
func (p *CliFlag) Read() (map[string]any, error) { |
| 58 |
out := make(map[string]any) |
| 59 |
|
| 60 |
// Get command lineage (from root to current command) |
| 61 |
lineage := p.ctx.Lineage() |
| 62 |
if len(lineage) > 0 { |
| 63 |
// Build command path and process flags for each level |
| 64 |
var cmdPath []string |
| 65 |
for i := len(lineage) - 1; i >= 0; i-- { |
| 66 |
cmd := lineage[i] |
| 67 |
if cmd.Command == nil { |
| 68 |
continue |
| 69 |
} |
| 70 |
cmdPath = append(cmdPath, cmd.Command.Name) |
| 71 |
prefix := strings.Join(cmdPath, p.delim) |
| 72 |
p.processFlags(cmd.Command.Flags, prefix, out) |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
if p.delim == "" { |
| 77 |
return out, nil |
| 78 |
} |
| 79 |
|
| 80 |
return maps.Unflatten(out, p.delim), nil |
| 81 |
} |
| 82 |
|
| 83 |
func (p *CliFlag) processFlags(flags []cli.Flag, prefix string, out map[string]any) { |
| 84 |
for _, flag := range flags { |
| 85 |
name := flag.Names()[0] |
| 86 |
if p.ctx.IsSet(name) || slices.Contains(p.config.Defaults, name) { |
| 87 |
value := p.getFlagValue(name) |
| 88 |
if value != nil { |
| 89 |
// Build the full path for the flag |
| 90 |
fullPath := name |
| 91 |
if prefix != "global" { |
| 92 |
fullPath = prefix + p.delim + name |
| 93 |
} |
| 94 |
|
| 95 |
p.setNestedValue(fullPath, value, out) |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
// setNestedValue sets a value in the nested configuration structure |
| 102 |
func (p *CliFlag) setNestedValue(path string, value any, out map[string]any) { |
| 103 |
parts := strings.Split(path, p.delim) |
| 104 |
current := out |
| 105 |
|
| 106 |
// Navigate/create the nested structure |
| 107 |
for i := 0; i < len(parts)-1; i++ { |
| 108 |
if _, exists := current[parts[i]]; !exists { |
| 109 |
current[parts[i]] = make(map[string]any) |
| 110 |
} |
| 111 |
current = current[parts[i]].(map[string]any) |
| 112 |
} |
| 113 |
|
| 114 |
// Set the final value |
| 115 |
current[parts[len(parts)-1]] = value |
| 116 |
} |
| 117 |
|
| 118 |
// getFlagValue extracts the typed value from the flag. |
| 119 |
func (p *CliFlag) getFlagValue(name string) any { |
| 120 |
// Find the flag definition |
| 121 |
flag := p.findFlag(name) |
| 122 |
if flag == nil { |
| 123 |
return nil |
| 124 |
} |
| 125 |
|
| 126 |
// Use type switch to get the appropriate value |
| 127 |
switch flag.(type) { |
| 128 |
case *cli.StringFlag: |
| 129 |
return p.ctx.String(name) |
| 130 |
case *cli.StringSliceFlag: |
| 131 |
return p.ctx.StringSlice(name) |
| 132 |
case *cli.IntFlag: |
| 133 |
return p.ctx.Int(name) |
| 134 |
case *cli.Int64Flag: |
| 135 |
return p.ctx.Int64(name) |
| 136 |
case *cli.IntSliceFlag: |
| 137 |
return p.ctx.IntSlice(name) |
| 138 |
case *cli.Float64Flag: |
| 139 |
return p.ctx.Float64(name) |
| 140 |
case *cli.Float64SliceFlag: |
| 141 |
return p.ctx.Float64Slice(name) |
| 142 |
case *cli.BoolFlag: |
| 143 |
return p.ctx.Bool(name) |
| 144 |
case *cli.DurationFlag: |
| 145 |
return p.ctx.Duration(name) |
| 146 |
case *cli.TimestampFlag: |
| 147 |
return p.ctx.Timestamp(name) |
| 148 |
case *cli.PathFlag: |
| 149 |
return p.ctx.Path(name) |
| 150 |
default: |
| 151 |
return p.ctx.Generic(name) |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
// findFlag looks up a flag by name in both global and command-specific flags |
| 156 |
func (p *CliFlag) findFlag(name string) cli.Flag { |
| 157 |
// Check global flags |
| 158 |
for _, f := range p.ctx.App.Flags { |
| 159 |
for _, n := range f.Names() { |
| 160 |
if n == name { |
| 161 |
return f |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
// Check command-specific flags if we're in a command |
| 167 |
if p.ctx.Command != nil { |
| 168 |
for _, f := range p.ctx.Command.Flags { |
| 169 |
for _, n := range f.Names() { |
| 170 |
if n == name { |
| 171 |
return f |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
return nil |
| 178 |
} |