-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathclean.go
More file actions
114 lines (96 loc) · 3.63 KB
/
clean.go
File metadata and controls
114 lines (96 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package terraform
import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
e "github.com/cloudposse/atmos/internal/exec"
cfg "github.com/cloudposse/atmos/pkg/config"
"github.com/cloudposse/atmos/pkg/flags"
"github.com/cloudposse/atmos/pkg/schema"
)
// cleanParser handles flag parsing for clean command.
var cleanParser *flags.StandardParser
// cleanCmd represents the terraform clean command (custom Atmos command).
var cleanCmd = &cobra.Command{
Use: "clean <component>",
Short: "Clean up Terraform state and artifacts",
Long: `Remove temporary files, state locks, and other artifacts created during Terraform operations.
This helps reset the environment and ensures no leftover data interferes with subsequent runs.
Common use cases:
- Releasing locks on Terraform state files.
- Cleaning up temporary workspaces or plans.
- Preparing the environment for a fresh deployment.`,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// Get component from args (optional - if empty, cleans all components).
var component string
if len(args) > 0 {
component = args[0]
}
// Use Viper to respect precedence (flag > env > config > default).
v := viper.GetViper()
// Bind terraform flags (--stack, etc.) to Viper.
if err := terraformParser.BindFlagsToViper(cmd, v); err != nil {
return err
}
// Bind clean-specific flags to Viper.
if err := cleanParser.BindFlagsToViper(cmd, v); err != nil {
return err
}
// Get flag values from Viper.
stack := v.GetString("stack")
force := v.GetBool("force")
everything := v.GetBool("everything")
skipLockFile := v.GetBool("skip-lock-file")
dryRun := v.GetBool("dry-run")
cache := v.GetBool("cache")
// Prompt for component/stack if neither is provided.
if component == "" && stack == "" {
prompted, err := promptForComponent(cmd)
if err == nil && prompted != "" {
component = prompted
}
prompted, err = promptForStack(cmd, component)
if err == nil && prompted != "" {
stack = prompted
}
}
// Initialize Atmos configuration.
atmosConfig, err := cfg.InitCliConfig(schema.ConfigAndStacksInfo{}, true)
if err != nil {
return err
}
opts := &e.CleanOptions{
Component: component,
Stack: stack,
Force: force,
Everything: everything,
SkipLockFile: skipLockFile,
DryRun: dryRun,
Cache: cache,
}
return e.ExecuteClean(opts, &atmosConfig)
},
}
func init() {
// Create parser with clean-specific flags using functional options.
cleanParser = flags.NewStandardParser(
flags.WithBoolFlag("everything", "", false, "If set atmos will also delete the Terraform state files and directories for the component"),
flags.WithBoolFlag("force", "f", false, "Forcefully delete Terraform state files and directories without interaction"),
flags.WithBoolFlag("skip-lock-file", "", false, "Skip deleting the `.terraform.lock.hcl` file"),
flags.WithBoolFlag("cache", "", false, "Clean Terraform plugin cache directory"),
flags.WithEnvVars("everything", "ATMOS_TERRAFORM_CLEAN_EVERYTHING"),
flags.WithEnvVars("force", "ATMOS_TERRAFORM_CLEAN_FORCE"),
flags.WithEnvVars("skip-lock-file", "ATMOS_TERRAFORM_CLEAN_SKIP_LOCK_FILE"),
flags.WithEnvVars("cache", "ATMOS_TERRAFORM_CLEAN_CACHE"),
)
// Register flags with the command as persistent flags.
cleanParser.RegisterPersistentFlags(cleanCmd)
// Bind flags to Viper for environment variable support.
if err := cleanParser.BindToViper(viper.GetViper()); err != nil {
panic(err)
}
// Register completions for cleanCmd.
RegisterTerraformCompletions(cleanCmd)
// Attach to parent terraform command.
terraformCmd.AddCommand(cleanCmd)
}