Debian Codegraph @

value_source.go

raw symbols SCIP
1 package cli
2
3 import (
4 "fmt"
5 "os"
6 "strings"
7 )
8
9 // ValueSource is a source which can be used to look up a value,
10 // typically for use with a cli.Flag
11 type ValueSource interface {
12 fmt.Stringer
13 fmt.GoStringer
14
15 // Lookup returns the value from the source and if it was found
16 // or returns an empty string and false
17 Lookup() (string, bool)
18 }
19
20 // EnvValueSource is to specifically detect env sources when
21 // printing help text
22 type EnvValueSource interface {
23 IsFromEnv() bool
24 Key() string
25 }
26
27 // MapSource is a source which can be used to look up a value
28 // based on a key
29 // typically for use with a cli.Flag
30 type MapSource interface {
31 fmt.Stringer
32 fmt.GoStringer
33
34 // Lookup returns the value from the source based on key
35 // and if it was found
36 // or returns an empty string and false
37 Lookup(string) (any, bool)
38 }
39
40 // ValueSourceChain contains an ordered series of ValueSource that
41 // allows for lookup where the first ValueSource to resolve is
42 // returned
43 type ValueSourceChain struct {
44 Chain []ValueSource
45 }
46
47 func NewValueSourceChain(src ...ValueSource) ValueSourceChain {
48 return ValueSourceChain{
49 Chain: src,
50 }
51 }
52
53 func (vsc *ValueSourceChain) Append(other ValueSourceChain) {
54 vsc.Chain = append(vsc.Chain, other.Chain...)
55 }
56
57 func (vsc *ValueSourceChain) EnvKeys() []string {
58 vals := []string{}
59
60 for _, src := range vsc.Chain {
61 if v, ok := src.(EnvValueSource); ok && v.IsFromEnv() {
62 vals = append(vals, v.Key())
63 }
64 }
65
66 return vals
67 }
68
69 func (vsc *ValueSourceChain) String() string {
70 s := []string{}
71
72 for _, vs := range vsc.Chain {
73 s = append(s, vs.String())
74 }
75
76 return strings.Join(s, ",")
77 }
78
79 func (vsc *ValueSourceChain) GoString() string {
80 s := []string{}
81
82 for _, vs := range vsc.Chain {
83 s = append(s, vs.GoString())
84 }
85
86 return fmt.Sprintf("&ValueSourceChain{Chain:{%[1]s}}", strings.Join(s, ","))
87 }
88
89 func (vsc *ValueSourceChain) Lookup() (string, bool) {
90 s, _, ok := vsc.LookupWithSource()
91 return s, ok
92 }
93
94 func (vsc *ValueSourceChain) LookupWithSource() (string, ValueSource, bool) {
95 for _, src := range vsc.Chain {
96 if value, found := src.Lookup(); found {
97 return value, src, true
98 }
99 }
100
101 return "", nil, false
102 }
103
104 // envVarValueSource encapsulates a ValueSource from an environment variable
105 type envVarValueSource struct {
106 key string
107 }
108
109 func (e *envVarValueSource) Lookup() (string, bool) {
110 return os.LookupEnv(strings.TrimSpace(string(e.key)))
111 }
112
113 func (e *envVarValueSource) IsFromEnv() bool {
114 return true
115 }
116
117 func (e *envVarValueSource) Key() string {
118 return e.key
119 }
120
121 func (e *envVarValueSource) String() string { return fmt.Sprintf("environment variable %[1]q", e.key) }
122 func (e *envVarValueSource) GoString() string {
123 return fmt.Sprintf("&envVarValueSource{Key:%[1]q}", e.key)
124 }
125
126 func EnvVar(key string) ValueSource {
127 return &envVarValueSource{
128 key: key,
129 }
130 }
131
132 // EnvVars is a helper function to encapsulate a number of
133 // envVarValueSource together as a ValueSourceChain
134 func EnvVars(keys ...string) ValueSourceChain {
135 vsc := ValueSourceChain{Chain: []ValueSource{}}
136
137 for _, key := range keys {
138 vsc.Chain = append(vsc.Chain, EnvVar(key))
139 }
140
141 return vsc
142 }
143
144 // fileValueSource encapsulates a ValueSource from a file
145 type fileValueSource struct {
146 Path string
147 }
148
149 func (f *fileValueSource) Lookup() (string, bool) {
150 data, err := os.ReadFile(f.Path)
151 return string(data), err == nil
152 }
153
154 func (f *fileValueSource) String() string { return fmt.Sprintf("file %[1]q", f.Path) }
155 func (f *fileValueSource) GoString() string {
156 return fmt.Sprintf("&fileValueSource{Path:%[1]q}", f.Path)
157 }
158
159 func File(path string) ValueSource {
160 return &fileValueSource{Path: path}
161 }
162
163 // Files is a helper function to encapsulate a number of
164 // fileValueSource together as a ValueSourceChain
165 func Files(paths ...string) ValueSourceChain {
166 vsc := ValueSourceChain{Chain: []ValueSource{}}
167
168 for _, path := range paths {
169 vsc.Chain = append(vsc.Chain, File(path))
170 }
171
172 return vsc
173 }
174
175 type mapSource struct {
176 name string
177 m map[any]any
178 }
179
180 func NewMapSource(name string, m map[any]any) MapSource {
181 return &mapSource{
182 name: name,
183 m: m,
184 }
185 }
186
187 func (ms *mapSource) String() string { return fmt.Sprintf("map source %[1]q", ms.name) }
188 func (ms *mapSource) GoString() string {
189 return fmt.Sprintf("&mapSource{name:%[1]q}", ms.name)
190 }
191
192 // Lookup returns a value from the map source. The lookup name may be a dot-separated path into the map.
193 // If that is the case, it will recursively traverse the map based on the '.' delimited sections to find
194 // a nested value for the key.
195 func (ms *mapSource) Lookup(name string) (any, bool) {
196 sections := strings.Split(name, ".")
197 if name == "" || len(sections) == 0 {
198 return nil, false
199 }
200
201 node := ms.m
202
203 // traverse into the map based on the dot-separated sections
204 if len(sections) >= 2 { // the last section is the value we want, we will return it directly at the end
205 for _, section := range sections[:len(sections)-1] {
206 child, ok := node[section]
207 if !ok {
208 return nil, false
209 }
210
211 switch child := child.(type) {
212 case map[string]any:
213 node = make(map[any]any, len(child))
214 for k, v := range child {
215 node[k] = v
216 }
217 case map[any]any:
218 node = child
219 default:
220 return nil, false
221 }
222 }
223 }
224
225 if val, ok := node[sections[len(sections)-1]]; ok {
226 return val, true
227 }
228 return nil, false
229 }
230
231 type mapValueSource struct {
232 key string
233 ms MapSource
234 }
235
236 func NewMapValueSource(key string, ms MapSource) ValueSource {
237 return &mapValueSource{
238 key: key,
239 ms: ms,
240 }
241 }
242
243 func (mvs *mapValueSource) String() string {
244 return fmt.Sprintf("key %[1]q from %[2]s", mvs.key, mvs.ms.String())
245 }
246
247 func (mvs *mapValueSource) GoString() string {
248 return fmt.Sprintf("&mapValueSource{key:%[1]q, src:%[2]s}", mvs.key, mvs.ms.GoString())
249 }
250
251 func (mvs *mapValueSource) Lookup() (string, bool) {
252 if v, ok := mvs.ms.Lookup(mvs.key); !ok {
253 return "", false
254 } else {
255 return fmt.Sprintf("%+v", v), true
256 }
257 }