github.com/lyraproj/hiera@v1.0.0-rc4/provider/yamldata.go (about) 1 package provider 2 3 import ( 4 "io/ioutil" 5 "os" 6 7 "github.com/lyraproj/dgo/dgo" 8 "github.com/lyraproj/dgo/vf" 9 "github.com/lyraproj/dgoyaml/yaml" 10 "github.com/lyraproj/hiera/api" 11 "github.com/lyraproj/hierasdk/hiera" 12 "github.com/tada/catch" 13 ) 14 15 // YamlData is a data_hash provider that reads a YAML hash from a file and returns it as a Map 16 func YamlData(ctx hiera.ProviderContext) dgo.Map { 17 pv := ctx.Option(`path`) 18 if pv == nil { 19 panic(api.MissingRequiredOption(`path`)) 20 } 21 path := pv.(dgo.String).GoString() 22 bs, err := ioutil.ReadFile(path) 23 if err != nil { 24 if os.IsNotExist(err) { 25 return vf.Map() 26 } 27 panic(catch.Error("could not read %s: %s", path, err.Error())) 28 } 29 v, err := yaml.Unmarshal(bs) 30 if err != nil { 31 panic(catch.Error("could not unmarshal %s: %s", path, err.Error())) 32 } 33 if data, ok := v.(dgo.Map); ok { 34 return data 35 } 36 panic(api.YamlNotHash(path)) 37 }