github.com/mineiros-io/terradoc@v0.0.9-0.20220711062319-018bd4ae81f5/internal/parsers/docparser/docparser.go (about) 1 package docparser 2 3 import ( 4 "fmt" 5 "io" 6 7 "github.com/hashicorp/hcl/v2/hclparse" 8 "github.com/mineiros-io/terradoc/internal/entities" 9 ) 10 11 const ( 12 titleAttributeName = "title" 13 contentAttributeName = "content" 14 descriptionAttributeName = "description" 15 typeAttributeName = "type" 16 readmeTypeAttributeName = "readmeType" 17 defaultAttributeName = "default" 18 requiredAttributeName = "required" 19 forcesRecreationAttributeName = "forces_recreation" 20 readmeExampleAttributeName = "readme_example" 21 valueAttributeName = "value" 22 imageAttributeName = "image" 23 urlAttributeName = "url" 24 textAttributeName = "text" 25 tocAttributeName = "toc" 26 27 sectionBlockName = "section" 28 variableBlockName = "variable" 29 attributeBlockName = "attribute" 30 referencesBlockName = "references" 31 refBlockName = "ref" 32 headerBlockName = "header" 33 badgeBlockName = "badge" 34 outputBlockName = "output" 35 ) 36 37 // Parse reads the content of a io.Reader and returns a Definition entity from its parsed values 38 func Parse(r io.Reader, filename string) (entities.Doc, error) { 39 src, err := io.ReadAll(r) 40 if err != nil { 41 return entities.Doc{}, err 42 } 43 44 return parseHCL(src, filename) 45 } 46 47 func parseHCL(src []byte, filename string) (entities.Doc, error) { 48 p := hclparse.NewParser() 49 50 f, diags := p.ParseHCL(src, filename) 51 if diags.HasErrors() { 52 return entities.Doc{}, fmt.Errorf("parsing HCL: %v", diags.Errs()) 53 } 54 55 return parseDoc(f) 56 }