atmos icon indicating copy to clipboard operation
atmos copied to clipboard

add terraform clean --everything delete terraform.tfstate.d folder

Open haitham911 opened this issue 1 year ago • 4 comments

What

  • Added a new option to terraform clean called --everything and terraform clean called --force to 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:

  1. If no component is specified:

    atmos terraform clean --everything delete with confim message yes/no
     atmos terraform clean --force delete force
    
    

    Cleans the state files for all components.

  2. If a specific component is specified:

    atmos terraform station clean --everything
    

    Cleans the state files for the specified component.

  3. If both a component and a stack are specified:

    atmos terraform clean --stack dev --everything
    

    Cleans 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 2024-10-24 02_32_59-Window

terraform clean --force 2024-10-24 02_33_36-Window

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 atmos CLI commands, clarifying the terraform clean command and its --everything and --force flags.
    • Introduced confirmation dialog functionality for user prompts.
    • Added functionality to find folders by prefix and delete files and folders recursively.
  • 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.

haitham911 avatar Oct 15 '24 23:10 haitham911

📝 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 clean command, which aligns with the main PR's updates to help messages for the atmos terraform clean command.
  • #775: This PR includes a cleanup step for the Terraform workspace before executing commands, which complements the main PR's focus on improving the terraform command functionality.
  • #803: This PR discusses the management of the ATMOS_SHLVL variable in the shell context, which is relevant to the main PR's focus on improving the user experience in the atmos terraform shell command.
  • #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.sum is 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:

  1. Empty arguments
  2. Invalid option formats
  3. 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?

❤️ Share
🪧 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 @coderabbitai in 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 @coderabbitai in 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 pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary or @auto-summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @auto-title anywhere 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.

coderabbitai[bot] avatar Oct 15 '24 23:10 coderabbitai[bot]

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

osterman avatar Oct 16 '24 01:10 osterman

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

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

aknysh avatar Oct 16 '24 05:10 aknysh

Also, please post screenshots of this in action.

osterman avatar Oct 21 '24 17:10 osterman

refacor code

haitham911 avatar Oct 24 '24 12:10 haitham911

Please output relative paths

osterman avatar Oct 24 '24 13:10 osterman

@haitham911 please resolve conflicts

osterman avatar Oct 30 '24 13:10 osterman

@haitham911 please resolve the conflicts

aknysh avatar Nov 01 '24 02:11 aknysh

@haitham911 please resolve conflicts

osterman avatar Nov 11 '24 15:11 osterman

@haitham911 please resolve the conflicts

aknysh avatar Nov 11 '24 23:11 aknysh

@haitham911 please update the docs for atmos terraform clean

https://atmos.tools/cli/commands/terraform/clean

osterman avatar Nov 14 '24 14:11 osterman

These changes were released in v1.114.0.

github-actions[bot] avatar Dec 05 '24 02:12 github-actions[bot]