diff --git a/hooks/_common.sh b/hooks/_common.sh index ae221b4de..db34fe87c 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -6,7 +6,7 @@ set -eo pipefail # Arguments: # script_dir - absolute path to hook dir location ####################################################################### -function common::initialize { +common::initialize() { local -r script_dir=$1 # source getopt function # shellcheck source=../lib_getopt @@ -24,7 +24,7 @@ function common::initialize { # $@ (array) all specified in `hooks.[].args` in # `.pre-commit-config.yaml` and filenames. ####################################################################### -function common::parse_cmdline { +common::parse_cmdline() { # common global arrays. # Populated via `common::parse_cmdline` and can be used inside hooks' functions declare -g -a ARGS=() HOOK_CONFIG=() FILES=() @@ -66,7 +66,7 @@ function common::parse_cmdline { # args (string with array) arguments that configure wrapped tool behavior # files (array) filenames to check ####################################################################### -function common::per_dir_hook { +common::per_dir_hook() { local -r args="$1" shift 1 local -a -r files=("$@") @@ -119,7 +119,7 @@ function common::per_dir_hook { # Outputs: # Print out provided text to stdout ####################################################################### -function common::colorify { +common::colorify() { # shellcheck disable=SC2034 local -r red="\e[0m\e[31m" # shellcheck disable=SC2034 diff --git a/hooks/infracost_breakdown.sh b/hooks/infracost_breakdown.sh index dc7bc51b1..ab72c2af4 100755 --- a/hooks/infracost_breakdown.sh +++ b/hooks/infracost_breakdown.sh @@ -6,7 +6,16 @@ readonly SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" # shellcheck source=_common.sh . "$SCRIPT_DIR/_common.sh" -function main { +####################################### +# main function +# Globals: +# ARGS +# HOOK_CONFIG +# SCRIPT_DIR +# Arguments: +# None +####################################### +main() { common::initialize "$SCRIPT_DIR" common::parse_cmdline "$@" # shellcheck disable=SC2153 # False positive @@ -25,7 +34,7 @@ function main { # Print out hook checks status (Passed/Failed), total monthly cost and # diff, summary about infracost check (non-supported resources etc.) ####################################################################### -function infracost_breakdown_ { +infracost_breakdown_() { local -r hook_config="$1" local args read -r -a args <<< "$2" @@ -85,8 +94,8 @@ function infracost_breakdown_ { real_value="$(jq "${jq_check[0]}" <<< "$RESULTS")" compare_value="${jq_check[1]}${jq_check[2]}" # Check types - jq_check_type="$(jq -r "${jq_check[0]} | type" <<< "$RESULTS")" - compare_value_type="$(jq -r "$compare_value | type" <<< "$RESULTS")" + jq_check_type="$(jq -r "${jq_check[0]} | type" <<<"$RESULTS")" + compare_value_type="$(jq -r "$compare_value | type" <<<"$RESULTS")" # Fail if comparing different types if [ "$jq_check_type" != "$compare_value_type" ]; then common::colorify "yellow" "Warning: Comparing values with different types may give incorrect result" @@ -127,12 +136,15 @@ function infracost_breakdown_ { echo -e "\nSummary: $(jq -r '.summary' <<< "$RESULTS")" - echo -e "\nTotal Monthly Cost: $(jq -r .totalMonthlyCost <<< "$RESULTS") $currency" - echo "Total Monthly Cost (diff): $(jq -r .projects[].diff.totalMonthlyCost <<< "$RESULTS") $currency" + echo -e "\nTotal Monthly Cost: $(jq -r .totalMonthlyCost <<<"$RESULTS") $currency" + echo "Total Monthly Cost (diff): $(jq -r .projects[].diff.totalMonthlyCost <<<"$RESULTS") $currency" if $have_failed_checks; then exit 1 fi } + + + [ "${BASH_SOURCE[0]}" != "$0" ] || main "$@" diff --git a/hooks/terraform_docs.sh b/hooks/terraform_docs.sh index e71aae474..d3fbea38a 100755 --- a/hooks/terraform_docs.sh +++ b/hooks/terraform_docs.sh @@ -6,7 +6,17 @@ readonly SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" # shellcheck source=_common.sh . "$SCRIPT_DIR/_common.sh" -function main { +####################################### +# main function +# Globals: +# ARGS +# FILES +# HOOK_CONFIG +# SCRIPT_DIR +# Arguments: +# None +####################################### +main() { common::initialize "$SCRIPT_DIR" common::parse_cmdline "$@" # Support for setting relative PATH to .terraform-docs.yml config. @@ -25,7 +35,7 @@ function main { # args (string with array) arguments that configure wrapped tool behavior # files (array) filenames to check ####################################################################### -function terraform_docs_ { +terraform_docs_() { local -r hook_config="$1" local -r args="$2" shift 2 @@ -81,7 +91,7 @@ function terraform_docs_ { # args (string with array) arguments that configure wrapped tool behavior # files (array) filenames to check ####################################################################### -function terraform_docs { +terraform_docs() { local -r terraform_docs_awk_file="$1" local -r hook_config="$2" local -r args="$3" @@ -141,8 +151,8 @@ function terraform_docs { # if $create_if_not_exist && [[ ! -f "$text_file" ]]; then dir_have_tf_files="$( - find . -maxdepth 1 -type f | sed 's|.*\.||' | sort -u | grep -oE '^tf$|^tfvars$' || - exit 0 + find . -maxdepth 1 -type f | sed 's|.*\.||' | sort -u | grep -oE '^tf$|^tfvars$' \ + || exit 0 )" # if no TF files - skip dir @@ -209,7 +219,7 @@ function terraform_docs { # Arguments: # output_file (string) filename where hack will be written to ####################################################################### -function terraform_docs_awk { +terraform_docs_awk() { local -r output_file=$1 cat << "EOF" > "$output_file" @@ -368,4 +378,7 @@ EOF } + + + [ "${BASH_SOURCE[0]}" != "$0" ] || main "$@" diff --git a/hooks/terraform_fmt.sh b/hooks/terraform_fmt.sh index 84746a62b..645419d57 100755 --- a/hooks/terraform_fmt.sh +++ b/hooks/terraform_fmt.sh @@ -6,7 +6,16 @@ readonly SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" # shellcheck source=_common.sh . "$SCRIPT_DIR/_common.sh" -function main { +####################################### +# main function +# Globals: +# ARGS +# FILES +# SCRIPT_DIR +# Arguments: +# None +####################################### +main() { common::initialize "$SCRIPT_DIR" common::parse_cmdline "$@" # shellcheck disable=SC2153 # False positive @@ -26,7 +35,7 @@ function main { # args (string with array) arguments that configure wrapped tool behavior # files (array) filenames to check ####################################################################### -function terraform_fmt_ { +terraform_fmt_() { local -r args="$1" shift 1 local -a -r files=("$@") @@ -70,7 +79,7 @@ function terraform_fmt_ { # terraform.tfvars are excluded by `terraform fmt` for tfvars_file in "${tfvars_files[@]}"; do tfvars_file="${tfvars_file//__REPLACED__SPACE__/ }" - + # shellcheck disable=SC2153 # False positive terraform fmt "${ARGS[@]}" "$tfvars_file" local exit_code=$? if [ $exit_code -ne 0 ]; then @@ -95,7 +104,7 @@ function terraform_fmt_ { # Outputs: # If failed - print out hook checks status ####################################################################### -function per_dir_hook_unique_part { +per_dir_hook_unique_part() { local -r args="$1" local -r dir_path="$2" @@ -108,4 +117,7 @@ function per_dir_hook_unique_part { return $exit_code } + + + [ "${BASH_SOURCE[0]}" != "$0" ] || main "$@" diff --git a/hooks/terraform_providers_lock.sh b/hooks/terraform_providers_lock.sh index 510ff3b4d..5e46832b8 100755 --- a/hooks/terraform_providers_lock.sh +++ b/hooks/terraform_providers_lock.sh @@ -7,7 +7,16 @@ readonly SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" # shellcheck source=_common.sh . "$SCRIPT_DIR/_common.sh" -function main { +####################################### +# main function +# Globals: +# ARGS +# FILES +# SCRIPT_DIR +# Arguments: +# None +####################################### +main() { common::initialize "$SCRIPT_DIR" common::parse_cmdline "$@" # shellcheck disable=SC2153 # False positive @@ -24,7 +33,7 @@ function main { # Outputs: # If failed - print out hook checks status ####################################################################### -function per_dir_hook_unique_part { +per_dir_hook_unique_part() { local -r args="$1" local -r dir_path="$2" @@ -48,4 +57,5 @@ function per_dir_hook_unique_part { return $exit_code } + [ "${BASH_SOURCE[0]}" != "$0" ] || main "$@" diff --git a/hooks/terraform_tflint.sh b/hooks/terraform_tflint.sh index b3788a92e..802697635 100755 --- a/hooks/terraform_tflint.sh +++ b/hooks/terraform_tflint.sh @@ -7,7 +7,16 @@ readonly SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" # shellcheck source=_common.sh . "$SCRIPT_DIR/_common.sh" -function main { +####################################### +# main function +# Globals: +# ARGS +# FILES +# SCRIPT_DIR +# Arguments: +# None +####################################### +main() { common::initialize "$SCRIPT_DIR" common::parse_cmdline "$@" # Support for setting PATH to repo root. @@ -27,7 +36,7 @@ function main { # Outputs: # If failed - print out hook checks status ####################################################################### -function per_dir_hook_unique_part { +per_dir_hook_unique_part() { local -r args="$1" local -r dir_path="$2" @@ -45,4 +54,6 @@ function per_dir_hook_unique_part { return $exit_code } + + [ "${BASH_SOURCE[0]}" != "$0" ] || main "$@" diff --git a/hooks/terraform_tfsec.sh b/hooks/terraform_tfsec.sh index f39c0285d..472c00291 100755 --- a/hooks/terraform_tfsec.sh +++ b/hooks/terraform_tfsec.sh @@ -6,7 +6,16 @@ readonly SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" # shellcheck source=_common.sh . "$SCRIPT_DIR/_common.sh" -function main { +####################################### +# main function +# Globals: +# ARGS +# FILES +# SCRIPT_DIR +# Arguments: +# None +####################################### +main() { common::initialize "$SCRIPT_DIR" common::parse_cmdline "$@" # Support for setting PATH to repo root. @@ -26,7 +35,7 @@ function main { # Outputs: # If failed - print out hook checks status ####################################################################### -function per_dir_hook_unique_part { +per_dir_hook_unique_part() { local -r args="$1" # shellcheck disable=SC2034 # Unused var. local -r dir_path="$2" @@ -40,4 +49,6 @@ function per_dir_hook_unique_part { return $exit_code } + + [ "${BASH_SOURCE[0]}" != "$0" ] || main "$@" diff --git a/hooks/terraform_validate.sh b/hooks/terraform_validate.sh index a123d5fbd..a85fff0da 100755 --- a/hooks/terraform_validate.sh +++ b/hooks/terraform_validate.sh @@ -6,15 +6,22 @@ readonly SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" # shellcheck source=_common.sh . "$SCRIPT_DIR/_common.sh" -# `terraform validate` requires this env variable to be set -export AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION:-us-east-1} - -function main { +####################################### +# main function +# Globals: +# SCRIPT_DIR +# Arguments: +# None +####################################### +main() { common::initialize "$SCRIPT_DIR" parse_cmdline_ "$@" terraform_validate_ } +# `terraform validate` requires this env variable to be set +export AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION:-us-east-1} + ####################################################################### # Parse args and filenames passed to script and populate respective # global variables with appropriate values @@ -28,7 +35,7 @@ function main { # $@ (array) all specified in `hooks.[].args` in # `.pre-commit-config.yaml` and filenames. ####################################################################### -function parse_cmdline_ { +parse_cmdline_() { declare argv argv=$(getopt -o e:i:a: --long envs:,init-args:,args: -- "$@") || return eval "set -- $argv" @@ -77,7 +84,7 @@ function parse_cmdline_ { # Outputs: # If failed - print out hook checks status ####################################################################### -function terraform_validate_ { +terraform_validate_() { # Setup environment variables local var var_name var_value @@ -144,6 +151,8 @@ function terraform_validate_ { fi } + + # global arrays declare -a INIT_ARGS declare -a ENVS diff --git a/hooks/terragrunt_fmt.sh b/hooks/terragrunt_fmt.sh index c750241df..07235a6ce 100755 --- a/hooks/terragrunt_fmt.sh +++ b/hooks/terragrunt_fmt.sh @@ -6,7 +6,16 @@ readonly SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" # shellcheck source=_common.sh . "$SCRIPT_DIR/_common.sh" -function main { +####################################### +# main function +# Globals: +# ARGS +# FILES +# SCRIPT_DIR +# Arguments: +# None +####################################### +main() { common::initialize "$SCRIPT_DIR" common::parse_cmdline "$@" # shellcheck disable=SC2153 # False positive @@ -23,7 +32,7 @@ function main { # Outputs: # If failed - print out hook checks status ####################################################################### -function per_dir_hook_unique_part { +per_dir_hook_unique_part() { local -r args="$1" # shellcheck disable=SC2034 # Unused var. local -r dir_path="$2" diff --git a/hooks/terragrunt_validate.sh b/hooks/terragrunt_validate.sh index e68497eea..bd385fe0b 100755 --- a/hooks/terragrunt_validate.sh +++ b/hooks/terragrunt_validate.sh @@ -6,7 +6,16 @@ readonly SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" # shellcheck source=_common.sh . "$SCRIPT_DIR/_common.sh" -function main { +####################################### +# main function +# Globals: +# ARGS +# FILES +# SCRIPT_DIR +# Arguments: +# None +####################################### +main() { common::initialize "$SCRIPT_DIR" common::parse_cmdline "$@" # shellcheck disable=SC2153 # False positive @@ -23,7 +32,7 @@ function main { # Outputs: # If failed - print out hook checks status ####################################################################### -function per_dir_hook_unique_part { +per_dir_hook_unique_part() { local -r args="$1" # shellcheck disable=SC2034 # Unused var. local -r dir_path="$2" diff --git a/hooks/terrascan.sh b/hooks/terrascan.sh index 31315aadd..59a50b20f 100755 --- a/hooks/terrascan.sh +++ b/hooks/terrascan.sh @@ -6,7 +6,16 @@ readonly SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" # shellcheck source=_common.sh . "$SCRIPT_DIR/_common.sh" -function main { +####################################### +# main function +# Globals: +# ARGS +# FILES +# SCRIPT_DIR +# Arguments: +# None +####################################### +main() { common::initialize "$SCRIPT_DIR" common::parse_cmdline "$@" # shellcheck disable=SC2153 # False positive @@ -23,7 +32,7 @@ function main { # Outputs: # If failed - print out hook checks status ####################################################################### -function per_dir_hook_unique_part { +per_dir_hook_unique_part() { local -r args="$1" # shellcheck disable=SC2034 # Unused var. local -r dir_path="$2" diff --git a/tests/hooks_performance_test.sh b/tests/hooks_performance_test.sh index 4f35fce23..b06033a94 100755 --- a/tests/hooks_performance_test.sh +++ b/tests/hooks_performance_test.sh @@ -6,7 +6,7 @@ TEST_DIR=$3 # '/tmp/infrastructure' TEST_DESCRIPTION="$TEST_NUM runs '$4'" # '`terraform_tfsec` PR #123:' RAW_TEST_RESULTS_FILE_NAME=$5 # terraform_tfsec_pr123 -function run_tests { +run_tests() { local TEST_NUM=$1 local TEST_DIR=$2 local TEST_COMMAND @@ -31,7 +31,7 @@ function run_tests { cd - > /dev/null } -function generate_table { +generate_table() { local FILE_PATH="tests/results/$1" local users_seconds system_seconds cpu total_time @@ -74,7 +74,7 @@ function generate_table { " } -function save_result { +save_result() { local DESCRIPTION=$1 local TABLE=$2 local TEST_RUN_START_TIME=$3