hcl

package
v1.223.0 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
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

func AppendAttribute(content []byte, address, value string, newline bool) ([]byte, error)

AppendAttribute adds a new attribute at address with value (a raw HCL expression). Fails if the attribute already exists.

func AppendAttributeFile

func AppendAttributeFile(filePath, address, value string, newline bool) error

AppendAttributeFile adds a new attribute at address in a file.

func AppendBlock

func AppendBlock(content []byte, parent, child string, newline bool) ([]byte, error)

AppendBlock creates a new empty child block (type plus labels, relative to parent) inside every block matching parent's address.

func AppendBlockFile

func AppendBlockFile(filePath, parent, child string, newline bool) error

AppendBlockFile creates a new empty child block inside every block matching parent's address in a file.

func Get

func Get(content []byte, address string, withComments bool) (string, error)

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 GetFile

func GetFile(filePath, address string, withComments bool) (string, error)

GetFile reads a Terraform file and returns the value at address.

func NewBlock

func NewBlock(content []byte, address string, newline bool) ([]byte, error)

NewBlock creates a new empty block at address (type plus labels, e.g. "resource.aws_instance.web").

func NewBlockFile

func NewBlockFile(filePath, address string, newline bool) error

NewBlockFile creates a new empty block at address in a file.

func RemoveAttribute

func RemoveAttribute(content []byte, address string) ([]byte, error)

RemoveAttribute removes the attribute at address.

func RemoveAttributeFile

func RemoveAttributeFile(filePath, address string) error

RemoveAttributeFile removes the attribute at address in a file.

func RemoveBlock

func RemoveBlock(content []byte, address string) ([]byte, error)

RemoveBlock removes every block matching address.

func RemoveBlockFile

func RemoveBlockFile(filePath, address string) error

RemoveBlockFile removes every block matching address in a file.

func SetAttribute

func SetAttribute(content []byte, address, value string) ([]byte, error)

SetAttribute assigns value (a raw HCL expression, e.g. `"t3.micro"` or `3`) to the attribute at address, preserving comments and formatting.

func SetAttributeFile

func SetAttributeFile(filePath, address, value string) error

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.

Jump to

Keyboard shortcuts

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