Skip to content

Commit 1e9debc

Browse files
lextonMaxymVlasov
andauthored
feat: TFLint: Add --hook-config=--delegate-chdir to use tflint -chdir (antonbabenko#512)
Co-authored-by: Maksym Vlasov <[email protected]>
1 parent 1431664 commit 1e9debc

12 files changed

+101
-16
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,14 @@ To replicate functionality in `terraform_docs` hook:
604604
- --args=--config=__GIT_WORKING_DIR__/.tflint.hcl
605605
```
606606

607+
3. By default pre-commit-terraform performs directory switching into the terraform modules for you. If you want to delgate the directory changing to the binary - this will allow tflint to determine the full paths for error/warning messages, rather than just module relative paths. *Note: this requires `tflint>=0.44.0`.* For example:
608+
609+
```yaml
610+
- id: terraform_tflint
611+
args:
612+
- --hook-config=--delegate-chdir
613+
```
614+
607615

608616
### terraform_tfsec
609617

hooks/_common.sh

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,25 @@ function common::per_dir_hook {
217217
((index += 1))
218218
done
219219

220+
# Lookup hook-config for modifiers that impact common behavior
221+
local change_dir_in_unique_part=false
222+
IFS=";" read -r -a configs <<< "${HOOK_CONFIG[*]}"
223+
for c in "${configs[@]}"; do
224+
IFS="=" read -r -a config <<< "$c"
225+
key=${config[0]}
226+
value=${config[1]}
227+
228+
case $key in
229+
--delegate-chdir)
230+
# this flag will skip pushing and popping directories
231+
# delegating the responsibility to the hooked plugin/binary
232+
if [[ ! $value || $value == true ]]; then
233+
change_dir_in_unique_part="delegate_chdir"
234+
fi
235+
;;
236+
esac
237+
done
238+
220239
# preserve errexit status
221240
shopt -qo errexit && ERREXIT_IS_SET=true
222241
# allow hook to continue if exit_code is greater than 0
@@ -226,16 +245,22 @@ function common::per_dir_hook {
226245
# run hook for each path
227246
for dir_path in $(echo "${dir_paths[*]}" | tr ' ' '\n' | sort -u); do
228247
dir_path="${dir_path//__REPLACED__SPACE__/ }"
229-
pushd "$dir_path" > /dev/null || continue
230248

231-
per_dir_hook_unique_part "$dir_path" "${args[@]}"
249+
if [[ $change_dir_in_unique_part == false ]]; then
250+
pushd "$dir_path" > /dev/null || continue
251+
fi
252+
253+
per_dir_hook_unique_part "$dir_path" "$change_dir_in_unique_part" "${args[@]}"
232254

233255
local exit_code=$?
234256
if [ $exit_code -ne 0 ]; then
235257
final_exit_code=$exit_code
236258
fi
237259

238-
popd > /dev/null
260+
if [[ $change_dir_in_unique_part == false ]]; then
261+
popd > /dev/null
262+
fi
263+
239264
done
240265

241266
# restore errexit if it was set before the "for" loop

hooks/terraform_checkov.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,19 @@ function main {
3131
# Arguments:
3232
# dir_path (string) PATH to dir relative to git repo root.
3333
# Can be used in error logging
34+
# change_dir_in_unique_part (string/false) Modifier which creates
35+
# possibilities to use non-common chdir strategies.
36+
# Availability depends on hook.
3437
# args (array) arguments that configure wrapped tool behavior
3538
# Outputs:
3639
# If failed - print out hook checks status
3740
#######################################################################
3841
function per_dir_hook_unique_part {
3942
# shellcheck disable=SC2034 # Unused var.
4043
local -r dir_path="$1"
41-
shift
44+
# shellcheck disable=SC2034 # Unused var.
45+
local -r change_dir_in_unique_part="$2"
46+
shift 2
4247
local -a -r args=("$@")
4348

4449
checkov -d . "${args[@]}"

hooks/terraform_fmt.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,19 @@ function main {
2828
# Arguments:
2929
# dir_path (string) PATH to dir relative to git repo root.
3030
# Can be used in error logging
31+
# change_dir_in_unique_part (string/false) Modifier which creates
32+
# possibilities to use non-common chdir strategies.
33+
# Availability depends on hook.
3134
# args (array) arguments that configure wrapped tool behavior
3235
# Outputs:
3336
# If failed - print out hook checks status
3437
#######################################################################
3538
function per_dir_hook_unique_part {
3639
# shellcheck disable=SC2034 # Unused var.
3740
local -r dir_path="$1"
38-
shift
41+
# shellcheck disable=SC2034 # Unused var.
42+
local -r change_dir_in_unique_part="$2"
43+
shift 2
3944
local -a -r args=("$@")
4045

4146
# pass the arguments to hook

hooks/terraform_providers_lock.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ function main {
2525
# Arguments:
2626
# dir_path (string) PATH to dir relative to git repo root.
2727
# Can be used in error logging
28+
# change_dir_in_unique_part (string/false) Modifier which creates
29+
# possibilities to use non-common chdir strategies.
30+
# Availability depends on hook.
2831
# args (array) arguments that configure wrapped tool behavior
2932
# Outputs:
3033
# If failed - print out hook checks status

hooks/terraform_tflint.sh

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function main {
2828
} || {
2929
local exit_code=$?
3030
common::colorify "red" "Command 'tflint --init' failed:"
31-
echo "${TFLINT_INIT}"
31+
echo -e "${TFLINT_INIT}"
3232
return ${exit_code}
3333
}
3434

@@ -41,21 +41,30 @@ function main {
4141
# Arguments:
4242
# dir_path (string) PATH to dir relative to git repo root.
4343
# Can be used in error logging
44+
# change_dir_in_unique_part (string/false) Modifier which creates
45+
# possibilities to use non-common chdir strategies.
46+
# Availability depends on hook.
4447
# args (array) arguments that configure wrapped tool behavior
4548
# Outputs:
4649
# If failed - print out hook checks status
4750
#######################################################################
4851
function per_dir_hook_unique_part {
4952
local -r dir_path="$1"
50-
shift
53+
local -r change_dir_in_unique_part="$2"
54+
shift 2
5155
local -a -r args=("$@")
5256

53-
TFLINT_OUTPUT=$(tflint "${args[@]}" 2>&1)
57+
if [ "$change_dir_in_unique_part" == "delegate_chdir" ]; then
58+
local dir_args="--chdir=$dir_path"
59+
fi
60+
61+
# shellcheck disable=SC2086 # we need to remove the arg if its unset
62+
TFLINT_OUTPUT=$(tflint ${dir_args:-} "${args[@]}" 2>&1)
5463
local exit_code=$?
5564

5665
if [ $exit_code -ne 0 ]; then
5766
common::colorify "yellow" "TFLint in $dir_path/:"
58-
echo "$TFLINT_OUTPUT"
67+
echo -e "$TFLINT_OUTPUT"
5968
fi
6069

6170
# return exit code to common::per_dir_hook

hooks/terraform_tfsec.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,19 @@ function main {
3131
# Arguments:
3232
# dir_path (string) PATH to dir relative to git repo root.
3333
# Can be used in error logging
34+
# change_dir_in_unique_part (string/false) Modifier which creates
35+
# possibilities to use non-common chdir strategies.
36+
# Availability depends on hook.
3437
# args (array) arguments that configure wrapped tool behavior
3538
# Outputs:
3639
# If failed - print out hook checks status
3740
#######################################################################
3841
function per_dir_hook_unique_part {
3942
# shellcheck disable=SC2034 # Unused var.
4043
local -r dir_path="$1"
41-
shift
44+
# shellcheck disable=SC2034 # Unused var.
45+
local -r change_dir_in_unique_part="$2"
46+
shift 2
4247
local -a -r args=("$@")
4348

4449
# pass the arguments to hook

hooks/terraform_validate.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,18 @@ function match_validate_errors {
7070
# Arguments:
7171
# dir_path (string) PATH to dir relative to git repo root.
7272
# Can be used in error logging
73+
# change_dir_in_unique_part (string/false) Modifier which creates
74+
# possibilities to use non-common chdir strategies.
75+
# Availability depends on hook.
7376
# args (array) arguments that configure wrapped tool behavior
7477
# Outputs:
7578
# If failed - print out hook checks status
7679
#######################################################################
7780
function per_dir_hook_unique_part {
7881
local -r dir_path="$1"
79-
shift
82+
# shellcheck disable=SC2034 # Unused var.
83+
local -r change_dir_in_unique_part="$2"
84+
shift 2
8085
local -a -r args=("$@")
8186

8287
local exit_code
@@ -95,7 +100,7 @@ function per_dir_hook_unique_part {
95100

96101
case $key in
97102
--retry-once-with-cleanup)
98-
if [ $retry_once_with_cleanup ]; then
103+
if [ $retry_once_with_cleanup ]; then
99104
common::colorify "yellow" 'Invalid hook config. Make sure that you specify not more than one "--retry-once-with-cleanup" flag'
100105
exit 1
101106
fi

hooks/terragrunt_fmt.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,19 @@ function main {
2424
# Arguments:
2525
# dir_path (string) PATH to dir relative to git repo root.
2626
# Can be used in error logging
27+
# change_dir_in_unique_part (string/false) Modifier which creates
28+
# possibilities to use non-common chdir strategies.
29+
# Availability depends on hook.
2730
# args (array) arguments that configure wrapped tool behavior
2831
# Outputs:
2932
# If failed - print out hook checks status
3033
#######################################################################
3134
function per_dir_hook_unique_part {
3235
# shellcheck disable=SC2034 # Unused var.
3336
local -r dir_path="$1"
34-
shift
37+
# shellcheck disable=SC2034 # Unused var.
38+
local -r change_dir_in_unique_part="$2"
39+
shift 2
3540
local -a -r args=("$@")
3641

3742
# pass the arguments to hook

hooks/terragrunt_validate.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,19 @@ function main {
2424
# Arguments:
2525
# dir_path (string) PATH to dir relative to git repo root.
2626
# Can be used in error logging
27+
# change_dir_in_unique_part (string/false) Modifier which creates
28+
# possibilities to use non-common chdir strategies.
29+
# Availability depends on hook.
2730
# args (array) arguments that configure wrapped tool behavior
2831
# Outputs:
2932
# If failed - print out hook checks status
3033
#######################################################################
3134
function per_dir_hook_unique_part {
3235
# shellcheck disable=SC2034 # Unused var.
3336
local -r dir_path="$1"
34-
shift
37+
# shellcheck disable=SC2034 # Unused var.
38+
local -r change_dir_in_unique_part="$2"
39+
shift 2
3540
local -a -r args=("$@")
3641

3742
# pass the arguments to hook

0 commit comments

Comments
 (0)