@@ -15,79 +15,7 @@ function main {
1515 common::parse_cmdline " $@ "
1616 common::parse_and_export_env_vars
1717 # shellcheck disable=SC2153 # False positive
18- terraform_fmt_ " ${ARGS[*]} " " ${FILES[@]} "
19- }
20-
21- # ######################################################################
22- # Hook execution boilerplate logic which is common to hooks, that run
23- # on per dir basis. Little bit extended than `common::per_dir_hook`
24- # 1. Because hook runs on whole dir, reduce file paths to uniq dir paths
25- # (unique) 1.1. Collect paths to *.tfvars files in a separate variable
26- # 2. Run for each dir `per_dir_hook_unique_part`, on all paths
27- # (unique) 2.1. Run `terraform fmt` on each *.tfvars file
28- # 2.2. If at least 1 check failed - change exit code to non-zero
29- # 3. Complete hook execution and return exit code
30- # Arguments:
31- # args (string with array) arguments that configure wrapped tool behavior
32- # files (array) filenames to check
33- # ######################################################################
34- function terraform_fmt_ {
35- local -r args=" $1 "
36- shift 1
37- local -a -r files=(" $@ " )
38- # consume modified files passed from pre-commit so that
39- # hook runs against only those relevant directories
40- local index=0
41- for file_with_path in " ${files[@]} " ; do
42- file_with_path=" ${file_with_path// / __REPLACED__SPACE__} "
43-
44- dir_paths[index]=$( dirname " $file_with_path " )
45- # TODO Unique part
46- if [[ " $file_with_path " == * " .tfvars" ]]; then
47- tfvars_files+=(" $file_with_path " )
48- fi
49- # ? End for unique part
50- (( index += 1 ))
51- done
52-
53- # preserve errexit status
54- shopt -qo errexit && ERREXIT_IS_SET=true
55- # allow hook to continue if exit_code is greater than 0
56- set +e
57- local final_exit_code=0
58-
59- # run hook for each path
60- for dir_path in $( echo " ${dir_paths[*]} " | tr ' ' ' \n' | sort -u) ; do
61- dir_path=" ${dir_path// __REPLACED__SPACE__/ } "
62- pushd " $dir_path " > /dev/null || continue
63-
64- per_dir_hook_unique_part " $args " " $dir_path "
65-
66- local exit_code=$?
67- if [ $exit_code -ne 0 ]; then
68- final_exit_code=$exit_code
69- fi
70-
71- popd > /dev/null
72- done
73-
74- # TODO: Unique part
75- # terraform.tfvars are excluded by `terraform fmt`
76- for tfvars_file in " ${tfvars_files[@]} " ; do
77- tfvars_file=" ${tfvars_file// __REPLACED__SPACE__/ } "
78-
79- terraform fmt " ${ARGS[@]} " " $tfvars_file "
80- local exit_code=$?
81- if [ $exit_code -ne 0 ]; then
82- final_exit_code=$exit_code
83- fi
84- done
85- # ? End for unique part
86- # restore errexit if it was set before the "for" loop
87- [[ $ERREXIT_IS_SET ]] && set -e
88- # return the hook final exit_code
89- exit $final_exit_code
90-
18+ common::per_dir_hook " ${ARGS[*]} " " $HOOK_ID " " ${FILES[@]} "
9119}
9220
9321# ######################################################################
@@ -102,6 +30,7 @@ function terraform_fmt_ {
10230# ######################################################################
10331function per_dir_hook_unique_part {
10432 local -r args=" $1 "
33+ # shellcheck disable=SC2034 # Unused var.
10534 local -r dir_path=" $2 "
10635
10736 # pass the arguments to hook
0 commit comments