github.com/paultyng/terraform@v0.6.11-0.20180227224804-66ff8f8bed40/configs/parser.go (about) 1 package configs 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/hashicorp/hcl2/hcl" 8 "github.com/hashicorp/hcl2/hclparse" 9 "github.com/spf13/afero" 10 ) 11 12 // Parser is the main interface to read configuration files and other related 13 // files from disk. 14 // 15 // It retains a cache of all files that are loaded so that they can be used 16 // to create source code snippets in diagnostics, etc. 17 type Parser struct { 18 fs afero.Afero 19 p *hclparse.Parser 20 } 21 22 // NewParser creates and returns a new Parser that reads files from the given 23 // filesystem. If a nil filesystem is passed then the system's "real" filesystem 24 // will be used, via afero.OsFs. 25 func NewParser(fs afero.Fs) *Parser { 26 if fs == nil { 27 fs = afero.OsFs{} 28 } 29 30 return &Parser{ 31 fs: afero.Afero{Fs: fs}, 32 p: hclparse.NewParser(), 33 } 34 } 35 36 // LoadHCLFile is a low-level method that reads the file at the given path, 37 // parses it, and returns the hcl.Body representing its root. In many cases 38 // it is better to use one of the other Load*File methods on this type, 39 // which additionally decode the root body in some way and return a higher-level 40 // construct. 41 // 42 // If the file cannot be read at all -- e.g. because it does not exist -- then 43 // this method will return a nil body and error diagnostics. In this case 44 // callers may wish to ignore the provided error diagnostics and produce 45 // a more context-sensitive error instead. 46 // 47 // The file will be parsed using the HCL native syntax unless the filename 48 // ends with ".json", in which case the HCL JSON syntax will be used. 49 func (p *Parser) LoadHCLFile(path string) (hcl.Body, hcl.Diagnostics) { 50 src, err := p.fs.ReadFile(path) 51 52 if err != nil { 53 return nil, hcl.Diagnostics{ 54 { 55 Severity: hcl.DiagError, 56 Summary: "Failed to read file", 57 Detail: fmt.Sprintf("The file %q could not be read.", path), 58 }, 59 } 60 } 61 62 var file *hcl.File 63 var diags hcl.Diagnostics 64 switch { 65 case strings.HasSuffix(path, ".json"): 66 file, diags = p.p.ParseJSON(src, path) 67 default: 68 file, diags = p.p.ParseHCL(src, path) 69 } 70 71 // If the returned file or body is nil, then we'll return a non-nil empty 72 // body so we'll meet our contract that nil means an error reading the file. 73 if file == nil || file.Body == nil { 74 return hcl.EmptyBody(), diags 75 } 76 77 return file.Body, diags 78 } 79 80 // Sources returns a map of the cached source buffers for all files that 81 // have been loaded through this parser, with source filenames (as requested 82 // when each file was opened) as the keys. 83 func (p *Parser) Sources() map[string][]byte { 84 return p.p.Sources() 85 }