clean

package
v1.206.2 Latest Latest
Warning

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

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

Documentation

Overview

Package clean provides functionality to clean up Terraform state and artifacts.

Index

Constants

View Source
const (
	ForceFlag                 = "--force"
	EverythingFlag            = "--everything"
	SkipTerraformLockFileFlag = "--skip-lock-file"
)

Flags for clean options.

View Source
const (
	// TerraformDir is the standard Terraform working directory.
	TerraformDir = ".terraform"
	// TerraformLockFile is the dependency lock file.
	TerraformLockFile = ".terraform.lock.hcl"
	// TerraformVarFileGlob is the glob pattern for generated variable files.
	TerraformVarFileGlob = "*.tfvars.json"
	// TerraformStateDir is the directory for local state files.
	TerraformStateDir = "terraform.tfstate.d"
	// BackendConfigFile is the auto-generated backend configuration file.
	BackendConfigFile = "backend.tf.json"
)

File and directory patterns for Terraform artifacts.

View Source
const EnvTFDataDir = "TF_DATA_DIR"

EnvTFDataDir is the environment variable name for TF_DATA_DIR.

Variables

View Source
var (
	ErrParseTerraformComponents  = errors.New("could not parse Terraform components")
	ErrParseComponentsAttributes = errors.New("could not parse component attributes")
	ErrDescribeStack             = errors.New("error describing stacks")
	ErrEmptyPath                 = errors.New("path cannot be empty")
	ErrPathNotExist              = errors.New("path does not exist")
	ErrFileStat                  = errors.New("error getting file stat")
	ErrMatchPattern              = errors.New("error matching pattern")
	ErrReadDir                   = errors.New("error reading directory")
	ErrFailedFoundStack          = errors.New("failed to find stack folders")
	ErrCollectFiles              = errors.New("failed to collect files")
	ErrEmptyEnvDir               = errors.New("ENV TF_DATA_DIR is empty")
	ErrResolveEnvDir             = errors.New("error resolving TF_DATA_DIR path")
	ErrRefusingToDeleteDir       = errors.New("refusing to delete root directory")
	ErrRefusingToDelete          = errors.New("refusing to delete directory containing")
	ErrRootPath                  = errors.New("root path cannot be empty")
	ErrComponentNotFound         = errors.New("could not find component")
	ErrRelPath                   = errors.New("error getting relative path")
)

Error variables for the clean package.

Functions

func DeletePath

func DeletePath(fullPath string, objectName string) error

DeletePath deletes the specified file or folder with a checkmark or xmark.

func FindFoldersNamesWithPrefix

func FindFoldersNamesWithPrefix(root, prefix string) ([]string, error)

FindFoldersNamesWithPrefix finds the names of folders that match the given prefix under the specified root path. The search is performed at the root level (level 1) and one level deeper (level 2).

func GetAllStacksComponentsPaths

func GetAllStacksComponentsPaths(stacksMap map[string]any) []string

GetAllStacksComponentsPaths retrieves all component relative paths to base Terraform directory from the stacks map. It deduplicates paths to avoid processing the same component multiple times when multiple stacks reference it.

func IsValidDataDir

func IsValidDataDir(tfDataDir string) error

IsValidDataDir validates the TF_DATA_DIR environment variable value.

Types

type Directory

type Directory struct {
	// Name is the base name of the directory.
	Name string
	// FullPath is the absolute path to the directory.
	FullPath string
	// RelativePath is the path relative to the component directory.
	RelativePath string
	// Files contains the list of files and subdirectories within this directory.
	Files []ObjectInfo
}

Directory represents a directory discovered during clean operations along with its contained files.

func CollectComponentsDirectoryObjects

func CollectComponentsDirectoryObjects(terraformDirAbsolutePath string, allComponentsRelativePaths []string, patterns []string) ([]Directory, error)

CollectComponentsDirectoryObjects collects files matching patterns for multiple component paths. It handles deduplication of component paths to avoid processing the same component multiple times.

func CollectDirectoryObjects

func CollectDirectoryObjects(basePath string, patterns []string) ([]Directory, error)

CollectDirectoryObjects collects files matching patterns in the given base path.

func GetStackTerraformStateFolder

func GetStackTerraformStateFolder(componentPath string, stack string) ([]Directory, error)

GetStackTerraformStateFolder gets stack terraform state files.

type ExecAdapter

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

ExecAdapter adapts internal/exec functions to the StackProcessor interface.

func NewExecAdapter

func NewExecAdapter(
	processStacks ExecProcessStacks,
	executeDescribeStacks ExecDescribeStacks,
	getGenerateFilenames ExecGetGenerateFilenames,
	collectComponentsDirectoryObjs ExecCollectComponentsDirectoryObjects,
	constructVarfileName ExecConstructVarfileName,
	constructPlanfileName ExecConstructPlanfileName,
	getAllStacksComponentsPaths ExecGetAllStacksComponentsPaths,
) *ExecAdapter

NewExecAdapter creates a new adapter that wraps internal/exec functions.

func (*ExecAdapter) CollectComponentsDirectoryObjects

func (a *ExecAdapter) CollectComponentsDirectoryObjects(basePath string, componentPaths []string, patterns []string) ([]Directory, error)

CollectComponentsDirectoryObjects implements StackProcessor.CollectComponentsDirectoryObjects.

func (*ExecAdapter) ConstructTerraformComponentPlanfileName

func (a *ExecAdapter) ConstructTerraformComponentPlanfileName(info *schema.ConfigAndStacksInfo) string

ConstructTerraformComponentPlanfileName implements StackProcessor.ConstructTerraformComponentPlanfileName.

func (*ExecAdapter) ConstructTerraformComponentVarfileName

func (a *ExecAdapter) ConstructTerraformComponentVarfileName(info *schema.ConfigAndStacksInfo) string

ConstructTerraformComponentVarfileName implements StackProcessor.ConstructTerraformComponentVarfileName.

func (*ExecAdapter) ExecuteDescribeStacks

func (a *ExecAdapter) ExecuteDescribeStacks(
	atmosConfig *schema.AtmosConfiguration,
	filterByStack string,
	components []string,
) (map[string]any, error)

ExecuteDescribeStacks implements StackProcessor.ExecuteDescribeStacks.

func (*ExecAdapter) GetAllStacksComponentsPaths

func (a *ExecAdapter) GetAllStacksComponentsPaths(stacksMap map[string]any) []string

GetAllStacksComponentsPaths implements StackProcessor.GetAllStacksComponentsPaths.

func (*ExecAdapter) GetGenerateFilenamesForComponent

func (a *ExecAdapter) GetGenerateFilenamesForComponent(componentSection map[string]any) []string

GetGenerateFilenamesForComponent implements StackProcessor.GetGenerateFilenamesForComponent.

func (*ExecAdapter) ProcessStacks

ProcessStacks implements StackProcessor.ProcessStacks.

type ExecCollectComponentsDirectoryObjects

type ExecCollectComponentsDirectoryObjects func(basePath string, componentPaths []string, patterns []string) ([]Directory, error)

ExecCollectComponentsDirectoryObjects is a function type for CollectComponentsDirectoryObjects.

type ExecConstructPlanfileName

type ExecConstructPlanfileName func(info *schema.ConfigAndStacksInfo) string

ExecConstructPlanfileName is a function type for constructTerraformComponentPlanfileName.

type ExecConstructVarfileName

type ExecConstructVarfileName func(info *schema.ConfigAndStacksInfo) string

ExecConstructVarfileName is a function type for constructTerraformComponentVarfileName.

type ExecDescribeStacks

type ExecDescribeStacks func(
	atmosConfig *schema.AtmosConfiguration,
	filterByStack string,
	components []string,
) (map[string]any, error)

ExecDescribeStacks is a function type for ExecuteDescribeStacks from internal/exec.

type ExecGetAllStacksComponentsPaths

type ExecGetAllStacksComponentsPaths func(stacksMap map[string]any) []string

ExecGetAllStacksComponentsPaths is a function type for getAllStacksComponentsPaths.

type ExecGetGenerateFilenames

type ExecGetGenerateFilenames func(componentSection map[string]any) []string

ExecGetGenerateFilenames is a function type for GetGenerateFilenamesForComponent.

type ExecProcessStacks

type ExecProcessStacks func(atmosConfig *schema.AtmosConfiguration, info schema.ConfigAndStacksInfo) (schema.ConfigAndStacksInfo, error)

ExecProcessStacks is a function type for ProcessStacks from internal/exec.

type ObjectInfo

type ObjectInfo struct {
	// FullPath is the absolute path to the object.
	FullPath string
	// RelativePath is the path relative to the component directory.
	RelativePath string
	// Name is the base name of the object.
	Name string
	// IsDir indicates whether the object is a directory.
	IsDir bool
}

ObjectInfo contains information about a file or directory object discovered during clean operations.

type Options

type Options struct {
	Component    string
	Stack        string
	Force        bool
	Everything   bool
	SkipLockFile bool
	DryRun       bool
	Cache        bool
}

Options holds options for the clean operation.

type Service

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

Service provides clean operations for Terraform components.

func NewService

func NewService(processor StackProcessor) *Service

NewService creates a new clean service with the given stack processor.

func (*Service) Execute

func (s *Service) Execute(opts *Options, atmosConfig *schema.AtmosConfiguration) error

Execute cleans up Terraform state and artifacts for a component.

func (*Service) HandleSubCommand

func (s *Service) HandleSubCommand(info *schema.ConfigAndStacksInfo, componentPath string, atmosConfig *schema.AtmosConfiguration) error

HandleSubCommand handles the 'clean' subcommand logic.

type StackProcessor

type StackProcessor interface {
	// ProcessStacks processes stacks and returns component configuration.
	ProcessStacks(atmosConfig *schema.AtmosConfiguration, info schema.ConfigAndStacksInfo) (schema.ConfigAndStacksInfo, error)

	// ExecuteDescribeStacks describes stacks for the clean operation.
	ExecuteDescribeStacks(
		atmosConfig *schema.AtmosConfiguration,
		filterByStack string,
		components []string,
	) (map[string]any, error)

	// GetGenerateFilenamesForComponent returns auto-generated filenames for a component.
	GetGenerateFilenamesForComponent(componentSection map[string]any) []string

	// CollectComponentsDirectoryObjects collects directory objects for components.
	CollectComponentsDirectoryObjects(basePath string, componentPaths []string, patterns []string) ([]Directory, error)

	// ConstructTerraformComponentVarfileName constructs the varfile name.
	ConstructTerraformComponentVarfileName(info *schema.ConfigAndStacksInfo) string

	// ConstructTerraformComponentPlanfileName constructs the planfile name.
	ConstructTerraformComponentPlanfileName(info *schema.ConfigAndStacksInfo) string

	// GetAllStacksComponentsPaths returns all component paths from stacks.
	GetAllStacksComponentsPaths(stacksMap map[string]any) []string
}

StackProcessor defines the interface for stack operations needed by clean.

Jump to

Keyboard shortcuts

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