-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
mapper.go
35 lines (30 loc) · 771 Bytes
/
mapper.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
package kongyaml
import (
"os"
"reflect"
"github.com/alecthomas/kong"
"gopkg.in/yaml.v3"
)
// YAMLFileMapper implements kong.MapperValue to decode a YAML file into
// a struct field.
//
// var cli struct {
// Profile Profile `type:"yamlfile"`
// }
//
// func main() {
// kong.Parse(&cli, kong.NamedMapper("yamlfile", YAMLFileMapper))
// }
var YAMLFileMapper = kong.MapperFunc(decodeYAMLFile) //nolint: gochecknoglobals
func decodeYAMLFile(ctx *kong.DecodeContext, target reflect.Value) error {
var fname string
if err := ctx.Scan.PopValueInto("filename", &fname); err != nil {
return err
}
f, err := os.Open(fname) //nolint:gosec
if err != nil {
return err
}
defer f.Close() //nolint
return yaml.NewDecoder(f).Decode(target.Addr().Interface())
}