2022-01-06 17:09:51 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
|
|
set -eo pipefail
|
|
|
|
|
|
2022-02-10 16:53:37 +01:00
|
|
|
# globals variables
|
2022-01-06 17:09:51 +02:00
|
|
|
# shellcheck disable=SC2155 # No way to assign to readonly variable in separate lines
|
2022-04-18 19:17:15 +03:00
|
|
|
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
|
2022-01-06 17:09:51 +02:00
|
|
|
# shellcheck source=_common.sh
|
|
|
|
|
. "$SCRIPT_DIR/_common.sh"
|
|
|
|
|
|
|
|
|
|
function main {
|
|
|
|
|
common::initialize "$SCRIPT_DIR"
|
|
|
|
|
common::parse_cmdline "$@"
|
2022-07-06 15:41:28 +03:00
|
|
|
common::export_provided_env_vars "${ENV_VARS[@]}"
|
2022-04-26 13:33:58 +03:00
|
|
|
common::parse_and_export_env_vars
|
2022-01-06 17:09:51 +02:00
|
|
|
# Support for setting PATH to repo root.
|
2022-10-06 19:16:29 +03:00
|
|
|
for i in "${!ARGS[@]}"; do
|
|
|
|
|
ARGS[i]=${ARGS[i]/__GIT_WORKING_DIR__/$(pwd)\/}
|
|
|
|
|
done
|
2022-07-06 15:34:13 +03:00
|
|
|
# JFYI: tflint color already suppressed via PRE_COMMIT_COLOR=never
|
2022-04-13 19:10:40 +03:00
|
|
|
|
|
|
|
|
# Run `tflint --init` for check that plugins installed.
|
|
|
|
|
# It should run once on whole repo.
|
|
|
|
|
{
|
2023-02-03 16:44:24 +01:00
|
|
|
TFLINT_INIT=$(tflint --init "${ARGS[@]}" 2>&1) 2> /dev/null &&
|
2022-04-13 19:10:40 +03:00
|
|
|
common::colorify "green" "Command 'tflint --init' successfully done:" &&
|
|
|
|
|
echo -e "${TFLINT_INIT}\n\n\n"
|
|
|
|
|
} || {
|
|
|
|
|
local exit_code=$?
|
|
|
|
|
common::colorify "red" "Command 'tflint --init' failed:"
|
2023-05-08 11:32:06 -04:00
|
|
|
echo -e "${TFLINT_INIT}"
|
2022-04-13 19:10:40 +03:00
|
|
|
return ${exit_code}
|
|
|
|
|
}
|
2022-10-06 19:16:29 +03:00
|
|
|
|
|
|
|
|
common::per_dir_hook "$HOOK_ID" "${#ARGS[@]}" "${ARGS[@]}" "${FILES[@]}"
|
2022-01-06 17:09:51 +02:00
|
|
|
}
|
|
|
|
|
|
2022-01-11 15:54:42 +02:00
|
|
|
#######################################################################
|
|
|
|
|
# Unique part of `common::per_dir_hook`. The function is executed in loop
|
|
|
|
|
# on each provided dir path. Run wrapped tool with specified arguments
|
|
|
|
|
# Arguments:
|
|
|
|
|
# dir_path (string) PATH to dir relative to git repo root.
|
|
|
|
|
# Can be used in error logging
|
2023-05-08 11:32:06 -04:00
|
|
|
# change_dir_in_unique_part (string/false) Modifier which creates
|
|
|
|
|
# possibilities to use non-common chdir strategies.
|
|
|
|
|
# Availability depends on hook.
|
2022-10-06 19:16:29 +03:00
|
|
|
# args (array) arguments that configure wrapped tool behavior
|
2022-01-11 15:54:42 +02:00
|
|
|
# Outputs:
|
|
|
|
|
# If failed - print out hook checks status
|
|
|
|
|
#######################################################################
|
2022-01-06 17:09:51 +02:00
|
|
|
function per_dir_hook_unique_part {
|
2022-10-06 19:16:29 +03:00
|
|
|
local -r dir_path="$1"
|
2023-05-08 11:32:06 -04:00
|
|
|
local -r change_dir_in_unique_part="$2"
|
|
|
|
|
shift 2
|
2022-10-06 19:16:29 +03:00
|
|
|
local -a -r args=("$@")
|
2022-01-06 17:09:51 +02:00
|
|
|
|
2023-05-08 11:32:06 -04:00
|
|
|
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)
|
2023-04-28 11:57:46 -04:00
|
|
|
local exit_code=$?
|
2022-01-06 17:09:51 +02:00
|
|
|
|
2023-04-28 11:57:46 -04:00
|
|
|
if [ $exit_code -ne 0 ]; then
|
|
|
|
|
common::colorify "yellow" "TFLint in $dir_path/:"
|
2023-05-08 11:32:06 -04:00
|
|
|
echo -e "$TFLINT_OUTPUT"
|
2023-04-28 11:57:46 -04:00
|
|
|
fi
|
2022-01-06 17:09:51 +02:00
|
|
|
|
|
|
|
|
# return exit code to common::per_dir_hook
|
|
|
|
|
return $exit_code
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[ "${BASH_SOURCE[0]}" != "$0" ] || main "$@"
|