Debian Codegraph @

providers/basicflag/basicflag.go

raw
1 // Package basicflag implements a koanf.Provider that reads commandline
2 // parameters as conf maps using the Go's flag package.
3 package basicflag
4
5 import (
6 "errors"
7 "flag"
8
9 "github.com/knadh/koanf/maps"
10 )
11
12 // Opt represents optional options (yup) passed to the provider.
13 type Opt struct {
14 KeyMap KoanfIntf
15 }
16
17 // KoanfIntf is an interface that represents a small subset of methods
18 // used by this package from Koanf{}.
19 type KoanfIntf interface {
20 Exists(string) bool
21 }
22
23 // Pflag implements a pflag command line provider.
24 type Pflag struct {
25 delim string
26 flagset *flag.FlagSet
27 cb func(key string, value string) (string, any)
28 opt *Opt
29 }
30
31 // Provider returns a commandline flags provider that returns
32 // a nested map[string]any of environment variable where the
33 // nesting hierarchy of keys are defined by delim. For instance, the
34 // delim "." will convert the key `parent.child.key: 1`
35 // to `{parent: {child: {key: 1}}}`.
36 //
37 // It takes an optional (but recommended) Opt{} argument containing a Koanf instance.
38 // It checks if the defined flags have been set by other providers (e.g., a config file).
39 // If not, default flag values are merged. If they exist, flag values are merged only if
40 // explicitly set in the command line. The function is variadic to maintain backward compatibility.
41 // See https://github.com/knadh/koanf/issues/255
42 func Provider(f *flag.FlagSet, delim string, opt ...*Opt) *Pflag {
43 pf := &Pflag{
44 flagset: f,
45 delim: delim,
46 }
47
48 if len(opt) > 0 {
49 pf.opt = opt[0]
50 }
51
52 return pf
53 }
54
55 // ProviderWithValue works exactly the same as Provider except the callback
56 // takes a (key, value) with the variable name and value and allows you
57 // to modify both. This is useful for cases where you may want to return
58 // other types like a string slice instead of just a string.
59 //
60 // It takes an optional Opt{} (but recommended) argument with a Koanf instance (opt.KeyMap) to see if
61 // the flags defined have been set from other providers, for instance,
62 // a config file. If they are not, then the default values of the flags
63 // are merged. If they do exist, the flag values are not merged but only
64 // the values that have been explicitly set in the command line are merged.
65 // It is a variadic function as a hack to ensure backwards compatibility with the
66 // function definition.
67 // See https://github.com/knadh/koanf/issues/255
68 func ProviderWithValue(f *flag.FlagSet, delim string, cb func(key string, value string) (string, any), ko ...KoanfIntf) *Pflag {
69 pf := &Pflag{
70 flagset: f,
71 delim: delim,
72 cb: cb,
73 }
74
75 if len(ko) > 0 {
76 pf.opt = &Opt{
77 KeyMap: ko[0],
78 }
79 }
80 return pf
81 }
82
83 // Read reads the flag variables and returns a nested conf map.
84 func (p *Pflag) Read() (map[string]any, error) {
85 var changed map[string]struct{}
86
87 // Prepare a map of flags that have been explicitly set by the user as aa KeyMap instance of Koanf
88 // has been provided.
89 if p.opt != nil && p.opt.KeyMap != nil {
90 changed = map[string]struct{}{}
91
92 p.flagset.Visit(func(f *flag.Flag) {
93 key := f.Name
94 if p.cb != nil {
95 key, _ = p.cb(f.Name, "")
96 }
97 if key == "" {
98 return
99 }
100
101 changed[key] = struct{}{}
102 })
103 }
104
105 mp := make(map[string]any)
106 p.flagset.VisitAll(func(f *flag.Flag) {
107 var (
108 key = f.Name
109 val any = f.Value.String()
110 )
111 if p.cb != nil {
112 k, v := p.cb(f.Name, f.Value.String())
113 // If the callback blanked the key, it should be omitted
114 if k == "" {
115 return
116 }
117
118 key = k
119 val = v
120 }
121
122 // If the default value of the flag was never changed by the user,
123 // it should not override the value in the conf map (if it exists in the first place).
124 if changed != nil {
125 if _, ok := changed[key]; !ok {
126 if p.opt.KeyMap.Exists(key) {
127 return
128 }
129 }
130 }
131
132 mp[key] = val
133 })
134 return maps.Unflatten(mp, p.delim), nil
135 }
136
137 // ReadBytes is not supported by the basicflag koanf.
138 func (p *Pflag) ReadBytes() ([]byte, error) {
139 return nil, errors.New("basicflag provider does not support this method")
140 }