generator

package
v1.219.0 Latest Latest
Warning

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

Go to latest
Published: May 19, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package generator provides sentinel error aliases from the central errors package. These are re-exported for backward compatibility and convenience.

Package generator provides a unified interface for generating Terraform configuration files. This includes varfiles, provider overrides, required_providers blocks, and backend configuration.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrGeneratorNotFound is returned when a requested generator is not in the registry.
	ErrGeneratorNotFound = errUtils.ErrGeneratorNotFound

	// ErrInvalidContext is returned when the generator context is missing required data.
	ErrInvalidContext = errUtils.ErrInvalidGeneratorCtx

	// ErrValidationFailed is returned when generator validation fails.
	ErrValidationFailed = errUtils.ErrGeneratorValidation

	// ErrGenerationFailed is returned when generation fails.
	ErrGenerationFailed = errUtils.ErrGenerationFailed

	// ErrWriteFailed is returned when writing the generated file fails.
	ErrWriteFailed = errUtils.ErrGeneratorWriteFailed

	// ErrMissingWorkingDir is returned when WorkingDir is not set.
	ErrMissingWorkingDir = errUtils.ErrMissingWorkingDir

	// ErrMissingComponent is returned when Component is not set.
	ErrMissingComponent = errUtils.ErrComponentEmpty

	// ErrMissingStack is returned when Stack is not set.
	ErrMissingStack = errUtils.ErrStackEmpty

	// ErrMissingProviderSource is returned when a required_provider is missing the source field.
	ErrMissingProviderSource = errUtils.ErrMissingProviderSource
)

Re-exported errors from the central errors package. These provide the generator-specific errors for use within this package.

Functions

func ApplyOptions

func ApplyOptions(ctx *GeneratorContext, opts ...Option)

ApplyOptions applies functional options to a GeneratorContext.

func Generate

func Generate(ctx context.Context, name string, genCtx *GeneratorContext, writer Writer) error

Generate runs a specific generator by name.

func GenerateAll

func GenerateAll(ctx context.Context, genCtx *GeneratorContext, writer Writer) error

GenerateAll runs all registered generators that should generate.

func Register

func Register(gen Generator)

Register adds a generator to the registry. This is typically called from a generator's init() function.

Types

type FileWriter

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

FileWriter is the production implementation that writes to the filesystem.

func NewFileWriter

func NewFileWriter(opts ...WriterOption) *FileWriter

NewFileWriter creates a new file writer with optional configuration.

func (*FileWriter) WriteHCL

func (w *FileWriter) WriteHCL(dir, filename string, data map[string]any) error

WriteHCL writes content as HCL to the specified directory and filename.

func (*FileWriter) WriteJSON

func (w *FileWriter) WriteJSON(dir, filename string, data map[string]any) error

WriteJSON writes content as JSON to the specified directory and filename.

type Format

type Format string

Format represents the output format for generated files.

const (
	// FormatJSON generates JSON output (.tf.json files).
	FormatJSON Format = "json"
	// FormatHCL generates HCL output (.tf files).
	FormatHCL Format = "hcl"
)

type Generator

type Generator interface {
	// Name returns the unique identifier for this generator.
	Name() string

	// Generate produces the Terraform configuration content.
	// Returns a map structure suitable for JSON/HCL serialization.
	Generate(ctx context.Context, genCtx *GeneratorContext) (map[string]any, error)

	// Validate checks if the generator context has sufficient data.
	Validate(genCtx *GeneratorContext) error

	// DefaultFilename returns the default output filename.
	DefaultFilename() string

	// ShouldGenerate returns true if this generator should run.
	// Based on whether relevant config exists.
	ShouldGenerate(genCtx *GeneratorContext) bool
}

Generator is the interface for Terraform file generators.

type GeneratorContext

type GeneratorContext struct {
	// AtmosConfig holds the Atmos configuration.
	AtmosConfig *schema.AtmosConfiguration

	// StackInfo holds the processed stack and component information.
	StackInfo *schema.ConfigAndStacksInfo

	// Component is the component name.
	Component string

	// Stack is the stack name.
	Stack string

	// ComponentPath is the path to the component directory.
	ComponentPath string

	// WorkingDir is the directory where generated files will be written.
	WorkingDir string

	// VarsSection contains the component variables.
	VarsSection map[string]any

	// ProvidersSection contains the provider configuration.
	ProvidersSection map[string]any

	// RequiredVersion is the Terraform version constraint (e.g., ">= 1.10.1").
	RequiredVersion string

	// RequiredProviders maps provider names to their configuration.
	// Example: {"aws": {"source": "hashicorp/aws", "version": "~> 5.0"}}.
	RequiredProviders map[string]map[string]any

	// BackendType is the Terraform backend type (e.g., "s3", "gcs").
	BackendType string

	// BackendConfig contains the backend configuration.
	BackendConfig map[string]any

	// DryRun when true, prevents file writes.
	DryRun bool

	// Format specifies the output format (JSON or HCL).
	Format Format

	// CustomFilename overrides the default filename when set.
	CustomFilename string
}

GeneratorContext provides component and stack context to generators.

func NewGeneratorContext

func NewGeneratorContext(
	atmosConfig *schema.AtmosConfiguration,
	info *schema.ConfigAndStacksInfo,
	workingDir string,
) *GeneratorContext

NewGeneratorContext creates a GeneratorContext from ConfigAndStacksInfo.

func NewGeneratorContextWithOptions

func NewGeneratorContextWithOptions(
	atmosConfig *schema.AtmosConfiguration,
	info *schema.ConfigAndStacksInfo,
	workingDir string,
	opts ...Option,
) *GeneratorContext

NewGeneratorContextWithOptions creates a GeneratorContext with functional options.

type GeneratorRegistry

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

GeneratorRegistry manages generator registration and execution.

func GetRegistry

func GetRegistry() *GeneratorRegistry

GetRegistry returns the global generator registry singleton.

func (*GeneratorRegistry) Get

func (r *GeneratorRegistry) Get(name string) (Generator, error)

Get retrieves a generator by name.

func (*GeneratorRegistry) List

func (r *GeneratorRegistry) List() []string

List returns all registered generator names in sorted order.

type MockWriter

type MockWriter struct {
	// Written maps file paths to their written content.
	Written map[string]map[string]any
	// WriteErr if set, will be returned by Write operations.
	WriteErr error
}

MockWriter is a test implementation that captures written content.

func NewMockWriter

func NewMockWriter() *MockWriter

NewMockWriter creates a new mock writer for testing.

func (*MockWriter) Clear

func (w *MockWriter) Clear()

Clear resets the mock writer state.

func (*MockWriter) GetWritten

func (w *MockWriter) GetWritten(dir, filename string) (map[string]any, bool)

GetWritten returns the content written to a specific path.

func (*MockWriter) WriteHCL

func (w *MockWriter) WriteHCL(dir, filename string, data map[string]any) error

WriteHCL captures the content without writing to disk.

func (*MockWriter) WriteJSON

func (w *MockWriter) WriteJSON(dir, filename string, data map[string]any) error

WriteJSON captures the content without writing to disk.

type Option

type Option func(*GeneratorContext)

Option is a functional option for configuring generators.

func WithDryRun

func WithDryRun(dryRun bool) Option

WithDryRun enables dry-run mode (no file writes).

func WithFormat

func WithFormat(format Format) Option

WithFormat sets the output format.

func WithWorkingDir

func WithWorkingDir(dir string) Option

WithWorkingDir sets the working directory for output files.

type Writer

type Writer interface {
	// WriteJSON writes content as JSON to the specified directory and filename.
	WriteJSON(dir, filename string, data map[string]any) error
	// WriteHCL writes content as HCL to the specified directory and filename.
	WriteHCL(dir, filename string, data map[string]any) error
}

Writer handles file output for generators.

type WriterOption

type WriterOption func(*FileWriter)

WriterOption is a functional option for configuring the file writer.

func WithFileMode

func WithFileMode(mode os.FileMode) WriterOption

WithFileMode sets the file mode for written files.

Directories

Path Synopsis
Package providers provides a generator for Terraform provider override files.
Package providers provides a generator for Terraform provider override files.
Package required_providers provides a generator for Terraform required_providers blocks.
Package required_providers provides a generator for Terraform required_providers blocks.
Package varfile provides a generator for Terraform variable files (.tfvars.json).
Package varfile provides a generator for Terraform variable files (.tfvars.json).

Jump to

Keyboard shortcuts

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