forked from github/pre-commit-opentofu
feat: TFLint: Add --hook-config=--delegate-chdir to use tflint -chdir (#512)
Co-authored-by: Maksym Vlasov <MaxymVlasov@users.noreply.github.com>
This commit is contained in:
parent
1431664e59
commit
1e9debc02f
12 changed files with 101 additions and 16 deletions
|
|
@ -604,6 +604,14 @@ To replicate functionality in `terraform_docs` hook:
|
||||||
- --args=--config=__GIT_WORKING_DIR__/.tflint.hcl
|
- --args=--config=__GIT_WORKING_DIR__/.tflint.hcl
|
||||||
```
|
```
|
||||||
|
|
||||||
|
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:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
- id: terraform_tflint
|
||||||
|
args:
|
||||||
|
- --hook-config=--delegate-chdir
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### terraform_tfsec
|
### terraform_tfsec
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -217,6 +217,25 @@ function common::per_dir_hook {
|
||||||
((index += 1))
|
((index += 1))
|
||||||
done
|
done
|
||||||
|
|
||||||
|
# Lookup hook-config for modifiers that impact common behavior
|
||||||
|
local change_dir_in_unique_part=false
|
||||||
|
IFS=";" read -r -a configs <<< "${HOOK_CONFIG[*]}"
|
||||||
|
for c in "${configs[@]}"; do
|
||||||
|
IFS="=" read -r -a config <<< "$c"
|
||||||
|
key=${config[0]}
|
||||||
|
value=${config[1]}
|
||||||
|
|
||||||
|
case $key in
|
||||||
|
--delegate-chdir)
|
||||||
|
# this flag will skip pushing and popping directories
|
||||||
|
# delegating the responsibility to the hooked plugin/binary
|
||||||
|
if [[ ! $value || $value == true ]]; then
|
||||||
|
change_dir_in_unique_part="delegate_chdir"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
# preserve errexit status
|
# preserve errexit status
|
||||||
shopt -qo errexit && ERREXIT_IS_SET=true
|
shopt -qo errexit && ERREXIT_IS_SET=true
|
||||||
# allow hook to continue if exit_code is greater than 0
|
# allow hook to continue if exit_code is greater than 0
|
||||||
|
|
@ -226,16 +245,22 @@ function common::per_dir_hook {
|
||||||
# run hook for each path
|
# run hook for each path
|
||||||
for dir_path in $(echo "${dir_paths[*]}" | tr ' ' '\n' | sort -u); do
|
for dir_path in $(echo "${dir_paths[*]}" | tr ' ' '\n' | sort -u); do
|
||||||
dir_path="${dir_path//__REPLACED__SPACE__/ }"
|
dir_path="${dir_path//__REPLACED__SPACE__/ }"
|
||||||
pushd "$dir_path" > /dev/null || continue
|
|
||||||
|
|
||||||
per_dir_hook_unique_part "$dir_path" "${args[@]}"
|
if [[ $change_dir_in_unique_part == false ]]; then
|
||||||
|
pushd "$dir_path" > /dev/null || continue
|
||||||
|
fi
|
||||||
|
|
||||||
|
per_dir_hook_unique_part "$dir_path" "$change_dir_in_unique_part" "${args[@]}"
|
||||||
|
|
||||||
local exit_code=$?
|
local exit_code=$?
|
||||||
if [ $exit_code -ne 0 ]; then
|
if [ $exit_code -ne 0 ]; then
|
||||||
final_exit_code=$exit_code
|
final_exit_code=$exit_code
|
||||||
fi
|
fi
|
||||||
|
|
||||||
popd > /dev/null
|
if [[ $change_dir_in_unique_part == false ]]; then
|
||||||
|
popd > /dev/null
|
||||||
|
fi
|
||||||
|
|
||||||
done
|
done
|
||||||
|
|
||||||
# restore errexit if it was set before the "for" loop
|
# restore errexit if it was set before the "for" loop
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,9 @@ function main {
|
||||||
# Arguments:
|
# Arguments:
|
||||||
# dir_path (string) PATH to dir relative to git repo root.
|
# dir_path (string) PATH to dir relative to git repo root.
|
||||||
# Can be used in error logging
|
# Can be used in error logging
|
||||||
|
# change_dir_in_unique_part (string/false) Modifier which creates
|
||||||
|
# possibilities to use non-common chdir strategies.
|
||||||
|
# Availability depends on hook.
|
||||||
# args (array) arguments that configure wrapped tool behavior
|
# args (array) arguments that configure wrapped tool behavior
|
||||||
# Outputs:
|
# Outputs:
|
||||||
# If failed - print out hook checks status
|
# If failed - print out hook checks status
|
||||||
|
|
@ -38,7 +41,9 @@ function main {
|
||||||
function per_dir_hook_unique_part {
|
function per_dir_hook_unique_part {
|
||||||
# shellcheck disable=SC2034 # Unused var.
|
# shellcheck disable=SC2034 # Unused var.
|
||||||
local -r dir_path="$1"
|
local -r dir_path="$1"
|
||||||
shift
|
# shellcheck disable=SC2034 # Unused var.
|
||||||
|
local -r change_dir_in_unique_part="$2"
|
||||||
|
shift 2
|
||||||
local -a -r args=("$@")
|
local -a -r args=("$@")
|
||||||
|
|
||||||
checkov -d . "${args[@]}"
|
checkov -d . "${args[@]}"
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,9 @@ function main {
|
||||||
# Arguments:
|
# Arguments:
|
||||||
# dir_path (string) PATH to dir relative to git repo root.
|
# dir_path (string) PATH to dir relative to git repo root.
|
||||||
# Can be used in error logging
|
# Can be used in error logging
|
||||||
|
# change_dir_in_unique_part (string/false) Modifier which creates
|
||||||
|
# possibilities to use non-common chdir strategies.
|
||||||
|
# Availability depends on hook.
|
||||||
# args (array) arguments that configure wrapped tool behavior
|
# args (array) arguments that configure wrapped tool behavior
|
||||||
# Outputs:
|
# Outputs:
|
||||||
# If failed - print out hook checks status
|
# If failed - print out hook checks status
|
||||||
|
|
@ -35,7 +38,9 @@ function main {
|
||||||
function per_dir_hook_unique_part {
|
function per_dir_hook_unique_part {
|
||||||
# shellcheck disable=SC2034 # Unused var.
|
# shellcheck disable=SC2034 # Unused var.
|
||||||
local -r dir_path="$1"
|
local -r dir_path="$1"
|
||||||
shift
|
# shellcheck disable=SC2034 # Unused var.
|
||||||
|
local -r change_dir_in_unique_part="$2"
|
||||||
|
shift 2
|
||||||
local -a -r args=("$@")
|
local -a -r args=("$@")
|
||||||
|
|
||||||
# pass the arguments to hook
|
# pass the arguments to hook
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,9 @@ function main {
|
||||||
# Arguments:
|
# Arguments:
|
||||||
# dir_path (string) PATH to dir relative to git repo root.
|
# dir_path (string) PATH to dir relative to git repo root.
|
||||||
# Can be used in error logging
|
# Can be used in error logging
|
||||||
|
# change_dir_in_unique_part (string/false) Modifier which creates
|
||||||
|
# possibilities to use non-common chdir strategies.
|
||||||
|
# Availability depends on hook.
|
||||||
# args (array) arguments that configure wrapped tool behavior
|
# args (array) arguments that configure wrapped tool behavior
|
||||||
# Outputs:
|
# Outputs:
|
||||||
# If failed - print out hook checks status
|
# If failed - print out hook checks status
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ function main {
|
||||||
} || {
|
} || {
|
||||||
local exit_code=$?
|
local exit_code=$?
|
||||||
common::colorify "red" "Command 'tflint --init' failed:"
|
common::colorify "red" "Command 'tflint --init' failed:"
|
||||||
echo "${TFLINT_INIT}"
|
echo -e "${TFLINT_INIT}"
|
||||||
return ${exit_code}
|
return ${exit_code}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -41,21 +41,30 @@ function main {
|
||||||
# Arguments:
|
# Arguments:
|
||||||
# dir_path (string) PATH to dir relative to git repo root.
|
# dir_path (string) PATH to dir relative to git repo root.
|
||||||
# Can be used in error logging
|
# Can be used in error logging
|
||||||
|
# change_dir_in_unique_part (string/false) Modifier which creates
|
||||||
|
# possibilities to use non-common chdir strategies.
|
||||||
|
# Availability depends on hook.
|
||||||
# args (array) arguments that configure wrapped tool behavior
|
# args (array) arguments that configure wrapped tool behavior
|
||||||
# Outputs:
|
# Outputs:
|
||||||
# If failed - print out hook checks status
|
# If failed - print out hook checks status
|
||||||
#######################################################################
|
#######################################################################
|
||||||
function per_dir_hook_unique_part {
|
function per_dir_hook_unique_part {
|
||||||
local -r dir_path="$1"
|
local -r dir_path="$1"
|
||||||
shift
|
local -r change_dir_in_unique_part="$2"
|
||||||
|
shift 2
|
||||||
local -a -r args=("$@")
|
local -a -r args=("$@")
|
||||||
|
|
||||||
TFLINT_OUTPUT=$(tflint "${args[@]}" 2>&1)
|
if [ "$change_dir_in_unique_part" == "delegate_chdir" ]; then
|
||||||
|
local dir_args="--chdir=$dir_path"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# shellcheck disable=SC2086 # we need to remove the arg if its unset
|
||||||
|
TFLINT_OUTPUT=$(tflint ${dir_args:-} "${args[@]}" 2>&1)
|
||||||
local exit_code=$?
|
local exit_code=$?
|
||||||
|
|
||||||
if [ $exit_code -ne 0 ]; then
|
if [ $exit_code -ne 0 ]; then
|
||||||
common::colorify "yellow" "TFLint in $dir_path/:"
|
common::colorify "yellow" "TFLint in $dir_path/:"
|
||||||
echo "$TFLINT_OUTPUT"
|
echo -e "$TFLINT_OUTPUT"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# return exit code to common::per_dir_hook
|
# return exit code to common::per_dir_hook
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,9 @@ function main {
|
||||||
# Arguments:
|
# Arguments:
|
||||||
# dir_path (string) PATH to dir relative to git repo root.
|
# dir_path (string) PATH to dir relative to git repo root.
|
||||||
# Can be used in error logging
|
# Can be used in error logging
|
||||||
|
# change_dir_in_unique_part (string/false) Modifier which creates
|
||||||
|
# possibilities to use non-common chdir strategies.
|
||||||
|
# Availability depends on hook.
|
||||||
# args (array) arguments that configure wrapped tool behavior
|
# args (array) arguments that configure wrapped tool behavior
|
||||||
# Outputs:
|
# Outputs:
|
||||||
# If failed - print out hook checks status
|
# If failed - print out hook checks status
|
||||||
|
|
@ -38,7 +41,9 @@ function main {
|
||||||
function per_dir_hook_unique_part {
|
function per_dir_hook_unique_part {
|
||||||
# shellcheck disable=SC2034 # Unused var.
|
# shellcheck disable=SC2034 # Unused var.
|
||||||
local -r dir_path="$1"
|
local -r dir_path="$1"
|
||||||
shift
|
# shellcheck disable=SC2034 # Unused var.
|
||||||
|
local -r change_dir_in_unique_part="$2"
|
||||||
|
shift 2
|
||||||
local -a -r args=("$@")
|
local -a -r args=("$@")
|
||||||
|
|
||||||
# pass the arguments to hook
|
# pass the arguments to hook
|
||||||
|
|
|
||||||
|
|
@ -70,13 +70,18 @@ function match_validate_errors {
|
||||||
# Arguments:
|
# Arguments:
|
||||||
# dir_path (string) PATH to dir relative to git repo root.
|
# dir_path (string) PATH to dir relative to git repo root.
|
||||||
# Can be used in error logging
|
# Can be used in error logging
|
||||||
|
# change_dir_in_unique_part (string/false) Modifier which creates
|
||||||
|
# possibilities to use non-common chdir strategies.
|
||||||
|
# Availability depends on hook.
|
||||||
# args (array) arguments that configure wrapped tool behavior
|
# args (array) arguments that configure wrapped tool behavior
|
||||||
# Outputs:
|
# Outputs:
|
||||||
# If failed - print out hook checks status
|
# If failed - print out hook checks status
|
||||||
#######################################################################
|
#######################################################################
|
||||||
function per_dir_hook_unique_part {
|
function per_dir_hook_unique_part {
|
||||||
local -r dir_path="$1"
|
local -r dir_path="$1"
|
||||||
shift
|
# shellcheck disable=SC2034 # Unused var.
|
||||||
|
local -r change_dir_in_unique_part="$2"
|
||||||
|
shift 2
|
||||||
local -a -r args=("$@")
|
local -a -r args=("$@")
|
||||||
|
|
||||||
local exit_code
|
local exit_code
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,9 @@ function main {
|
||||||
# Arguments:
|
# Arguments:
|
||||||
# dir_path (string) PATH to dir relative to git repo root.
|
# dir_path (string) PATH to dir relative to git repo root.
|
||||||
# Can be used in error logging
|
# Can be used in error logging
|
||||||
|
# change_dir_in_unique_part (string/false) Modifier which creates
|
||||||
|
# possibilities to use non-common chdir strategies.
|
||||||
|
# Availability depends on hook.
|
||||||
# args (array) arguments that configure wrapped tool behavior
|
# args (array) arguments that configure wrapped tool behavior
|
||||||
# Outputs:
|
# Outputs:
|
||||||
# If failed - print out hook checks status
|
# If failed - print out hook checks status
|
||||||
|
|
@ -31,7 +34,9 @@ function main {
|
||||||
function per_dir_hook_unique_part {
|
function per_dir_hook_unique_part {
|
||||||
# shellcheck disable=SC2034 # Unused var.
|
# shellcheck disable=SC2034 # Unused var.
|
||||||
local -r dir_path="$1"
|
local -r dir_path="$1"
|
||||||
shift
|
# shellcheck disable=SC2034 # Unused var.
|
||||||
|
local -r change_dir_in_unique_part="$2"
|
||||||
|
shift 2
|
||||||
local -a -r args=("$@")
|
local -a -r args=("$@")
|
||||||
|
|
||||||
# pass the arguments to hook
|
# pass the arguments to hook
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,9 @@ function main {
|
||||||
# Arguments:
|
# Arguments:
|
||||||
# dir_path (string) PATH to dir relative to git repo root.
|
# dir_path (string) PATH to dir relative to git repo root.
|
||||||
# Can be used in error logging
|
# Can be used in error logging
|
||||||
|
# change_dir_in_unique_part (string/false) Modifier which creates
|
||||||
|
# possibilities to use non-common chdir strategies.
|
||||||
|
# Availability depends on hook.
|
||||||
# args (array) arguments that configure wrapped tool behavior
|
# args (array) arguments that configure wrapped tool behavior
|
||||||
# Outputs:
|
# Outputs:
|
||||||
# If failed - print out hook checks status
|
# If failed - print out hook checks status
|
||||||
|
|
@ -31,7 +34,9 @@ function main {
|
||||||
function per_dir_hook_unique_part {
|
function per_dir_hook_unique_part {
|
||||||
# shellcheck disable=SC2034 # Unused var.
|
# shellcheck disable=SC2034 # Unused var.
|
||||||
local -r dir_path="$1"
|
local -r dir_path="$1"
|
||||||
shift
|
# shellcheck disable=SC2034 # Unused var.
|
||||||
|
local -r change_dir_in_unique_part="$2"
|
||||||
|
shift 2
|
||||||
local -a -r args=("$@")
|
local -a -r args=("$@")
|
||||||
|
|
||||||
# pass the arguments to hook
|
# pass the arguments to hook
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,9 @@ function main {
|
||||||
# Arguments:
|
# Arguments:
|
||||||
# dir_path (string) PATH to dir relative to git repo root.
|
# dir_path (string) PATH to dir relative to git repo root.
|
||||||
# Can be used in error logging
|
# Can be used in error logging
|
||||||
|
# change_dir_in_unique_part (string/false) Modifier which creates
|
||||||
|
# possibilities to use non-common chdir strategies.
|
||||||
|
# Availability depends on hook.
|
||||||
# args (array) arguments that configure wrapped tool behavior
|
# args (array) arguments that configure wrapped tool behavior
|
||||||
# Outputs:
|
# Outputs:
|
||||||
# If failed - print out hook checks status
|
# If failed - print out hook checks status
|
||||||
|
|
@ -31,7 +34,9 @@ function main {
|
||||||
function per_dir_hook_unique_part {
|
function per_dir_hook_unique_part {
|
||||||
# shellcheck disable=SC2034 # Unused var.
|
# shellcheck disable=SC2034 # Unused var.
|
||||||
local -r dir_path="$1"
|
local -r dir_path="$1"
|
||||||
shift
|
# shellcheck disable=SC2034 # Unused var.
|
||||||
|
local -r change_dir_in_unique_part="$2"
|
||||||
|
shift 2
|
||||||
local -a -r args=("$@")
|
local -a -r args=("$@")
|
||||||
|
|
||||||
# pass the arguments to hook
|
# pass the arguments to hook
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,9 @@ function main {
|
||||||
# Arguments:
|
# Arguments:
|
||||||
# dir_path (string) PATH to dir relative to git repo root.
|
# dir_path (string) PATH to dir relative to git repo root.
|
||||||
# Can be used in error logging
|
# Can be used in error logging
|
||||||
|
# change_dir_in_unique_part (string/false) Modifier which creates
|
||||||
|
# possibilities to use non-common chdir strategies.
|
||||||
|
# Availability depends on hook.
|
||||||
# args (array) arguments that configure wrapped tool behavior
|
# args (array) arguments that configure wrapped tool behavior
|
||||||
# Outputs:
|
# Outputs:
|
||||||
# If failed - print out hook checks status
|
# If failed - print out hook checks status
|
||||||
|
|
@ -41,7 +44,9 @@ function main {
|
||||||
function per_dir_hook_unique_part {
|
function per_dir_hook_unique_part {
|
||||||
# shellcheck disable=SC2034 # Unused var.
|
# shellcheck disable=SC2034 # Unused var.
|
||||||
local -r dir_path="$1"
|
local -r dir_path="$1"
|
||||||
shift
|
# shellcheck disable=SC2034 # Unused var.
|
||||||
|
local -r change_dir_in_unique_part="$2"
|
||||||
|
shift 2
|
||||||
local -a -r args=("$@")
|
local -a -r args=("$@")
|
||||||
|
|
||||||
# pass the arguments to hook
|
# pass the arguments to hook
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue