Help us shape the future of Go! Take the 2026 Go Developer Survey

terraform

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RootBlockReflectionInformation = func(v map[string]cty.Value, b *RootBlock) {
	var moduleObj = cty.ObjectVal(map[string]cty.Value{
		"key":      cty.StringVal(""),
		"version":  cty.StringVal(""),
		"source":   cty.StringVal(""),
		"dir":      cty.StringVal(""),
		"abs_dir":  cty.StringVal(""),
		"git_hash": cty.StringVal(""),
	})
	if b.module != nil {
		moduleObj = cty.ObjectVal(map[string]cty.Value{
			"key":      cty.StringVal(b.module.Key),
			"version":  cty.StringVal(b.module.Version),
			"source":   cty.StringVal(b.module.Source),
			"dir":      cty.StringVal(b.module.Dir),
			"abs_dir":  cty.StringVal(b.module.AbsDir),
			"git_hash": cty.StringVal(b.module.GitHash),
		})
	}
	labels := golden.ToCtyValue(b.Labels)
	v["mptf"] = cty.ObjectVal(map[string]cty.Value{
		"block_address":     cty.StringVal(b.Address),
		"terraform_address": cty.StringVal(blockAddressToRef(b.Address)),
		"block_type":        cty.StringVal(b.Type),
		"block_labels":      labels,
		"module":            moduleObj,
		"range": cty.ObjectVal(map[string]cty.Value{
			"file_name":    cty.StringVal(b.Range().Filename),
			"start_line":   cty.NumberIntVal(int64(b.Range().Start.Line)),
			"start_column": cty.NumberIntVal(int64(b.Range().Start.Column)),
			"end_line":     cty.NumberIntVal(int64(b.Range().End.Line)),
			"end_column":   cty.NumberIntVal(int64(b.Range().End.Column)),
		}),
	})
}

Functions

func ListOfObject

func ListOfObject[T Object](objs []T) cty.Value

func SetNormalizeOptions added in v0.1.2

func SetNormalizeOptions(opts NormalizeOptions)

SetNormalizeOptions overrides the package-level options consulted by SaveToDisk when it calls normalizeFileWhitespace. Intended to be called once at startup from the CLI based on user flags. Not goroutine-safe; callers should set this before kicking off any transform that may write to disk.

func TestFilePath added in v0.3.0

func TestFilePath(moduleDir, filename string) (string, error)

TestFilePath resolves one test file relative to its owning module.

Types

type Attribute

type Attribute struct {
	Name string
	*hclsyntax.Attribute
	WriteAttribute *hclwrite.Attribute
}

func NewAttribute

func NewAttribute(name string, attribute *hclsyntax.Attribute, writeAttribute *hclwrite.Attribute) *Attribute

func (*Attribute) String

func (a *Attribute) String() string

type Block

type Block interface {
	EvalContext() cty.Value
	GetAttributes() map[string]*Attribute
	GetNestedBlocks() NestedBlocks
	WriteBody() *hclwrite.Body
	SetAttributeRaw(name string, tokens hclwrite.Tokens)
	AppendBlock(block *hclwrite.Block)
	Range() hcl.Range
	RemoveContent(path string)
}

type Module

type Module struct {
	Dir    string
	AbsDir string

	ResourceBlocks  []*RootBlock
	DataBlocks      []*RootBlock
	EphemeralBlocks []*RootBlock
	ModuleBlocks    []*RootBlock
	TerraformBlocks []*RootBlock
	Variables       []*RootBlock
	Outputs         []*RootBlock
	Locals          []*RootBlock
	MovedBlocks     []*RootBlock
	Key             string
	Source          string
	Version         string
	GitHash         string
	TestFile        string
	TestBlocks      []*RootBlock
	// contains filtered or unexported fields
}

func LoadModule

func LoadModule(mr ModuleRef) (*Module, error)

func (*Module) AddBlock

func (m *Module) AddBlock(fileName string, block *hclwrite.Block) error

func (*Module) Blocks

func (m *Module) Blocks() []*RootBlock

func (*Module) RemoveBlock

func (m *Module) RemoveBlock(block *hclwrite.Block)

func (*Module) SaveToDisk

func (m *Module) SaveToDisk() error

func (*Module) ValidateTargetFile added in v0.3.0

func (m *Module) ValidateTargetFile(fileName string) error

type ModuleRef

type ModuleRef struct {
	Key      string `json:"Key"`
	Source   string `json:"Source"`
	Dir      string `json:"Dir"`
	AbsDir   string
	Version  string `json:"Version"`
	GitHash  string
	TestFile string
}

type NestedBlock

type NestedBlock struct {
	Type string
	*hclsyntax.Block

	WriteBlock   *hclwrite.Block
	ForEach      *Attribute
	Iterator     *Attribute
	Attributes   map[string]*Attribute
	NestedBlocks NestedBlocks
	// contains filtered or unexported fields
}

func NewNestedBlock

func NewNestedBlock(rb *hclsyntax.Block, wb *hclwrite.Block) *NestedBlock

func (*NestedBlock) AppendBlock

func (nb *NestedBlock) AppendBlock(block *hclwrite.Block)

func (*NestedBlock) EvalContext

func (nb *NestedBlock) EvalContext() cty.Value

func (*NestedBlock) GetAttributes

func (nb *NestedBlock) GetAttributes() map[string]*Attribute

func (*NestedBlock) GetNestedBlocks

func (nb *NestedBlock) GetNestedBlocks() NestedBlocks

func (*NestedBlock) MptfObject

func (nb *NestedBlock) MptfObject() cty.Value

func (*NestedBlock) RemoveContent

func (nb *NestedBlock) RemoveContent(path string)

func (*NestedBlock) SetAttributeRaw

func (nb *NestedBlock) SetAttributeRaw(name string, tokens hclwrite.Tokens)

func (*NestedBlock) String

func (nb *NestedBlock) String() string

func (*NestedBlock) WriteBody

func (nb *NestedBlock) WriteBody() *hclwrite.Body

type NestedBlocks

type NestedBlocks map[string][]*NestedBlock

func (NestedBlocks) Values

func (nbs NestedBlocks) Values() map[string]cty.Value

type NormalizeOptions added in v0.1.2

type NormalizeOptions struct {
	// KeepAdjacentSameKindUnlabeledBlocks, when true, suppresses the blank
	// line between adjacent root-level blocks that have no labels AND share
	// the same leading identifier (e.g. two `locals {}` or two `moved {}`).
	// Labeled blocks (`variable "a" {}` and `variable "b" {}`, two `resource`
	// blocks of the same type, etc.) are unaffected — they always get a
	// blank line between them.
	//
	// Default false: every pair of root blocks gets exactly one blank line
	// between them regardless of kind or label, matching the v0.1.1
	// normalizer's behaviour. This is the safe default because most existing
	// Terraform style guides and tooling pipelines expect a blank line
	// between every root block.
	//
	// Set to true via the `--keep-adjacent-blocks` CLI flag when a target
	// repository prefers grouping adjacent same-kind unlabeled blocks
	// together (matches the way `terraform fmt` leaves hand-written
	// adjacent `locals {}` blocks).
	KeepAdjacentSameKindUnlabeledBlocks bool
}

NormalizeOptions tunes normalizeFileWhitespace's inter-block layout behaviour. The zero value is the default behaviour applied by SaveToDisk when no CLI override is provided.

func NormalizeOptionsValue added in v0.1.2

func NormalizeOptionsValue() NormalizeOptions

NormalizeOptionsValue returns the current package-level NormalizeOptions previously set via SetNormalizeOptions (or the zero value if none was set). Exposed for tests and diagnostic logging.

type Object

type Object interface {
	EvalContext() cty.Value
}

type RootBlock

type RootBlock struct {
	*hclsyntax.Block

	WriteBlock   *hclwrite.Block
	Count        *Attribute
	ForEach      *Attribute
	Attributes   map[string]*Attribute
	NestedBlocks NestedBlocks
	Type         string
	Labels       []string
	Address      string
	// contains filtered or unexported fields
}

func NewBlock

func NewBlock(m *Module, rb *hclsyntax.Block, wb *hclwrite.Block) *RootBlock

func (*RootBlock) AppendBlock

func (b *RootBlock) AppendBlock(block *hclwrite.Block)

func (*RootBlock) EvalContext

func (b *RootBlock) EvalContext() cty.Value

func (*RootBlock) GetAttributes

func (b *RootBlock) GetAttributes() map[string]*Attribute

func (*RootBlock) GetNestedBlocks

func (b *RootBlock) GetNestedBlocks() NestedBlocks

func (*RootBlock) RemoveContent

func (b *RootBlock) RemoveContent(path string)

func (*RootBlock) SetAttributeRaw

func (b *RootBlock) SetAttributeRaw(name string, tokens hclwrite.Tokens)

func (*RootBlock) WriteBody

func (b *RootBlock) WriteBody() *hclwrite.Body

Jump to

Keyboard shortcuts

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