github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/configs/parser.go (about)

     1  package configs
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/hashicorp/hcl/v2"
     8  	"github.com/hashicorp/hcl/v2/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  	// allowExperiments controls whether we will allow modules to opt in to
    22  	// experimental language features. In main code this will be set only
    23  	// for alpha releases and some development builds. Test code must decide
    24  	// for itself whether to enable it so that tests can cover both the
    25  	// allowed and not-allowed situations.
    26  	allowExperiments bool
    27  }
    28  
    29  // NewParser creates and returns a new Parser that reads files from the given
    30  // filesystem. If a nil filesystem is passed then the system's "real" filesystem
    31  // will be used, via afero.OsFs.
    32  func NewParser(fs afero.Fs) *Parser {
    33  	if fs == nil {
    34  		fs = afero.OsFs{}
    35  	}
    36  
    37  	return &Parser{
    38  		fs: afero.Afero{Fs: fs},
    39  		p:  hclparse.NewParser(),
    40  	}
    41  }
    42  
    43  // LoadHCLFile is a low-level method that reads the file at the given path,
    44  // parses it, and returns the hcl.Body representing its root. In many cases
    45  // it is better to use one of the other Load*File methods on this type,
    46  // which additionally decode the root body in some way and return a higher-level
    47  // construct.
    48  //
    49  // If the file cannot be read at all -- e.g. because it does not exist -- then
    50  // this method will return a nil body and error diagnostics. In this case
    51  // callers may wish to ignore the provided error diagnostics and produce
    52  // a more context-sensitive error instead.
    53  //
    54  // The file will be parsed using the HCL native syntax unless the filename
    55  // ends with ".json", in which case the HCL JSON syntax will be used.
    56  func (p *Parser) LoadHCLFile(path string) (hcl.Body, hcl.Diagnostics) {
    57  	src, err := p.fs.ReadFile(path)
    58  
    59  	if err != nil {
    60  		return nil, hcl.Diagnostics{
    61  			{
    62  				Severity: hcl.DiagError,
    63  				Summary:  "Failed to read file",
    64  				Detail:   fmt.Sprintf("The file %q could not be read.", path),
    65  			},
    66  		}
    67  	}
    68  
    69  	var file *hcl.File
    70  	var diags hcl.Diagnostics
    71  	switch {
    72  	case strings.HasSuffix(path, ".json"):
    73  		file, diags = p.p.ParseJSON(src, path)
    74  	default:
    75  		file, diags = p.p.ParseHCL(src, path)
    76  	}
    77  
    78  	// If the returned file or body is nil, then we'll return a non-nil empty
    79  	// body so we'll meet our contract that nil means an error reading the file.
    80  	if file == nil || file.Body == nil {
    81  		return hcl.EmptyBody(), diags
    82  	}
    83  
    84  	return file.Body, diags
    85  }
    86  
    87  // Sources returns a map of the cached source buffers for all files that
    88  // have been loaded through this parser, with source filenames (as requested
    89  // when each file was opened) as the keys.
    90  func (p *Parser) Sources() map[string][]byte {
    91  	return p.p.Sources()
    92  }
    93  
    94  // ForceFileSource artificially adds source code to the cache of file sources,
    95  // as if it had been loaded from the given filename.
    96  //
    97  // This should be used only in special situations where configuration is loaded
    98  // some other way. Most callers should load configuration via methods of
    99  // Parser, which will update the sources cache automatically.
   100  func (p *Parser) ForceFileSource(filename string, src []byte) {
   101  	// We'll make a synthetic hcl.File here just so we can reuse the
   102  	// existing cache.
   103  	p.p.AddFile(filename, &hcl.File{
   104  		Body:  hcl.EmptyBody(),
   105  		Bytes: src,
   106  	})
   107  }
   108  
   109  // AllowLanguageExperiments specifies whether subsequent LoadConfigFile (and
   110  // similar) calls will allow opting in to experimental language features.
   111  //
   112  // If this method is never called for a particular parser, the default behavior
   113  // is to disallow language experiments.
   114  //
   115  // Main code should set this only for alpha or development builds. Test code
   116  // is responsible for deciding for itself whether and how to call this
   117  // method.
   118  func (p *Parser) AllowLanguageExperiments(allowed bool) {
   119  	p.allowExperiments = allowed
   120  }