hclparse

package
v0.68.15-beta2024111501 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 15, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package hclparse provides a wrapper around the HCL2 parser to handle diagnostics and errors in a more user-friendly way.

The package wraps `hclparse.Parser` to be able to handle diagnostic errors from one place, see `handleDiagnostics(diags hcl.Diagnostics) error` func. This allows us to halt the process only when certain errors occur, such as skipping all errors not related to the `catalog` block.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attribute

type Attribute struct {
	*File
	*hcl.Attribute
}

func (*Attribute) ValidateIdentifier

func (attr *Attribute) ValidateIdentifier() error

func (*Attribute) Value

func (attr *Attribute) Value(evalCtx *hcl.EvalContext) (cty.Value, error)

type Attributes

type Attributes []*Attribute

func NewAttributes

func NewAttributes(file *File, hclAttrs hcl.Attributes) Attributes

func (Attributes) Range added in v0.61.0

func (attrs Attributes) Range() hcl.Range

func (Attributes) ValidateIdentifier

func (attrs Attributes) ValidateIdentifier() error

type Block

type Block struct {
	*File
	*hcl.Block
}

func (*Block) JustAttributes

func (block *Block) JustAttributes() (Attributes, error)

JustAttributes loads the block into name expression pairs to assist with evaluation of the attrs prior to evaluating the whole config. Note that this is exactly the same as terraform/configs/named_values.go:decodeLocalsBlock

type File

type File struct {
	*Parser
	*hcl.File
	ConfigPath string
}

func (*File) Blocks

func (file *File) Blocks(name string, isMultipleAllowed bool) ([]*Block, error)

Blocks takes a parsed HCL file and extracts a reference to the `name` block, if there are defined.

func (*File) Content

func (file *File) Content() string

func (*File) Decode

func (file *File) Decode(out interface{}, evalContext *hcl.EvalContext) (err error)

Decode uses the HCL2 parser to decode the parsed HCL into the struct specified by out.

Note that we take a two pass approach to support parsing include blocks without a label. Ideally we can parse include blocks with and without labels in a single pass, but the HCL parser is fairly restrictive when it comes to parsing blocks with labels, requiring the exact number of expected labels in the parsing step. To handle this restriction, we first see if there are any include blocks without any labels, and if there is, we modify it in the file object to inject the label as "".

func (*File) HandleDiagnostics added in v0.61.0

func (file *File) HandleDiagnostics(diags hcl.Diagnostics) error

func (*File) JustAttributes

func (file *File) JustAttributes() (Attributes, error)

func (*File) Update

func (file *File) Update(content []byte) error

Update reparses the file with the new `content`.

type Option

type Option func(*Parser) *Parser

func WithDiagnosticsHandler added in v0.61.0

func WithDiagnosticsHandler(fn func(file *hcl.File, diags hcl.Diagnostics) (hcl.Diagnostics, error)) Option

func WithDiagnosticsWriter added in v0.62.2

func WithDiagnosticsWriter(writer io.Writer, disableColor bool) Option

func WithFileUpdate

func WithFileUpdate(fn func(*File) error) Option

WithFileUpdate sets the `fileUpdateHandlerFunc` func which is run before each file decoding.

func WithHaltOnErrorOnlyForBlocks

func WithHaltOnErrorOnlyForBlocks(blockNames []string) Option

WithHaltOnErrorOnlyForBlocks configures a diagnostic error handler that runs when diagnostic errors occur. If errors occur in the given `blockNames` blocks, parser returns the error to its caller, otherwise it skips the error.

func WithLogger

func WithLogger(logger log.Logger) Option

type PanicWhileParsingConfigError

type PanicWhileParsingConfigError struct {
	ConfigFile     string
	RecoveredValue interface{}
}

func (PanicWhileParsingConfigError) Error

type Parser

type Parser struct {
	*hclparse.Parser
	// contains filtered or unexported fields
}

func NewParser

func NewParser(opts ...Option) *Parser

func (*Parser) GetDiagnosticsWriter added in v0.67.5

func (parser *Parser) GetDiagnosticsWriter(writer io.Writer, disableColor bool) hcl.DiagnosticWriter

GetDiagnosticsWriter returns a hcl2 parsing diagnostics emitter for the current terminal.

func (*Parser) ParseFromBytes

func (parser *Parser) ParseFromBytes(content []byte, configPath string) (file *File, err error)

func (*Parser) ParseFromFile

func (parser *Parser) ParseFromFile(configPath string) (*File, error)

func (*Parser) ParseFromString

func (parser *Parser) ParseFromString(content, configPath string) (file *File, err error)

ParseFromString uses the HCL2 parser to parse the given string into an HCL file body.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL