github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/steampipeconfig/parse/plugin.go (about) 1 package parse 2 3 import ( 4 "github.com/hashicorp/hcl/v2" 5 "github.com/hashicorp/hcl/v2/gohcl" 6 "github.com/hashicorp/hcl/v2/hclsyntax" 7 8 "github.com/turbot/steampipe/pkg/steampipeconfig/modconfig" 9 ) 10 11 func DecodePlugin(block *hcl.Block) (*modconfig.Plugin, hcl.Diagnostics) { 12 // manually decode child limiter blocks 13 content, rest, diags := block.Body.PartialContent(PluginBlockSchema) 14 if diags.HasErrors() { 15 return nil, diags 16 } 17 body := rest.(*hclsyntax.Body) 18 19 // decode attributes using 'rest' (these are automativally parsed so are not in schema) 20 var plugin = &modconfig.Plugin{ 21 // default source and name to label 22 Instance: block.Labels[0], 23 Alias: block.Labels[0], 24 } 25 moreDiags := gohcl.DecodeBody(body, nil, plugin) 26 if moreDiags.HasErrors() { 27 diags = append(diags, moreDiags...) 28 return nil, diags 29 } 30 31 // decode limiter blocks using 'content' 32 for _, block := range content.Blocks { 33 switch block.Type { 34 // only block defined in schema 35 case modconfig.BlockTypeRateLimiter: 36 limiter, moreDiags := DecodeLimiter(block) 37 diags = append(diags, moreDiags...) 38 if moreDiags.HasErrors() { 39 continue 40 } 41 limiter.SetPlugin(plugin) 42 plugin.Limiters = append(plugin.Limiters, limiter) 43 } 44 } 45 if !diags.HasErrors() { 46 plugin.OnDecoded(block) 47 } 48 49 return plugin, diags 50 }