-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoml.go
More file actions
112 lines (91 loc) · 2.39 KB
/
toml.go
File metadata and controls
112 lines (91 loc) · 2.39 KB
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
package structconf
import (
"fmt"
"os"
"strings"
"github.com/BurntSushi/toml"
"github.com/urfave/cli/v3"
)
// workaround until https://github.com/urfave/cli/issues/2037 and https://github.com/urfave/cli-altsrc/issues/14
// are fixed
type mapSource struct {
name string
m map[any]any
}
func NewMapSource(name string, m map[any]any) cli.MapSource {
return &mapSource{
name: name,
m: m,
}
}
func NewTomlFileSource(name string, file string) (cli.MapSource, error) {
data, err := os.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("failed to read file %q: %w", file, err)
}
container := make(map[any]any)
if err := toml.Unmarshal(data, &container); err != nil {
return nil, fmt.Errorf("failed to parse file as toml %q: %w", file, err)
}
return NewMapSource(name, container), nil
}
func (ms *mapSource) String() string { return fmt.Sprintf("map source %[1]q", ms.name) }
func (ms *mapSource) GoString() string {
return fmt.Sprintf("&mapSource{flag:%[1]q}", ms.name)
}
func (ms *mapSource) Lookup(name string) (any, bool) {
node := ms.m
sections := strings.Split(name, ".")
if len(sections) == 0 {
return nil, false
}
// recurse into the map, splitting the key on "."
if len(sections) > 1 {
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 //nolint: modernize
}
case map[any]any:
node = child
default:
return nil, false
}
}
}
// now lookup the last section
if val, ok := node[sections[len(sections)-1]]; ok {
return val, true
}
return nil, false
}
type mapsValueSource struct {
key string
maps []cli.MapSource
}
func (mvs *mapsValueSource) String() string {
return fmt.Sprintf("key %[1]q from %[2]d maps", mvs.key, len(mvs.maps))
}
func (mvs *mapsValueSource) GoString() string {
return fmt.Sprintf("&mapsValueSource{key:%[1]q, src:%[2]v}", mvs.key, mvs.maps)
}
func (mvs *mapsValueSource) Lookup() (string, bool) {
for _, ms := range mvs.maps {
if v, ok := ms.Lookup(mvs.key); ok { // return the first defaultValue found
return fmt.Sprintf("%+v", v), true
}
}
return "", false
}
func NewValueSourceFromMaps(key string, sources ...cli.MapSource) cli.ValueSource {
return &mapsValueSource{
key: key,
maps: sources,
}
}