-
Notifications
You must be signed in to change notification settings - Fork 2
/
toml_value_source.go
66 lines (52 loc) · 1.26 KB
/
toml_value_source.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
package altsrc
import (
"fmt"
"github.com/BurntSushi/toml"
"github.com/urfave/cli/v3"
)
// TOML is a helper function to encapsulate a number of
// tomlValueSource together as a cli.ValueSourceChain
func TOML(key string, paths ...string) cli.ValueSourceChain {
vsc := cli.ValueSourceChain{Chain: []cli.ValueSource{}}
for _, path := range paths {
vsc.Chain = append(
vsc.Chain,
&tomlValueSource{
file: path,
key: key,
tmc: tomlMapFileSourceCache{
file: path,
f: tomlUnmarshalFile,
},
},
)
}
return vsc
}
type tomlValueSource struct {
file string
key string
tmc tomlMapFileSourceCache
}
func (tvs *tomlValueSource) Lookup() (string, bool) {
if v, ok := nestedVal(tvs.key, tvs.tmc.Get().Map); ok {
return fmt.Sprintf("%[1]v", v), ok
}
return "", false
}
func (tvs *tomlValueSource) String() string {
return fmt.Sprintf("toml file %[1]q at key %[2]q", tvs.file, tvs.key)
}
func (tvs *tomlValueSource) GoString() string {
return fmt.Sprintf("&tomlValueSource{file:%[1]q,keyPath:%[2]q}", tvs.file, tvs.key)
}
func tomlUnmarshalFile(filePath string, container any) error {
b, err := readURI(filePath)
if err != nil {
return err
}
if err := toml.Unmarshal(b, container); err != nil {
return err
}
return nil
}