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

feat: Support set custom TF/OpenTofu binary. | If you use a custom Docker image build, please note that TERRAFORM_VERSION now must be provided #670

Merged
merged 36 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
c85dfd5
Allowing setting custom TF binary
den-is May 4, 2024
30674bc
README update
den-is May 4, 2024
af193f1
Update README.md
den-is May 9, 2024
43edcea
Update README.md
den-is May 9, 2024
78f7f10
Update hooks/_common.sh
den-is May 9, 2024
45706a8
Update hooks/_common.sh
den-is May 9, 2024
4018878
Update hooks/_common.sh
den-is May 9, 2024
c25e0fb
Update hooks/_common.sh
den-is May 9, 2024
7c448a6
Update hooks/_common.sh
den-is May 9, 2024
b1dd3bb
Update hooks/_common.sh
den-is May 9, 2024
2e353ba
refactoring
den-is May 9, 2024
8ca6f2d
update terraform_providers_lock.sh
den-is May 9, 2024
d3c85b4
README update
den-is May 9, 2024
c73c2e8
add globals doc for common::get_tf_path
den-is May 9, 2024
5301302
centralize TF_PATH initialization
den-is May 9, 2024
53146fe
fix expected hook-config arg
den-is May 9, 2024
a6fedca
fix command checks
den-is May 9, 2024
2f6f7b0
README update
den-is May 11, 2024
200b96b
README update
den-is May 11, 2024
5091208
README update
den-is May 11, 2024
3f94d4f
README update
den-is May 11, 2024
741aac6
README update
den-is May 11, 2024
128d4fa
README update
den-is May 11, 2024
c20b5fd
Apply suggestions from code review
MaxymVlasov May 21, 2024
e7deaaf
Apply suggestions from code review
MaxymVlasov May 21, 2024
e771a4f
Update README.md
den-is May 21, 2024
258234f
README update
den-is May 21, 2024
3660e75
Discard changes to README.md
MaxymVlasov May 21, 2024
6670e51
Revert "Discard changes to README.md"
MaxymVlasov May 21, 2024
825b7f0
Revert "README update"
MaxymVlasov May 21, 2024
bc2766c
Switch from global env to local in-function constant
MaxymVlasov May 21, 2024
168dd6a
fixup. Useless suppresions
MaxymVlasov May 21, 2024
80d239d
Add tofu to Docker, make terraform and tofu non-required deps
MaxymVlasov May 21, 2024
c7ad7d1
Add tofu to ISSUE template
MaxymVlasov May 21, 2024
e3c66bd
Apply suggestions from code review
MaxymVlasov May 22, 2024
1cb551e
Extract only need binary file
MaxymVlasov May 22, 2024
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
39 changes: 27 additions & 12 deletions README.md
den-is marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ If you are using `pre-commit-terraform` already or want to support its developme
* [Table of content](#table-of-content)
* [How to install](#how-to-install)
* [1. Install dependencies](#1-install-dependencies)
* [1.1 Custom Terraform binaries and OpenTofu support](#11-custom-terraform-binaries-and-opentofu-support)
* [2. Install the pre-commit hook globally](#2-install-the-pre-commit-hook-globally)
* [3. Add configs and hooks](#3-add-configs-and-hooks)
* [4. Run](#4-run)
Expand Down Expand Up @@ -67,7 +68,7 @@ If you are using `pre-commit-terraform` already or want to support its developme
### 1. Install dependencies

* [`pre-commit`](https://pre-commit.com/#install),
<sub><sup>[`terraform`](https://www.terraform.io/downloads.html),
den-is marked this conversation as resolved.
Show resolved Hide resolved
<sub><sup>[`terraform`](https://www.terraform.io/downloads.html) or [`opentofu`](https://opentofu.org/docs/intro/install/),
<sub><sup>[`git`](https://git-scm.com/downloads),
<sub><sup>[BASH `3.2.57` or newer](https://www.gnu.org/software/bash/#download),
<sub><sup>Internet connection (on first run),
Expand All @@ -77,17 +78,31 @@ If you are using `pre-commit-terraform` already or want to support its developme
<sub><sup>Some basic physical laws,
<sub><sup>Hope that it all will work.
</sup></sub></sup></sub></sup></sub></sup></sub></sup></sub></sup></sub></sup></sub></sup></sub></sup></sub><br><br>
* [`checkov`](https://github.com/bridgecrewio/checkov) required for `terraform_checkov` hook.
* [`terraform-docs`](https://github.com/terraform-docs/terraform-docs) required for `terraform_docs` hook.
* [`terragrunt`](https://terragrunt.gruntwork.io/docs/getting-started/install/) required for `terragrunt_validate` hook.
* [`terrascan`](https://github.com/tenable/terrascan) required for `terrascan` hook.
* [`TFLint`](https://github.com/terraform-linters/tflint) required for `terraform_tflint` hook.
* [`TFSec`](https://github.com/liamg/tfsec) required for `terraform_tfsec` hook.
* [`Trivy`](https://github.com/aquasecurity/trivy) required for `terraform_trivy` hook.
* [`infracost`](https://github.com/infracost/infracost) required for `infracost_breakdown` hook.
* [`jq`](https://github.com/stedolan/jq) required for `terraform_validate` with `--retry-once-with-cleanup` flag, and for `infracost_breakdown` hook.
* [`tfupdate`](https://github.com/minamijoyo/tfupdate) required for `tfupdate` hook.
* [`hcledit`](https://github.com/minamijoyo/hcledit) required for `terraform_wrapper_module_for_each` hook.
* [`checkov`](https://github.com/bridgecrewio/checkov) required for `terraform_checkov` hook
* [`terraform-docs`](https://github.com/terraform-docs/terraform-docs) required for `terraform_docs` hook
* [`terragrunt`](https://terragrunt.gruntwork.io/docs/getting-started/install/) required for `terragrunt_validate` hook
* [`terrascan`](https://github.com/tenable/terrascan) required for `terrascan` hook
* [`TFLint`](https://github.com/terraform-linters/tflint) required for `terraform_tflint` hook
* [`TFSec`](https://github.com/liamg/tfsec) required for `terraform_tfsec` hook
* [`Trivy`](https://github.com/aquasecurity/trivy) required for `terraform_trivy` hook
* [`infracost`](https://github.com/infracost/infracost) required for `infracost_breakdown` hook
* [`jq`](https://github.com/stedolan/jq) required for `terraform_validate` with `--retry-once-with-cleanup` flag, and for `infracost_breakdown` hook
* [`tfupdate`](https://github.com/minamijoyo/tfupdate) required for `tfupdate` hook
* [`hcledit`](https://github.com/minamijoyo/hcledit) required for `terraform_wrapper_module_for_each` hook


#### 1.1 Custom Terraform binaries and OpenTofu support

It is possible to set custom path to `terraform` binary.
This makes it possible to use [OpenTofu](https://opentofu.org) binary `tofu` instead of `terraform`.

How binary discovery works and how you can redefine it:
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved

1. Check if set per hook configuration `--hook-config=--tf-path=<path_to_binary_or_binary_name>`
2. Check if `PCT_TFPATH=<path_to_binary_or_binary_name>` environment variable exist
3. Check if `TERRAGRUNT_TFPATH=<path_to_binary_or_binary_name>` environment variable set
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
4. Check if `terraform` binary can be found in the user's $PATH
5. Check if `tofu` binary can be found in the user's $PATH

<details><summary><b>Docker</b></summary><br>

Expand Down
62 changes: 59 additions & 3 deletions hooks/_common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ function common::per_dir_hook {
# despite there's only one positional ARG left
local -a -r files=("$@")

local -r tf_path=$(common::get_tf_binary_path)

# check is (optional) function defined
if [ "$(type -t run_hook_on_whole_repo)" == function ] &&
# check is hook run via `pre-commit run --all`
Expand Down Expand Up @@ -383,7 +385,7 @@ function common::per_dir_hook {
pushd "$dir_path" > /dev/null
fi

per_dir_hook_unique_part "$dir_path" "$change_dir_in_unique_part" "$parallelism_disabled" "${args[@]}"
per_dir_hook_unique_part "$dir_path" "$change_dir_in_unique_part" "$parallelism_disabled" "$tf_path" "${args[@]}"
} &
pids+=("$!")

Expand Down Expand Up @@ -445,13 +447,66 @@ function common::colorify {
echo -e "${COLOR}${TEXT}${RESET}" >&2
}

#######################################################################
# Get Terraform/OpenTofu binary path
# Allows user to set the path to custom Terraform or OpenTofu binary
# Globals (init and populate):
# HOOK_CONFIG (array) arguments that configure hook behavior
# PCT_TFPATH (string) user defined env var with path to Terraform/OpenTofu binary
# TERRAGRUNT_TFPATH (string) user defined env var with path to Terraform/OpenTofu binary
# Outputs:
# If failed - exit 1 with error message about missing Terraform/OpenTofu binary
#######################################################################
function common::get_tf_binary_path {
local hook_config_tf_path

for config in "${HOOK_CONFIG[@]}"; do
if [[ $config == --tf-path=* ]]; then
hook_config_tf_path=${config#*=}
hook_config_tf_path=${hook_config_tf_path%;}
den-is marked this conversation as resolved.
Show resolved Hide resolved
break
fi
done

# direct hook config, has the highest precedence
if [[ $hook_config_tf_path ]]; then
echo "${hook_config_tf_path}"
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
return

# environment variable
elif [[ $PCT_TFPATH ]]; then
echo "${PCT_TFPATH}"
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
return

# Maybe there is a similar setting for Terragrunt already
elif [[ $TERRAGRUNT_TFPATH ]]; then
echo "${TERRAGRUNT_TFPATH}"
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
return
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get it

According to https://terragrunt.gruntwork.io/docs/reference/cli-options/#terragrunt-tfpath it will be automatically specified for terragrunt => for terragrunt hooks too.

Why do we need to set Terragrunt path for vanilla TF hooks?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imagine you are working in some environment that already uses TERRAGRUNT_TFPATH env variable (global env, using direnv, or any other method)
So if a variable already exists and serves exactly the same purposes, why not to check it and reuse it, why to have other args/vars/etc?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I'm following the gist of the question, though probably (I'm purely guesstimating) this way it would be easier for people using both TF and TG hooks to specify custom binary only once using TG's method but covering also TF? 🤔 And from this point of view it makes sense to me.

@den-is What's your idea?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I'm following the gist of the question, though probably (I'm purely guesstimating) this way it would be easier for people using both TF and TG hooks to specify custom binary only once using TG's method but covering also TF? 🤔 And from this point of view it makes sense to me.

@yermulnik Erm... this was already replied to by me yesterday.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Folks that use terragrunt should use terragrunt hooks, not terraform hooks - terraform will just not work in most cases. It's hard to imagine that someone uses both terragrunt and vanilla terraform, but if so - why do we think that folks would be happy with interference between "prj1 with TG and OpenTofu" and "prj2 with vanilla TF"?

And another question: do we need to specify $tf_path for terragrunt hooks?

Like terragrunt validate --terragrunt-tfpath "$tf_path" "${args[@]}"

terragrunt validate "${args[@]}"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Erm... this was already replied to by me yesterday.

@den-is My bad. I missed that comment (found it now). Thanks.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's hard to imagine that someone uses both terragrunt and vanilla terraform

I don't in particular, but a fellow squad in my project — does. And I'd imagine there are people in the wild who do the same and hence they may like using TERRAGRUNT_TFPATH to cover both use cases if they use pre-commit-terraform.

And another question: do we need to specify $tf_path for terragrunt hooks?

That's a good question. We probably should. But only when it is provided via TG hook config. In the rest of use cases TG should rely upon its own logic, including TERRAGRUNT_TFPATH and others described at https://terragrunt.gruntwork.io/docs/reference/cli-options/#terragrunt-tfpath

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had a conversation with @den-is
TL;DR: we assume that it will no break anything in 99% of cases. And if such a thing happened - I'll mention Den is such an issue :D

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me 👍🏻

I'll mention Den is such an issue :D

I feel like is should be in, but with is the sentence sounds fabulous 🤣


# check if Terraform binary is available
elif command -v terraform &> /dev/null; then
command -v terraform
return

# finally, check if Tofu binary is available
elif command -v tofu &> /dev/null; then
command -v tofu
return

else
common::colorify "red" "Neither Terraform nor OpenTofu binary could be found. Please either set the \"--tf-path\" hook configuration argument, or set the \"PCT_TFPATH\" environment variable, or set the \"TERRAGRUNT_TFPATH\" environment variable, or install Terraform or OpenTofu globally."
exit 1
yermulnik marked this conversation as resolved.
Show resolved Hide resolved
fi
}

#######################################################################
# Run terraform init command
# Arguments:
# command_name (string) command that will tun after successful init
# dir_path (string) PATH to dir relative to git repo root.
# Can be used in error logging
# parallelism_disabled (bool) if true - skip lock mechanism
# tf_path (string) PATH to Terraform/OpenTofu binary
# Globals (init and populate):
# TF_INIT_ARGS (array) arguments for `terraform init` command
# TF_PLUGIN_CACHE_DIR (string) user defined env var with name of the directory
Expand All @@ -464,6 +519,7 @@ function common::terraform_init {
local -r command_name=$1
local -r dir_path=$2
local -r parallelism_disabled=$3
local -r tf_path=$4

local exit_code=0
local init_output
Expand All @@ -480,13 +536,13 @@ function common::terraform_init {
# Plugin cache dir can't be written concurrently or read during write
# https://github.com/hashicorp/terraform/issues/31964
if [[ -z $TF_PLUGIN_CACHE_DIR || $parallelism_disabled == true ]]; then
init_output=$(terraform init -backend=false "${TF_INIT_ARGS[@]}" 2>&1)
init_output=$($tf_path init -backend=false "${TF_INIT_ARGS[@]}" 2>&1)
exit_code=$?
else
# Locking just doesn't work, and the below works quicker instead. Details:
# https://github.com/hashicorp/terraform/issues/31964#issuecomment-1939869453
for i in {1..10}; do
init_output=$(terraform init -backend=false "${TF_INIT_ARGS[@]}" 2>&1)
init_output=$($tf_path init -backend=false "${TF_INIT_ARGS[@]}" 2>&1)
exit_code=$?

if [ $exit_code -eq 0 ]; then
Expand Down
5 changes: 4 additions & 1 deletion hooks/terraform_checkov.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function main {
# Availability depends on hook.
# parallelism_disabled (bool) if true - skip lock mechanism
# args (array) arguments that configure wrapped tool behavior
# tf_path (string) PATH to Terraform/OpenTofu binary
# Outputs:
# If failed - print out hook checks status
#######################################################################
Expand All @@ -46,7 +47,9 @@ function per_dir_hook_unique_part {
local -r change_dir_in_unique_part="$2"
# shellcheck disable=SC2034 # Unused var.
local -r parallelism_disabled="$3"
shift 3
# shellcheck disable=SC2034 # Unused var.
local -r tf_path="$4"
shift 4
local -a -r args=("$@")

checkov -d . "${args[@]}"
Expand Down
6 changes: 4 additions & 2 deletions hooks/terraform_fmt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function main {
# Availability depends on hook.
# parallelism_disabled (bool) if true - skip lock mechanism
# args (array) arguments that configure wrapped tool behavior
# tf_path (string) PATH to Terraform/OpenTofu binary
# Outputs:
# If failed - print out hook checks status
#######################################################################
Expand All @@ -43,11 +44,12 @@ function per_dir_hook_unique_part {
local -r change_dir_in_unique_part="$2"
# shellcheck disable=SC2034 # Unused var.
local -r parallelism_disabled="$3"
shift 3
local -r tf_path="$4"
shift 4
local -a -r args=("$@")

# pass the arguments to hook
terraform fmt "${args[@]}"
$tf_path fmt "${args[@]}"

# return exit code to common::per_dir_hook
local exit_code=$?
Expand Down
10 changes: 7 additions & 3 deletions hooks/terraform_providers_lock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,19 @@ function lockfile_contains_all_needed_sha {
# Availability depends on hook.
# parallelism_disabled (bool) if true - skip lock mechanism
# args (array) arguments that configure wrapped tool behavior
# tf_path (string) PATH to Terraform/OpenTofu binary
# Outputs:
# If failed - print out hook checks status
#######################################################################
function per_dir_hook_unique_part {
# shellcheck disable=SC2034 # Unused var.
local -r dir_path="$1"
# shellcheck disable=SC2034 # Unused var.
local -r change_dir_in_unique_part="$2"
# shellcheck disable=SC2034 # Unused var.
local -r parallelism_disabled="$3"
shift 3
local -r tf_path="$4"
shift 4
local -a -r args=("$@")

local platforms_count=0
Expand Down Expand Up @@ -138,7 +142,7 @@ function per_dir_hook_unique_part {
common::colorify "yellow" "DEPRECATION NOTICE: We introduced '--mode' flag for this hook.
Check migration instructions at https://github.com/antonbabenko/pre-commit-terraform#terraform_providers_lock
"
common::terraform_init 'terraform providers lock' "$dir_path" "$parallelism_disabled" || {
common::terraform_init 'terraform providers lock' "$dir_path" "$parallelism_disabled" "$tf_path" || {
exit_code=$?
return $exit_code
}
Expand All @@ -153,7 +157,7 @@ Check migration instructions at https://github.com/antonbabenko/pre-commit-terra
#? Don't require `tf init` for providers, but required `tf init` for modules
#? Mitigated by `function match_validate_errors` from terraform_validate hook
# pass the arguments to hook
terraform providers lock "${args[@]}"
$tf_path providers lock "${args[@]}"

# return exit code to common::per_dir_hook
exit_code=$?
Expand Down
7 changes: 6 additions & 1 deletion hooks/terraform_tflint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,20 @@ function main {
# Availability depends on hook.
# parallelism_disabled (bool) if true - skip lock mechanism
# args (array) arguments that configure wrapped tool behavior
# tf_path (string) PATH to Terraform/OpenTofu binary
# Outputs:
# If failed - print out hook checks status
#######################################################################
function per_dir_hook_unique_part {
# shellcheck disable=SC2034 # Unused var.
local -r dir_path="$1"
# shellcheck disable=SC2034 # Unused var.
local -r change_dir_in_unique_part="$2"
# shellcheck disable=SC2034 # Unused var.
local -r parallelism_disabled="$3"
shift 3
# shellcheck disable=SC2034 # Unused var.
local -r tf_path="$4"
shift 4
local -a -r args=("$@")

if [ "$change_dir_in_unique_part" == "delegate_chdir" ]; then
Expand Down
5 changes: 4 additions & 1 deletion hooks/terraform_tfsec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function main {
# Availability depends on hook.
# parallelism_disabled (bool) if true - skip lock mechanism
# args (array) arguments that configure wrapped tool behavior
# tf_path (string) PATH to Terraform/OpenTofu binary
# Outputs:
# If failed - print out hook checks status
#######################################################################
Expand All @@ -49,7 +50,9 @@ function per_dir_hook_unique_part {
local -r change_dir_in_unique_part="$2"
# shellcheck disable=SC2034 # Unused var.
local -r parallelism_disabled="$3"
shift 3
# shellcheck disable=SC2034 # Unused var.
local -r tf_path="$4"
shift 4
local -a -r args=("$@")

# pass the arguments to hook
Expand Down
5 changes: 4 additions & 1 deletion hooks/terraform_trivy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function main {
# Availability depends on hook.
# parallelism_disabled (bool) if true - skip lock mechanism
# args (array) arguments that configure wrapped tool behavior
# tf_path (string) PATH to Terraform/OpenTofu binary
# Outputs:
# If failed - print out hook checks status
#######################################################################
Expand All @@ -41,7 +42,9 @@ function per_dir_hook_unique_part {
local -r change_dir_in_unique_part="$2"
# shellcheck disable=SC2034 # Unused var.
local -r parallelism_disabled="$3"
shift 3
# shellcheck disable=SC2034 # Unused var.
local -r tf_path="$4"
shift 4
local -a -r args=("$@")

# pass the arguments to hook
Expand Down
18 changes: 11 additions & 7 deletions hooks/terraform_validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,19 @@ function match_validate_errors {
# Availability depends on hook.
# parallelism_disabled (bool) if true - skip lock mechanism
# args (array) arguments that configure wrapped tool behavior
# tf_path (string) PATH to Terraform/OpenTofu binary
# Outputs:
# If failed - print out hook checks status
#######################################################################
function per_dir_hook_unique_part {
# shellcheck disable=SC2034 # Unused var.
local -r dir_path="$1"
# shellcheck disable=SC2034 # Unused var.
local -r change_dir_in_unique_part="$2"
# shellcheck disable=SC2034 # Unused var.
local -r parallelism_disabled="$3"
shift 3
local -r tf_path="$4"
shift 4
local -a -r args=("$@")

local exit_code
Expand Down Expand Up @@ -116,25 +120,25 @@ function per_dir_hook_unique_part {
# 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 && {
$tf_path 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" "$parallelism_disabled" || {
common::terraform_init "$tf_path validate" "$dir_path" "$parallelism_disabled" "$tf_path" || {
exit_code=$?
return $exit_code
}

if [ "$retry_once_with_cleanup" != "true" ]; then
# terraform validate only
validate_output=$(terraform validate "${args[@]}" 2>&1)
validate_output=$($tf_path validate "${args[@]}" 2>&1)
exit_code=$?
else
# terraform validate, plus capture possible errors
validate_output=$(terraform validate -json "${args[@]}" 2>&1)
validate_output=$($tf_path validate -json "${args[@]}" 2>&1)
exit_code=$?

# Match specific validation errors
Expand All @@ -152,12 +156,12 @@ function per_dir_hook_unique_part {

common::colorify "yellow" "Re-validating: $dir_path"

common::terraform_init 'terraform validate' "$dir_path" "$parallelism_disabled" || {
common::terraform_init "$tf_path validate" "$dir_path" "$parallelism_disabled" "$tf_path" || {
exit_code=$?
return $exit_code
}

validate_output=$(terraform validate "${args[@]}" 2>&1)
validate_output=$($tf_path validate "${args[@]}" 2>&1)
exit_code=$?
fi
fi
Expand Down
5 changes: 4 additions & 1 deletion hooks/terragrunt_fmt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function main {
# Availability depends on hook.
# parallelism_disabled (bool) if true - skip lock mechanism
# args (array) arguments that configure wrapped tool behavior
# tf_path (string) PATH to Terraform/OpenTofu binary
# Outputs:
# If failed - print out hook checks status
#######################################################################
Expand All @@ -39,7 +40,9 @@ function per_dir_hook_unique_part {
local -r change_dir_in_unique_part="$2"
# shellcheck disable=SC2034 # Unused var.
local -r parallelism_disabled="$3"
shift 3
# shellcheck disable=SC2034 # Unused var.
local -r tf_path="$4"
shift 4
local -a -r args=("$@")

# pass the arguments to hook
Expand Down
Loading
Loading