From b52b475053c8b57d618aa6ce5758ec5919f95963 Mon Sep 17 00:00:00 2001 From: Maksym Vlasov Date: Fri, 25 Nov 2022 19:37:52 +0200 Subject: [PATCH] Apply suggestions from code review Co-authored-by: George L. Yermulnik --- README.md | 13 +++++++------ hooks/terraform_validate.sh | 4 ++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index d40797a90..8aad4b9c2 100644 --- a/README.md +++ b/README.md @@ -663,14 +663,15 @@ Example: - --hook-config=--retry-once-with-cleanup=true # Boolean. true or false ``` - > Note: That flag require additional installed dependency: `jq`. + > Note: The flag requires additional dependency to be installed: `jq`. If `--retry-once-with-cleanup=true`, then in each failed directory the cached modules and providers from the `.terraform` directory will be deleted, before retrying once more. To avoid unnecessary deletion of this directory, the cleanup and retry will only happen if Terraform produces any of the following error messages: - * Missing or corrupted provider plugins - * Module source has changed - * Module version requirements have changed - * Module not installed + * "Missing or corrupted provider plugins" + * "Module source has changed" + * "Module version requirements have changed" + * "Module not installed" + * "Could not load plugin" **Warning:** When using `--retry-once-with-cleanup=true`, problematic `.terraform/modules/` and `.terraform/providers/` directories will be recursively deleted without prompting for consent. Other files and directories will not be affected, such as the `.terraform/environment` file. @@ -690,7 +691,7 @@ Example: `terraform_validate` hook will try to reinitialize them before running the `terraform validate` command. - **Warning:** If you use Terraform workspaces, DO NOT use this option ([details](https://github.com/antonbabenko/pre-commit-terraform/issues/203#issuecomment-918791847)). Consider the first option, or wait to [`force-init`](https://github.com/antonbabenko/pre-commit-terraform/issues/224) option implementation. + **Warning:** If you use Terraform workspaces, DO NOT use this option ([details](https://github.com/antonbabenko/pre-commit-terraform/issues/203#issuecomment-918791847)). Consider the first option, or wait for [`force-init`](https://github.com/antonbabenko/pre-commit-terraform/issues/224) option implementation. 4. `terraform_validate` in a repo with Terraform module, written using Terraform 0.15+ and which uses provider `configuration_aliases` ([Provider Aliases Within Modules](https://www.terraform.io/language/modules/develop/providers#provider-aliases-within-modules)), errors out. diff --git a/hooks/terraform_validate.sh b/hooks/terraform_validate.sh index 6d048d032..37eee6262 100755 --- a/hooks/terraform_validate.sh +++ b/hooks/terraform_validate.sh @@ -120,8 +120,8 @@ function per_dir_hook_unique_part { validate_errors_matched=$? # Errors matched; Retry validation - if [ "$validate_errors_matched" = "1" ]; then - common::colorify "yellow" "Validation failed. Removing cached providers and modules from $dir_path/.terraform" + if [ "$validate_errors_matched" -eq 1 ]; then + common::colorify "yellow" "Validation failed. Removing cached providers and modules from \"$dir_path/.terraform\" directory" # `.terraform` dir may comprise some extra files, like `environment` # which stores info about current TF workspace, so we can't just remove # `.terraform` dir completely.