Documentation
¶
Overview ¶
Package hcl provides address-based HCL editing for Terraform component files, preserving comments and formatting.
It wraps github.com/minamijoyo/hcledit/editor, the same engine behind the hcledit CLI, and mirrors pkg/yaml's Get/Set/Delete-style API so structural edits to .tf files follow the same shape as structural edits to YAML stack manifests.
Example usage:
out, err := hcl.SetAttribute(content, "resource.aws_instance.web.instance_type", `"t3.micro"`)
if err != nil {
return err
}
Index ¶
- Variables
- func AppendAttribute(content []byte, address, value string, newline bool) ([]byte, error)
- func AppendAttributeFile(filePath, address, value string, newline bool) error
- func AppendBlock(content []byte, parent, child string, newline bool) ([]byte, error)
- func AppendBlockFile(filePath, parent, child string, newline bool) error
- func Get(content []byte, address string, withComments bool) (string, error)
- func GetFile(filePath, address string, withComments bool) (string, error)
- func NewBlock(content []byte, address string, newline bool) ([]byte, error)
- func NewBlockFile(filePath, address string, newline bool) error
- func RemoveAttribute(content []byte, address string) ([]byte, error)
- func RemoveAttributeFile(filePath, address string) error
- func RemoveBlock(content []byte, address string) ([]byte, error)
- func RemoveBlockFile(filePath, address string) error
- func SetAttribute(content []byte, address, value string) ([]byte, error)
- func SetAttributeFile(filePath, address, value string) error
Constants ¶
This section is empty.
Variables ¶
var ( // ErrHCLAddressNotFound is returned when an address does not resolve to // an attribute or a block. ErrHCLAddressNotFound = errors.New("HCL address not found") // ErrHCLUpdateFailed is returned when an edit operation fails (invalid // address syntax, attribute already exists for an append, etc.). ErrHCLUpdateFailed = errors.New("failed to update HCL") // ErrHCLInvalidResult is returned when an edit would produce HCL that // fails to parse. The strict editing contract: never persist this. ErrHCLInvalidResult = errors.New("edit would produce invalid HCL") // ErrHCLParseFailed is returned when the source content is not valid HCL. ErrHCLParseFailed = errors.New("failed to parse HCL") // ErrHCLReadFile is returned when a file cannot be read. ErrHCLReadFile = errors.New("failed to read file") // ErrHCLWriteFile is returned when a file cannot be written. ErrHCLWriteFile = errors.New("failed to write file") )
Functions ¶
func AppendAttribute ¶
AppendAttribute adds a new attribute at address with value (a raw HCL expression). Fails if the attribute already exists.
func AppendAttributeFile ¶
AppendAttributeFile adds a new attribute at address in a file.
func AppendBlock ¶
AppendBlock creates a new empty child block (type plus labels, relative to parent) inside every block matching parent's address.
func AppendBlockFile ¶
AppendBlockFile creates a new empty child block inside every block matching parent's address in a file.
func Get ¶
Get reads the value at address, trying an attribute lookup first and falling back to a block lookup if the address doesn't resolve to an attribute. The withComments flag includes an attribute's inline trailing comment in the result; it has no effect on block results (a block's own comments are always part of its text). Returns ErrHCLAddressNotFound if address resolves to neither.
func NewBlock ¶
NewBlock creates a new empty block at address (type plus labels, e.g. "resource.aws_instance.web").
func NewBlockFile ¶
NewBlockFile creates a new empty block at address in a file.
func RemoveAttribute ¶
RemoveAttribute removes the attribute at address.
func RemoveAttributeFile ¶
RemoveAttributeFile removes the attribute at address in a file.
func RemoveBlock ¶
RemoveBlock removes every block matching address.
func RemoveBlockFile ¶
RemoveBlockFile removes every block matching address in a file.
func SetAttribute ¶
SetAttribute assigns value (a raw HCL expression, e.g. `"t3.micro"` or `3`) to the attribute at address, preserving comments and formatting.
func SetAttributeFile ¶
SetAttributeFile sets an attribute's value at address in a file, writing the result back atomically while preserving the original file mode.
Types ¶
This section is empty.