-
Notifications
You must be signed in to change notification settings - Fork 4
/
altsrc.go
75 lines (65 loc) · 1.33 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
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 checks if the name has '.' delimiters.
// If so, it tries to traverse the tree by the '.' delimited sections to find
// a nested value for the key.
func NestedVal(name string, tree map[any]any) (any, bool) {
if sections := strings.Split(name, "."); len(sections) > 1 {
node := tree
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
}