mirror of
https://github.com/tofuutils/pre-commit-opentofu.git
synced 2025-10-15 17:38:54 +02:00
chore: Rewrite terraform_validate to common::per_dir_hook (#404)
* Refactor: Rewrite `terraform_validate` to `common::per_dir_hook` * Add ability to specify `--hook-config` in future
This commit is contained in:
parent
57467c56d4
commit
2623b7b8be
1 changed files with 55 additions and 73 deletions
|
|
@ -17,7 +17,18 @@ function main {
|
||||||
common::initialize "$SCRIPT_DIR"
|
common::initialize "$SCRIPT_DIR"
|
||||||
parse_cmdline_ "$@"
|
parse_cmdline_ "$@"
|
||||||
common::parse_and_export_env_vars
|
common::parse_and_export_env_vars
|
||||||
terraform_validate_
|
|
||||||
|
# Export provided env var K/V pairs to environment
|
||||||
|
local var var_name var_value
|
||||||
|
for var in "${ENVS[@]}"; do
|
||||||
|
var_name="${var%%=*}"
|
||||||
|
var_value="${var#*=}"
|
||||||
|
# shellcheck disable=SC2086
|
||||||
|
export $var_name="$var_value"
|
||||||
|
done
|
||||||
|
|
||||||
|
# shellcheck disable=SC2153 # False positive
|
||||||
|
common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}"
|
||||||
}
|
}
|
||||||
|
|
||||||
#######################################################################
|
#######################################################################
|
||||||
|
|
@ -25,6 +36,7 @@ function main {
|
||||||
# global variables with appropriate values
|
# global variables with appropriate values
|
||||||
# Globals (init and populate):
|
# Globals (init and populate):
|
||||||
# ARGS (array) arguments that configure wrapped tool behavior
|
# ARGS (array) arguments that configure wrapped tool behavior
|
||||||
|
# HOOK_CONFIG (array) arguments that configure hook behavior
|
||||||
# INIT_ARGS (array) arguments to `terraform init` command
|
# INIT_ARGS (array) arguments to `terraform init` command
|
||||||
# ENVS (array) environment variables that will be used with
|
# ENVS (array) environment variables that will be used with
|
||||||
# `terraform` commands
|
# `terraform` commands
|
||||||
|
|
@ -45,6 +57,11 @@ function parse_cmdline_ {
|
||||||
ARGS+=("$1")
|
ARGS+=("$1")
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
|
-h | --hook-config)
|
||||||
|
shift
|
||||||
|
HOOK_CONFIG+=("$1;")
|
||||||
|
shift
|
||||||
|
;;
|
||||||
-i | --init-args)
|
-i | --init-args)
|
||||||
shift
|
shift
|
||||||
INIT_ARGS+=("$1")
|
INIT_ARGS+=("$1")
|
||||||
|
|
@ -65,88 +82,53 @@ function parse_cmdline_ {
|
||||||
}
|
}
|
||||||
|
|
||||||
#######################################################################
|
#######################################################################
|
||||||
# Wrapper around `terraform validate` tool that checks if code is valid
|
# Unique part of `common::per_dir_hook`. The function is executed in loop
|
||||||
# 1. Export provided env var K/V pairs to environment
|
# on each provided dir path. Run wrapped tool with specified arguments
|
||||||
# 2. Because hook runs on whole dir, reduce file paths to uniq dir paths
|
# 1. Check if `.terraform` dir exists and if not - run `terraform init`
|
||||||
# 3. In each dir that have *.tf files:
|
# 2. Run `terraform validate`
|
||||||
# 3.1. Check if `.terraform` dir exists and if not - run `terraform init`
|
# 3. If at least 1 check failed - change the exit code to non-zero
|
||||||
# 3.2. Run `terraform validate`
|
# Arguments:
|
||||||
# 3.3. If at least 1 check failed - change exit code to non-zero
|
# args (string with array) arguments that configure wrapped tool behavior
|
||||||
# 4. Complete hook execution and return exit code
|
# dir_path (string) PATH to dir relative to git repo root.
|
||||||
|
# Can be used in error logging
|
||||||
# Globals:
|
# Globals:
|
||||||
# ARGS (array) arguments that configure wrapped tool behavior
|
|
||||||
# INIT_ARGS (array) arguments for `terraform init` command`
|
# INIT_ARGS (array) arguments for `terraform init` command`
|
||||||
# ENVS (array) environment variables that will be used with
|
# ENVS (array) environment variables that will be used with
|
||||||
# `terraform` commands
|
# `terraform` commands
|
||||||
# FILES (array) filenames to check
|
|
||||||
# Outputs:
|
# Outputs:
|
||||||
# If failed - print out hook checks status
|
# If failed - print out hook checks status
|
||||||
#######################################################################
|
#######################################################################
|
||||||
function terraform_validate_ {
|
function per_dir_hook_unique_part {
|
||||||
|
local -r args="$1"
|
||||||
|
local -r dir_path="$2"
|
||||||
|
|
||||||
# Setup environment variables
|
local exit_code
|
||||||
local var var_name var_value
|
local init_output
|
||||||
for var in "${ENVS[@]}"; do
|
local validate_output
|
||||||
var_name="${var%%=*}"
|
|
||||||
var_value="${var#*=}"
|
|
||||||
# shellcheck disable=SC2086
|
|
||||||
export $var_name="$var_value"
|
|
||||||
done
|
|
||||||
|
|
||||||
declare -a paths
|
if [ ! -d .terraform ]; then
|
||||||
local index=0
|
init_output=$(terraform init -backend=false "${INIT_ARGS[@]}" 2>&1)
|
||||||
local error=0
|
exit_code=$?
|
||||||
|
|
||||||
local file_with_path
|
if [ $exit_code -ne 0 ]; then
|
||||||
for file_with_path in "${FILES[@]}"; do
|
common::colorify "yellow" "'terraform init' failed, 'terraform validate' skipped: $dir_path"
|
||||||
file_with_path="${file_with_path// /__REPLACED__SPACE__}"
|
echo "$init_output"
|
||||||
|
return $exit_code
|
||||||
paths[index]=$(dirname "$file_with_path")
|
|
||||||
((index += 1))
|
|
||||||
done
|
|
||||||
|
|
||||||
local dir_path
|
|
||||||
for dir_path in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do
|
|
||||||
dir_path="${dir_path//__REPLACED__SPACE__/ }"
|
|
||||||
|
|
||||||
if [[ -n "$(find "$dir_path" -maxdepth 1 -name '*.tf' -print -quit)" ]]; then
|
|
||||||
|
|
||||||
pushd "$(cd "$dir_path" > /dev/null && pwd -P)" > /dev/null
|
|
||||||
|
|
||||||
if [ ! -d .terraform ]; then
|
|
||||||
set +e
|
|
||||||
init_output=$(terraform init -backend=false "${INIT_ARGS[@]}" 2>&1)
|
|
||||||
init_code=$?
|
|
||||||
set -e
|
|
||||||
|
|
||||||
if [ $init_code -ne 0 ]; then
|
|
||||||
error=1
|
|
||||||
echo "Init before validation failed: $dir_path"
|
|
||||||
echo "$init_output"
|
|
||||||
popd > /dev/null
|
|
||||||
continue
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
set +e
|
|
||||||
validate_output=$(terraform validate "${ARGS[@]}" 2>&1)
|
|
||||||
validate_code=$?
|
|
||||||
set -e
|
|
||||||
|
|
||||||
if [ $validate_code -ne 0 ]; then
|
|
||||||
error=1
|
|
||||||
echo "Validation failed: $dir_path"
|
|
||||||
echo "$validate_output"
|
|
||||||
echo
|
|
||||||
fi
|
|
||||||
|
|
||||||
popd > /dev/null
|
|
||||||
fi
|
fi
|
||||||
done
|
|
||||||
|
|
||||||
if [ $error -ne 0 ]; then
|
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# pass the arguments to hook
|
||||||
|
# shellcheck disable=SC2068 # hook fails when quoting is used ("$arg[@]")
|
||||||
|
validate_output=$(terraform validate ${args[@]} 2>&1)
|
||||||
|
exit_code=$?
|
||||||
|
|
||||||
|
if [ $exit_code -ne 0 ]; then
|
||||||
|
common::colorify "red" "Validation failed: $dir_path"
|
||||||
|
echo -e "$validate_output\n\n"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# return exit code to common::per_dir_hook
|
||||||
|
return $exit_code
|
||||||
}
|
}
|
||||||
|
|
||||||
# global arrays
|
# global arrays
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue