Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add skip lock file flag to clean command #417

Merged
merged 1 commit into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/exec/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func processHelp(componentType string, command string) error {
u.PrintMessage(" - 'atmos terraform apply' and 'atmos terraform deploy' commands commands support '--planfile' flag to specify the path " +
"to a planfile. The '--planfile' flag should be used instead of the planfile argument in the native 'terraform apply <planfile>' command")
u.PrintMessage(" - 'atmos terraform clean' command deletes the '.terraform' folder, '.terraform.lock.hcl' lock file, " +
"and the previously generated 'planfile' and 'varfile' for the specified component and stack")
"and the previously generated 'planfile' and 'varfile' for the specified component and stack. Use --skip-lock-file flag to skip deleting the lock file.")
u.PrintMessage(" - 'atmos terraform workspace' command first runs 'terraform init -reconfigure', then 'terraform workspace select', " +
"and if the workspace was not created before, it then runs 'terraform workspace new'")
u.PrintMessage(" - 'atmos terraform import' command searches for 'region' in the variables for the specified component and stack, " +
Expand Down
18 changes: 11 additions & 7 deletions internal/exec/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ package exec

import (
"fmt"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"os"
"path"
"strings"

"github.com/pkg/errors"
"github.com/spf13/cobra"

cfg "github.com/cloudposse/atmos/pkg/config"
"github.com/cloudposse/atmos/pkg/schema"
u "github.com/cloudposse/atmos/pkg/utils"
)

const (
autoApproveFlag = "-auto-approve"
outFlag = "-out"
varFileFlag = "-var-file"
autoApproveFlag = "-auto-approve"
outFlag = "-out"
varFileFlag = "-var-file"
skipTerraformLockFileFlag = "--skip-lock-file"
)

// ExecuteTerraformCmd parses the provided arguments and flags and executes terraform commands
Expand Down Expand Up @@ -81,8 +83,10 @@ func ExecuteTerraform(info schema.ConfigAndStacksInfo) error {
u.LogWarning(cliConfig, err.Error())
}

u.LogTrace(cliConfig, "Deleting '.terraform.lock.hcl' file")
_ = os.Remove(path.Join(componentPath, ".terraform.lock.hcl"))
if !u.SliceContainsString(info.AdditionalArgsAndFlags, skipTerraformLockFileFlag) {
u.LogTrace(cliConfig, "Deleting '.terraform.lock.hcl' file")
_ = os.Remove(path.Join(componentPath, ".terraform.lock.hcl"))
}

u.LogTrace(cliConfig, fmt.Sprintf("Deleting terraform varfile: %s\n", varFile))
_ = os.Remove(path.Join(componentPath, varFile))
Expand Down
14 changes: 8 additions & 6 deletions website/docs/cli/commands/terraform/terraform-clean.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ component in a stack.
Execute the `terraform clean` command like this:

```shell
atmos terraform clean <component> -s <stack>
atmos terraform clean <component> -s <stack> [--skip-lock-file]
```

:::tip
Expand All @@ -28,6 +28,7 @@ Run `atmos terraform clean --help` to see all the available options
```shell
atmos terraform clean top-level-component1 -s tenant1-ue2-dev
atmos terraform clean infra/vpc -s tenant1-ue2-staging
atmos terraform clean infra/vpc -s tenant1-ue2-staging --skip-lock-file
atmos terraform clean test/test-component -s tenant1-ue2-dev
atmos terraform clean test/test-component-override-2 -s tenant2-ue2-prod
atmos terraform clean test/test-component-override-3 -s tenant1-ue2-dev
Expand All @@ -36,12 +37,13 @@ atmos terraform clean test/test-component-override-3 -s tenant1-ue2-dev
## Arguments

| Argument | Description | Required |
|:------------|:--------------------------|:---------|
| :---------- | :------------------------ | :------- |
| `component` | Atmos terraform component | yes |

## Flags

| Flag | Description | Alias | Required |
|:------------|:------------|:------|:---------|
| `--stack` | Atmos stack | `-s` | yes |
| `--dry-run` | Dry run | | no |
| Flag | Description | Alias | Required |
| :----------------- | :-------------------------------- | :---- | :------- |
| `--stack` | Atmos stack | `-s` | yes |
| `--dry-run` | Dry run | | no |
| `--skip-lock-file` | Skip deleting .terraform.lock.hcl | | no |
19 changes: 10 additions & 9 deletions website/docs/cli/commands/terraform/usage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ sidebar_label: terraform
sidebar_class_name: command
---

import DocCardList from '@theme/DocCardList';
import DocCardList from "@theme/DocCardList";

:::note Purpose
Use these subcommands to interact with `terraform`.
Expand All @@ -19,15 +19,15 @@ atmos terraform <command> <component> -s <stack> [options]
atmos terraform <command> <component> --stack <stack> [options]
```

<br/>
<br />

:::info
Atmos supports all `terraform` commands and options described in [Terraform CLI reference](https://www.terraform.io/cli/commands).

In addition, the `component` argument and `stack` flag are required to generate variables and backend config for the component in the stack.
:::

<br/>
<br />

**Additions and differences from native `terraform`:**

Expand All @@ -53,7 +53,7 @@ In addition, the `component` argument and `stack` flag are required to generate
planfile

- `atmos terraform clean` command deletes the `.terraform` folder, `.terraform.lock.hcl` lock file, and the previously generated `planfile`
and `varfile` for the specified component and stack
and `varfile` for the specified component and stack. Use the `--skip-lock-file` flag to skip deleting the `.terraform.lock.hcl` file.

- `atmos terraform workspace` command first runs `terraform init -reconfigure`, then `terraform workspace select`, and if the workspace was not
created before, it then runs `terraform workspace new`
Expand All @@ -72,7 +72,7 @@ In addition, the `component` argument and `stack` flag are required to generate
- `atmos terraform shell` command configures an environment for an Atmos component in a stack and starts a new shell allowing executing all native
terraform commands inside the shell

<br/>
<br />

:::tip
Run `atmos terraform --help` to see all the available options
Expand All @@ -82,6 +82,7 @@ Run `atmos terraform --help` to see all the available options

```shell
atmos terraform plan test/test-component-override-3 -s tenant1-ue2-dev
atmos terraform plan test/test-component-override-3 -s tenant1-ue2-dev --skip-lock-file
atmos terraform plan test/test-component-override-2 -s tenant1-ue2-dev --redirect-stderr /dev/stdout
atmos terraform plan test/test-component-override -s tenant1-ue2-dev --redirect-stderr ./errors.txt

Expand All @@ -106,18 +107,18 @@ atmos terraform workspace test/test-component-override-3 -s tenant1-ue2-dev --re
## Arguments

| Argument | Description | Required |
|:------------|:--------------------------|:---------|
| :---------- | :------------------------ | :------- |
| `component` | Atmos terraform component | yes |

## Flags

| Flag | Description | Alias | Required |
|:--------------------|:----------------------------------------------------------------------------------------------------------------------------------------------|:------|:---------|
| :------------------ | :-------------------------------------------------------------------------------------------------------------------------------------------- | :---- | :------- |
| `--stack` | Atmos stack | `-s` | yes |
| `--dry-run` | Dry run | | no |
| `--redirect-stderr` | File descriptor to redirect `stderr` to.<br/>Errors can be redirected to any file or any standard file descriptor<br/>(including `/dev/null`) | | no |

<br/>
<br />

:::note

Expand All @@ -127,4 +128,4 @@ All native `terraform` flags are supported

## Subcommands

<DocCardList/>
<DocCardList />
Loading