forked from urfave/cli-altsrc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaltsrc.go
139 lines (115 loc) · 2.75 KB
/
altsrc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package altsrc
import (
"errors"
"fmt"
"os"
"runtime"
"strings"
)
var (
Err = errors.New("urfave/cli-altsrc error")
isTracingOn = os.Getenv("URFAVE_CLI_TRACING") == "on"
)
func tracef(format string, a ...any) {
if !isTracingOn {
return
}
if !strings.HasSuffix(format, "\n") {
format = format + "\n"
}
pc, file, line, _ := runtime.Caller(1)
cf := runtime.FuncForPC(pc)
fmt.Fprintf(
os.Stderr,
strings.Join([]string{
"## URFAVE CLI TRACE ",
file,
":",
fmt.Sprintf("%v", line),
" ",
fmt.Sprintf("(%s)", cf.Name()),
" ",
format,
}, ""),
a...,
)
}
// NestedVal returns a value from the given map. The lookup name may be a dot-separated path into the map.
// If that is the case, it will recursively traverse the map based on the '.' delimited sections to find
// a nested value for the key.
func NestedVal(name string, tree map[any]any) (any, bool) {
sections := strings.Split(name, ".")
if name == "" || len(sections) == 0 {
return nil, false
}
node := tree
// traverse into the map based on the dot-separated sections
if len(sections) >= 2 { // the last section is the value we want, we will return it directly at the end
for _, section := range sections[:len(sections)-1] {
child, ok := node[section]
if !ok {
return nil, false
}
switch child := child.(type) {
case map[string]any:
node = make(map[any]any, len(child))
for k, v := range child {
node[k] = v
}
case map[any]any:
node = child
default:
return nil, false
}
}
}
if val, ok := node[sections[len(sections)-1]]; ok {
return val, true
}
return nil, false
}
type Sourcer interface {
SourceURI() string
}
type StringSourcer string
func (s StringSourcer) SourceURI() string {
return string(s)
}
type StringPtrSourcer struct {
ptr *string
}
func NewStringPtrSourcer(p *string) StringPtrSourcer {
return StringPtrSourcer{
ptr: p,
}
}
func (s StringPtrSourcer) SourceURI() string {
return *s.ptr
}
type ValueSource struct {
key string
desc string
sourcer Sourcer
um func([]byte, any) error
}
func (vs *ValueSource) Lookup() (string, bool) {
maafsc := NewMapAnyAnyURISourceCache(vs.sourcer.SourceURI(), vs.um)
if v, ok := NestedVal(vs.key, maafsc.Get()); ok {
return fmt.Sprintf("%[1]v", v), ok
}
return "", false
}
func (vs *ValueSource) String() string {
return fmt.Sprintf("%s file %[2]q at key %[3]q", vs.desc, vs.sourcer.SourceURI(), vs.key)
}
func (vs *ValueSource) GoString() string {
return fmt.Sprintf("%sValueSource{file:%[2]q,keyPath:%[3]q}", vs.desc, vs.sourcer.SourceURI(), vs.key)
}
func NewValueSource(f func([]byte, any) error, desc string, key string, uriSrc Sourcer) *ValueSource {
return &ValueSource{
sourcer: uriSrc,
key: key,
desc: desc,
um: f,
}
}