-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
mapper.go
39 lines (35 loc) · 864 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
36
37
38
39
package konghcl
import (
"io/ioutil"
"os"
"reflect"
"github.com/alecthomas/kong"
"github.com/hashicorp/hcl"
)
// HCLFileMapper implements kong.MapperValue to decode an HCL file into
// a struct field.
//
// var cli struct {
// Profile Profile `type:"hclfile"`
// }
//
// func main() {
// kong.Parse(&cli, kong.NamedMapper("hclfile", konghcl.HCLFileMapper))
// }
var HCLFileMapper = kong.MapperFunc(decodeHCLFile) //hsnolint: gochecknoglobals
func decodeHCLFile(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
b, err := ioutil.ReadAll(f)
if err != nil {
return err
}
return hcl.Unmarshal(b, target.Addr().Interface())
}