add terraform clean --everything delete terraform.tfstate.d folder
What
- Added a new option to
terraform cleancalled--everythingandterraform cleancalled--forceto clean state files. This option deletes the Terraform-related folder and files, including:-
backend.tf.json -
.terraform -
terraform.tfstate.d -
.terraform.lock.hcl
-
The following scenarios are covered:
-
If no component is specified:
atmos terraform clean --everything delete with confim message yes/no atmos terraform clean --force delete forceCleans the state files for all components.
-
If a specific component is specified:
atmos terraform station clean --everythingCleans the state files for the specified component.
-
If both a component and a stack are specified:
atmos terraform clean --stack dev --everythingCleans the state files for the specified component and stack.
- Fixed a bug where the component name was being incorrectly parsed from the arguments. Previously, when the component was not provided in the command, it would incorrectly assign additional arguments as the component name (e.g., in
terraform clean --everything, the component name was mistakenly set to--everything). terraform clean --everything
terraform clean --force
Why
Cleaning state files is useful when running tests, but it should not be the default behavior to avoid unintended data loss.
References
https://linear.app/cloudposse/issue/DEV-346/atmos-terraform-clean-everything-should-clean-statefiles
Summary by CodeRabbit
-
New Features
- Enhanced help messages for
atmosCLI commands, clarifying theterraform cleancommand and its--everythingand--forceflags. - Introduced confirmation dialog functionality for user prompts.
- Added functionality to find folders by prefix and delete files and folders recursively.
- Enhanced help messages for
-
Bug Fixes
- Improved error handling in the command-line interface for scenarios with missing stacks.
- Refined argument processing to ensure correct identification of command-line options.
-
Documentation
- Updated help output to provide clearer instructions for users.
📝 Walkthrough
📝 Walkthrough
Walkthrough
This pull request introduces several changes to the CLI functionality of the atmos tool. Key enhancements include improved help messages for the terraform clean command, new constants for handling command flags, and modifications to the ExecuteTerraform function to better manage command-line arguments and flags. Additionally, several dependencies in the go.mod file have been added or updated to support these changes.
Changes
| File Path | Change Summary |
|---|---|
| internal/exec/help.go | Enhanced help messages for atmos terraform clean, detailing --everything and --force options. |
| internal/exec/path_utils.go | Replaced path package with filepath for improved file path handling. |
| internal/exec/terraform.go | Introduced constants everythingFlag and forceFlag, modified ExecuteTerraform to handle new command logic. |
| internal/exec/utils.go | Updated ProcessStacks and processArgsAndFlags for improved error handling and argument processing. |
| internal/exec/terraform_clean.go | Introduced functionality for managing Terraform-related directories and files, including search and deletion logic. |
| go.mod | Added new indirect dependencies and updated existing ones, including github.com/charmbracelet/huh and github.com/mitchellh/hashstructure/v2. |
Assessment against linked issues
| Objective | Addressed | Explanation |
|---|---|---|
atmos terraform clean --everything should clean statefiles (DEV-346) |
✅ |
Possibly related PRs
- #724: This PR modifies the help command structure, which is relevant to the changes made in the main PR that enhance help messages for CLI commands.
-
#729: This PR introduces new flags related to the
terraform cleancommand, which aligns with the main PR's updates to help messages for theatmos terraform cleancommand. -
#775: This PR includes a cleanup step for the Terraform workspace before executing commands, which complements the main PR's focus on improving the
terraformcommand functionality. -
#803: This PR discusses the management of the
ATMOS_SHLVLvariable in the shell context, which is relevant to the main PR's focus on improving the user experience in theatmos terraform shellcommand. - #818: This PR corrects documentation regarding the remote state backend, which is relevant to the main PR's focus on enhancing help messages and documentation for CLI commands.
Suggested reviewers
- osterman
- gberenice
📜 Recent review details
Configuration used: .coderabbit.yaml Review profile: CHILL
📥 Commits
Reviewing files that changed from the base of the PR and between 5e0c92f2a9da9acf1c32a9a5303d29d78fb4d219 and d7cfbbf6aa8d002dc266c8ea9c31b252d16e9189.
⛔ Files ignored due to path filters (1)
-
go.sumis excluded by!**/*.sum
📒 Files selected for processing (2)
-
go.mod(4 hunks) -
internal/exec/utils.go(2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- go.mod
🔇 Additional comments (2)
internal/exec/utils.go (2)
396-402: LGTM! The added condition improves flexibility.
The new condition allows the function to proceed without error when no stacks are found and checkStack is false. This enhancement supports operations like terraform clean that don't necessarily require a stack context.
1058-1071: LGTM! Enhanced argument validation improves robustness.
The added validation logic properly handles:
- Empty arguments
- Invalid option formats
- Differentiation between options and component names
This fixes the component name parsing bug mentioned in the PR objectives.
Let's verify the argument parsing behavior:
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?
🪧 Tips
Chat
There are 3 ways to chat with CodeRabbit:
- Review comments: Directly reply to a review comment made by CodeRabbit. Example:
-
I pushed a fix in commit <commit_id>, please review it. -
Generate unit testing code for this file. -
Open a follow-up GitHub issue for this discussion.
-
- Files and specific lines of code (under the "Files changed" tab): Tag
@coderabbitaiin a new review comment at the desired location with your query. Examples:-
@coderabbitai generate unit testing code for this file. -
@coderabbitai modularize this function.
-
- PR comments: Tag
@coderabbitaiin a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:-
@coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase. -
@coderabbitai read src/utils.ts and generate unit testing code. -
@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format. -
@coderabbitai help me debug CodeRabbit configuration file.
-
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.
CodeRabbit Commands (Invoked using PR comments)
-
@coderabbitai pauseto pause the reviews on a PR. -
@coderabbitai resumeto resume the paused reviews. -
@coderabbitai reviewto trigger an incremental review. This is useful when automatic reviews are disabled for the repository. -
@coderabbitai full reviewto do a full review from scratch and review all the files again. -
@coderabbitai summaryto regenerate the summary of the PR. -
@coderabbitai resolveresolve all the CodeRabbit review comments. -
@coderabbitai configurationto show the current CodeRabbit configuration for the repository. -
@coderabbitai helpto get help.
Other keywords and placeholders
- Add
@coderabbitai ignoreanywhere in the PR description to prevent this PR from being reviewed. - Add
@coderabbitai summaryor@auto-summaryto generate the high-level summary at a specific location in the PR description. - Add
@coderabbitaior@auto-titleanywhere in the PR title to generate the title automatically.
Documentation and Community
- Visit our Documentation for detailed information on how to use CodeRabbit.
- Join our Discord Community to get help, request features, and share feedback.
- Follow us on X/Twitter for updates and announcements.
Here's what we should aim to replicate with --everything
https://github.com/cloudposse/atmos/blob/main/demo/screengrabs/scripts/demo-stacks/.demo.rc#L17-L22
Of this, I think we're missing also the *.tfvar.json and *.tf.json files.
Also, if no component is passed, can we then process all components?
This would clean all components:
atmos terraform clean --everything
and this would clean the vpc component.
atmos terraform clean vpc --everything
Note, many stacks can share the same component.
Also, please make sure to update the related docs for atmos terraform clean
Here's what we should aim to replicate with
--everythinghttps://github.com/cloudposse/atmos/blob/main/demo/screengrabs/scripts/demo-stacks/.demo.rc#L17-L22
Of this, I think we're missing also the
*.tfvar.jsonand*.tf.jsonfiles.Also, if no component is passed, can we then process all components?
This would clean all components:
atmos terraform clean --everythingand this would clean the vpc component.
atmos terraform clean vpc --everythingNote, many stacks can share the same component.
Also, please make sure to update the related docs for
atmos terraform clean
The docs need to be updted. As a general rule, any new feature or updates to the existing feature should be reflected in the docs - this way we keep the code and the docs in sync and will. ot spend timelater trying to remember what was done or explain to other peopke howmto use it
@haitham911 let me know if you need help on updating the docs
Also, please post screenshots of this in action.
refacor code
Please output relative paths
@haitham911 please resolve conflicts
@haitham911 please resolve the conflicts
@haitham911 please resolve conflicts
@haitham911 please resolve the conflicts
@haitham911 please update the docs for atmos terraform clean
https://atmos.tools/cli/commands/terraform/clean
These changes were released in v1.114.0.