engine

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package engine implements tforg's core: parsing Terraform files, moving top-level blocks into their conventional files, duplicate detection, and terraform-fmt-identical formatting via hclwrite.

Index

Constants

View Source
const ConfigFileName = ".tforg.hcl"

ConfigFileName is the per-repo configuration file discovered upward from each target directory.

Variables

This section is empty.

Functions

func Apply

func Apply(o DirOutcome) []string

Apply writes the computed changes to disk. Writes are atomic (temp file + rename) so an interrupted run can never leave a truncated .tf file behind.

func MatchesIgnore

func MatchesIgnore(patterns []string, baseDir, file string) bool

MatchesIgnore reports whether file matches any ignore pattern. Patterns with a slash match the path relative to baseDir (doublestar globs, so **/generated/** works); bare patterns match any single path component, like .gitignore.

func ValidDestName

func ValidDestName(v string) error

ValidDestName checks a mapping/rule destination: a bare .tf file name.

func ValidPattern

func ValidPattern(p string) error

ValidPattern checks a place-rule name pattern.

Types

type Config

type Config struct {
	Check   bool
	Quiet   bool
	FmtOnly bool
	Sort    bool
	Diff    bool
	Staged  bool
	Dest    map[string]string
	Rules   []PlaceRule
}

Config carries the flags and placement configuration for one directory run.

func EffectiveConfig

func EffectiveConfig(base Config, rc *RepoConfig, cliDest map[string]string, cliRules []PlaceRule) Config

EffectiveConfig layers destinations and rules for one directory: built-in defaults < .tforg.hcl < -map flags.

type ConfigLoader

type ConfigLoader struct {
	// contains filtered or unexported fields
}

ConfigLoader finds the nearest .tforg.hcl for a directory, walking upward (like .gitignore discovery), with per-directory memoization.

func NewConfigLoader

func NewConfigLoader(disabled bool, explicitPath string) (*ConfigLoader, error)

func (*ConfigLoader) ForDir

func (l *ConfigLoader) ForDir(dir string) (*RepoConfig, error)

type DirOutcome

type DirOutcome struct {
	Dir     string
	Writes  map[string][]byte // base name -> new file content
	Creates map[string]bool   // subset of Writes that are new files
	Deletes map[string]bool   // files emptied by moves
	Origs   map[string][]byte // previous content of written/deleted files
	Moves   []MoveEvent
	FmtOnly []string // files changed by formatting alone
	Errs    []string
}

DirOutcome is the computed result for one directory. Nothing is written to disk until Apply runs.

func ProcessDir

func ProcessDir(dir string, bases []string, cfg Config) DirOutcome

ProcessDir computes the reorganization and formatting of the targeted files (base names, sorted) within a single directory. Blocks only ever move between files in the same directory, since a Terraform module is a directory.

func (*DirOutcome) Changed

func (o *DirOutcome) Changed() bool

type MoveEvent

type MoveEvent struct {
	From, Dest, Desc string
}

MoveEvent records one block relocation for reporting.

type PlaceRule

type PlaceRule struct {
	BlockType string
	Pattern   string // matched against the block's labels joined with "."
	File      string
	Keep      bool
}

PlaceRule pins blocks matching a type and name to a destination file (or to wherever they currently live, with Keep). Rules run before the type mapping; first match wins.

type RepoConfig

type RepoConfig struct {
	Dir    string   // directory the file was found in; Ignore patterns are relative to it
	Ignore []string // gitignore-style globs for files tforg must never touch
	// contains filtered or unexported fields
}

RepoConfig is the parsed content of a .tforg.hcl file.

Jump to

Keyboard shortcuts

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