antonbabenko / pre-commit-terraform

pre-commit git hooks to take care of Terraform configurations 🇺🇦

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

feat: Add argument like `force-init` to do init before terraform validate

antonbabenko opened this issue · comments

Add an argument to terraform_validate to run terraform init before running terraform validate.

Originally posted by @eytanhanig in #203 (comment)

Notes fo contributors:


Additionally to force-init option, we can use terraform providers and run terraform init if necessary, automatically. See this comment for more details - #299 (comment)

Not needed anymore. The current realization is superior to that

If there are no deps for t validate - it will fail in a fraction of a second, so no need to "force-init"

# First try `terraform validate` with the hope that all deps are
# pre-installed. That is needed for cases when `.terraform/modules`
# or `.terraform/providers` missed AND that is expected.
terraform validate "${args[@]}" &> /dev/null && {
exit_code=$?
return $exit_code
}
# In case `terraform validate` failed to execute
# - check is simple `terraform init` will help
common::terraform_init 'terraform validate' "$dir_path" || {
exit_code=$?
return $exit_code
}
if [ "$retry_once_with_cleanup" != "true" ]; then
# terraform validate only
validate_output=$(terraform validate "${args[@]}" 2>&1)
exit_code=$?
else