7 "github.com/hashicorp/hcl2/hcl"
8 "github.com/hashicorp/hcl2/hclparse"
9 "github.com/spf13/afero"
12 // Parser is the main interface to read configuration files and other related
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.
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 {
31 fs: afero.Afero{Fs: fs},
32 p: hclparse.NewParser(),
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
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.
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)
53 return nil, hcl.Diagnostics{
55 Severity: hcl.DiagError,
56 Summary: "Failed to read file",
57 Detail: fmt.Sprintf("The file %q could not be read.", path),
63 var diags hcl.Diagnostics
65 case strings.HasSuffix(path, ".json"):
66 file, diags = p.p.ParseJSON(src, path)
68 file, diags = p.p.ParseHCL(src, path)
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
77 return file.Body, diags
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 {
87 // ForceFileSource artificially adds source code to the cache of file sources,
88 // as if it had been loaded from the given filename.
90 // This should be used only in special situations where configuration is loaded
91 // some other way. Most callers should load configuration via methods of
92 // Parser, which will update the sources cache automatically.
93 func (p *Parser) ForceFileSource(filename string, src []byte) {
94 // We'll make a synthetic hcl.File here just so we can reuse the
96 p.p.AddFile(filename, &hcl.File{
97 Body: hcl.EmptyBody(),