mirror of
https://github.com/tofuutils/pre-commit-opentofu.git
synced 2025-10-15 17:38:54 +02:00
chore: Improved code structure (moved hooks into a separate dir) (#316)
This commit is contained in:
parent
3045dd55a3
commit
c5f2a618a8
27 changed files with 1066 additions and 1591 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
- id: infracost_breakdown
|
- id: infracost_breakdown
|
||||||
name: Infracost breakdown
|
name: Infracost breakdown
|
||||||
description: Check terraform infrastructure cost
|
description: Check terraform infrastructure cost
|
||||||
entry: infracost_breakdown.sh
|
entry: hooks/infracost_breakdown.sh
|
||||||
language: script
|
language: script
|
||||||
require_serial: true
|
require_serial: true
|
||||||
files: \.(tf(vars)?|hcl)$
|
files: \.(tf(vars)?|hcl)$
|
||||||
|
|
@ -10,7 +10,7 @@
|
||||||
- id: terraform_fmt
|
- id: terraform_fmt
|
||||||
name: Terraform fmt
|
name: Terraform fmt
|
||||||
description: Rewrites all Terraform configuration files to a canonical format.
|
description: Rewrites all Terraform configuration files to a canonical format.
|
||||||
entry: terraform_fmt.sh
|
entry: hooks/terraform_fmt.sh
|
||||||
language: script
|
language: script
|
||||||
files: (\.tf|\.tfvars)$
|
files: (\.tf|\.tfvars)$
|
||||||
exclude: \.terraform\/.*$
|
exclude: \.terraform\/.*$
|
||||||
|
|
@ -19,7 +19,7 @@
|
||||||
name: Terraform docs
|
name: Terraform docs
|
||||||
description: Inserts input and output documentation into README.md (using terraform-docs).
|
description: Inserts input and output documentation into README.md (using terraform-docs).
|
||||||
require_serial: true
|
require_serial: true
|
||||||
entry: terraform_docs.sh
|
entry: hooks/terraform_docs.sh
|
||||||
language: script
|
language: script
|
||||||
files: (\.tf|\.terraform\.lock\.hcl)$
|
files: (\.tf|\.terraform\.lock\.hcl)$
|
||||||
exclude: \.terraform\/.*$
|
exclude: \.terraform\/.*$
|
||||||
|
|
@ -28,7 +28,7 @@
|
||||||
name: Terraform docs (without aggregate type defaults)
|
name: Terraform docs (without aggregate type defaults)
|
||||||
description: Inserts input and output documentation into README.md (using terraform-docs). Identical to terraform_docs.
|
description: Inserts input and output documentation into README.md (using terraform-docs). Identical to terraform_docs.
|
||||||
require_serial: true
|
require_serial: true
|
||||||
entry: terraform_docs.sh
|
entry: hooks/terraform_docs.sh
|
||||||
language: script
|
language: script
|
||||||
files: (\.tf)$
|
files: (\.tf)$
|
||||||
exclude: \.terraform\/.*$
|
exclude: \.terraform\/.*$
|
||||||
|
|
@ -46,7 +46,7 @@
|
||||||
name: Terraform validate
|
name: Terraform validate
|
||||||
description: Validates all Terraform configuration files.
|
description: Validates all Terraform configuration files.
|
||||||
require_serial: true
|
require_serial: true
|
||||||
entry: terraform_validate.sh
|
entry: hooks/terraform_validate.sh
|
||||||
language: script
|
language: script
|
||||||
files: (\.tf|\.tfvars)$
|
files: (\.tf|\.tfvars)$
|
||||||
exclude: \.terraform\/.*$
|
exclude: \.terraform\/.*$
|
||||||
|
|
@ -55,7 +55,7 @@
|
||||||
name: Lock terraform provider versions
|
name: Lock terraform provider versions
|
||||||
description: Updates provider signatures in dependency lock files.
|
description: Updates provider signatures in dependency lock files.
|
||||||
require_serial: true
|
require_serial: true
|
||||||
entry: terraform_providers_lock.sh
|
entry: hooks/terraform_providers_lock.sh
|
||||||
language: script
|
language: script
|
||||||
files: (\.terraform\.lock\.hcl)$
|
files: (\.terraform\.lock\.hcl)$
|
||||||
exclude: \.terraform\/.*$
|
exclude: \.terraform\/.*$
|
||||||
|
|
@ -64,7 +64,7 @@
|
||||||
name: Terraform validate with tflint
|
name: Terraform validate with tflint
|
||||||
description: Validates all Terraform configuration files with TFLint.
|
description: Validates all Terraform configuration files with TFLint.
|
||||||
require_serial: true
|
require_serial: true
|
||||||
entry: terraform_tflint.sh
|
entry: hooks/terraform_tflint.sh
|
||||||
language: script
|
language: script
|
||||||
files: (\.tf|\.tfvars)$
|
files: (\.tf|\.tfvars)$
|
||||||
exclude: \.terraform\/.*$
|
exclude: \.terraform\/.*$
|
||||||
|
|
@ -72,7 +72,7 @@
|
||||||
- id: terragrunt_fmt
|
- id: terragrunt_fmt
|
||||||
name: Terragrunt fmt
|
name: Terragrunt fmt
|
||||||
description: Rewrites all Terragrunt configuration files to a canonical format.
|
description: Rewrites all Terragrunt configuration files to a canonical format.
|
||||||
entry: terragrunt_fmt.sh
|
entry: hooks/terragrunt_fmt.sh
|
||||||
language: script
|
language: script
|
||||||
files: (\.hcl)$
|
files: (\.hcl)$
|
||||||
exclude: \.terraform\/.*$
|
exclude: \.terraform\/.*$
|
||||||
|
|
@ -80,7 +80,7 @@
|
||||||
- id: terragrunt_validate
|
- id: terragrunt_validate
|
||||||
name: Terragrunt validate
|
name: Terragrunt validate
|
||||||
description: Validates all Terragrunt configuration files.
|
description: Validates all Terragrunt configuration files.
|
||||||
entry: terragrunt_validate.sh
|
entry: hooks/terragrunt_validate.sh
|
||||||
language: script
|
language: script
|
||||||
files: (\.hcl)$
|
files: (\.hcl)$
|
||||||
exclude: \.terraform\/.*$
|
exclude: \.terraform\/.*$
|
||||||
|
|
@ -89,13 +89,13 @@
|
||||||
name: Terraform validate with tfsec
|
name: Terraform validate with tfsec
|
||||||
description: Static analysis of Terraform templates to spot potential security issues.
|
description: Static analysis of Terraform templates to spot potential security issues.
|
||||||
require_serial: true
|
require_serial: true
|
||||||
entry: terraform_tfsec.sh
|
entry: hooks/terraform_tfsec.sh
|
||||||
language: script
|
language: script
|
||||||
|
|
||||||
- id: checkov
|
- id: checkov
|
||||||
name: Checkov
|
name: Checkov
|
||||||
description: Runs checkov on Terraform templates.
|
description: Runs checkov on Terraform templates.
|
||||||
entry: checkov -d .
|
entry: hooks/checkov -d .
|
||||||
language: python
|
language: python
|
||||||
pass_filenames: false
|
pass_filenames: false
|
||||||
always_run: false
|
always_run: false
|
||||||
|
|
@ -107,7 +107,7 @@
|
||||||
name: terrascan
|
name: terrascan
|
||||||
description: Runs terrascan on Terraform templates.
|
description: Runs terrascan on Terraform templates.
|
||||||
language: script
|
language: script
|
||||||
entry: terrascan.sh
|
entry: hooks/terrascan.sh
|
||||||
files: \.tf$
|
files: \.tf$
|
||||||
exclude: \.terraform\/.*$
|
exclude: \.terraform\/.*$
|
||||||
require_serial: true
|
require_serial: true
|
||||||
|
|
|
||||||
46
terragrunt_fmt.sh → hooks/_common.sh
Executable file → Normal file
46
terragrunt_fmt.sh → hooks/_common.sh
Executable file → Normal file
|
|
@ -1,26 +1,17 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -eo pipefail
|
set -eo pipefail
|
||||||
|
|
||||||
function main {
|
|
||||||
common::initialize
|
|
||||||
common::parse_cmdline "$@"
|
|
||||||
common::per_dir_hook "${ARGS[*]}" "${FILES[@]}"
|
|
||||||
}
|
|
||||||
|
|
||||||
function common::initialize {
|
function common::initialize {
|
||||||
local SCRIPT_DIR
|
local -r script_dir=$1
|
||||||
# get directory containing this script
|
|
||||||
SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
|
|
||||||
|
|
||||||
# source getopt function
|
# source getopt function
|
||||||
# shellcheck source=lib_getopt
|
# shellcheck source=lib_getopt
|
||||||
. "$SCRIPT_DIR/lib_getopt"
|
. "$script_dir/../lib_getopt"
|
||||||
}
|
}
|
||||||
|
|
||||||
function common::parse_cmdline {
|
function common::parse_cmdline {
|
||||||
# common global arrays.
|
# common global arrays.
|
||||||
# Populated via `common::parse_cmdline` and can be used inside hooks' functions
|
# Populated via `common::parse_cmdline` and can be used inside hooks' functions
|
||||||
declare -g -a ARGS=() FILES=() HOOK_CONFIG=()
|
declare -g -a ARGS=() HOOK_CONFIG=() FILES=()
|
||||||
|
|
||||||
local argv
|
local argv
|
||||||
argv=$(getopt -o a:,h: --long args:,hook-config: -- "$@") || return
|
argv=$(getopt -o a:,h: --long args:,hook-config: -- "$@") || return
|
||||||
|
|
@ -40,6 +31,7 @@ function common::parse_cmdline {
|
||||||
;;
|
;;
|
||||||
--)
|
--)
|
||||||
shift
|
shift
|
||||||
|
# shellcheck disable=SC2034 # Variable is used
|
||||||
FILES=("$@")
|
FILES=("$@")
|
||||||
break
|
break
|
||||||
;;
|
;;
|
||||||
|
|
@ -90,18 +82,24 @@ function common::per_dir_hook {
|
||||||
exit $final_exit_code
|
exit $final_exit_code
|
||||||
}
|
}
|
||||||
|
|
||||||
function per_dir_hook_unique_part {
|
function common::colorify {
|
||||||
# common logic located in common::per_dir_hook
|
# shellcheck disable=SC2034
|
||||||
local -r args="$1"
|
local -r red="\e[0m\e[31m"
|
||||||
local -r dir_path="$2"
|
# shellcheck disable=SC2034
|
||||||
|
local -r green="\e[0m\e[32m"
|
||||||
|
# shellcheck disable=SC2034
|
||||||
|
local -r yellow="\e[0m\e[33m"
|
||||||
|
# Color reset
|
||||||
|
local -r RESET="\e[0m"
|
||||||
|
|
||||||
# pass the arguments to hook
|
# Params start #
|
||||||
# shellcheck disable=SC2068 # hook fails when quoting is used ("$arg[@]")
|
local COLOR="${!1}"
|
||||||
terragrunt hclfmt ${args[@]}
|
local -r TEXT=$2
|
||||||
|
# Params end #
|
||||||
|
|
||||||
# return exit code to common::per_dir_hook
|
if [ "$PRE_COMMIT_COLOR" = "never" ]; then
|
||||||
local exit_code=$?
|
COLOR=$RESET
|
||||||
return $exit_code
|
fi
|
||||||
|
|
||||||
|
echo -e "${COLOR}${TEXT}${RESET}"
|
||||||
}
|
}
|
||||||
|
|
||||||
[ "${BASH_SOURCE[0]}" != "$0" ] || main "$@"
|
|
||||||
|
|
@ -1,75 +1,18 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -eo pipefail
|
set -eo pipefail
|
||||||
|
|
||||||
|
# shellcheck disable=SC2155 # No way to assign to readonly variable in separate lines
|
||||||
|
readonly SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
|
||||||
|
# shellcheck source=_common.sh
|
||||||
|
. "$SCRIPT_DIR/_common.sh"
|
||||||
|
|
||||||
function main {
|
function main {
|
||||||
common::initialize
|
common::initialize "$SCRIPT_DIR"
|
||||||
common::parse_cmdline "$@"
|
common::parse_cmdline "$@"
|
||||||
|
# shellcheck disable=SC2153 # False positive
|
||||||
infracost_breakdown_ "${HOOK_CONFIG[*]}" "${ARGS[*]}"
|
infracost_breakdown_ "${HOOK_CONFIG[*]}" "${ARGS[*]}"
|
||||||
}
|
}
|
||||||
|
|
||||||
function common::colorify {
|
|
||||||
# shellcheck disable=SC2034
|
|
||||||
local -r red="\e[0m\e[31m"
|
|
||||||
# shellcheck disable=SC2034
|
|
||||||
local -r green="\e[0m\e[32m"
|
|
||||||
# shellcheck disable=SC2034
|
|
||||||
local -r yellow="\e[0m\e[33m"
|
|
||||||
# Color reset
|
|
||||||
local -r RESET="\e[0m"
|
|
||||||
|
|
||||||
# Params start #
|
|
||||||
local COLOR="${!1}"
|
|
||||||
local -r TEXT=$2
|
|
||||||
# Params end #
|
|
||||||
|
|
||||||
if [ "$PRE_COMMIT_COLOR" = "never" ]; then
|
|
||||||
COLOR=$RESET
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo -e "${COLOR}${TEXT}${RESET}"
|
|
||||||
}
|
|
||||||
|
|
||||||
function common::initialize {
|
|
||||||
local SCRIPT_DIR
|
|
||||||
# get directory containing this script
|
|
||||||
SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
|
|
||||||
|
|
||||||
# source getopt function
|
|
||||||
# shellcheck source=lib_getopt
|
|
||||||
. "$SCRIPT_DIR/lib_getopt"
|
|
||||||
}
|
|
||||||
|
|
||||||
function common::parse_cmdline {
|
|
||||||
# common global arrays.
|
|
||||||
# Populated via `common::parse_cmdline` and can be used inside hooks' functions
|
|
||||||
declare -g -a ARGS=() FILES=() HOOK_CONFIG=()
|
|
||||||
|
|
||||||
local argv
|
|
||||||
argv=$(getopt -o a:,h: --long args:,hook-config: -- "$@") || return
|
|
||||||
eval "set -- $argv"
|
|
||||||
|
|
||||||
for argv; do
|
|
||||||
case $argv in
|
|
||||||
-a | --args)
|
|
||||||
shift
|
|
||||||
ARGS+=("$1")
|
|
||||||
shift
|
|
||||||
;;
|
|
||||||
-h | --hook-config)
|
|
||||||
shift
|
|
||||||
HOOK_CONFIG+=("$1;")
|
|
||||||
shift
|
|
||||||
;;
|
|
||||||
--)
|
|
||||||
shift
|
|
||||||
# shellcheck disable=SC2034 # Common function
|
|
||||||
FILES=("$@")
|
|
||||||
break
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
function infracost_breakdown_ {
|
function infracost_breakdown_ {
|
||||||
local -r hook_config="$1"
|
local -r hook_config="$1"
|
||||||
local args
|
local args
|
||||||
|
|
@ -1,54 +1,22 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -eo pipefail
|
set -eo pipefail
|
||||||
|
|
||||||
|
# shellcheck disable=SC2155 # No way to assign to readonly variable in separate lines
|
||||||
|
readonly SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
|
||||||
|
# shellcheck source=_common.sh
|
||||||
|
. "$SCRIPT_DIR/_common.sh"
|
||||||
|
|
||||||
function main {
|
function main {
|
||||||
common::initialize
|
common::initialize "$SCRIPT_DIR"
|
||||||
common::parse_cmdline "$@"
|
common::parse_cmdline "$@"
|
||||||
# Support for setting relative PATH to .terraform-docs.yml config.
|
# Support for setting relative PATH to .terraform-docs.yml config.
|
||||||
|
# shellcheck disable=SC2178 # It's the simplest syntax for that case
|
||||||
ARGS=${ARGS[*]/--config=/--config=$(pwd)\/}
|
ARGS=${ARGS[*]/--config=/--config=$(pwd)\/}
|
||||||
|
# shellcheck disable=SC2128 # It's the simplest syntax for that case
|
||||||
|
# shellcheck disable=SC2153 # False positive
|
||||||
terraform_docs_ "${HOOK_CONFIG[*]}" "$ARGS" "${FILES[@]}"
|
terraform_docs_ "${HOOK_CONFIG[*]}" "$ARGS" "${FILES[@]}"
|
||||||
}
|
}
|
||||||
|
|
||||||
function common::initialize {
|
|
||||||
local SCRIPT_DIR
|
|
||||||
# get directory containing this script
|
|
||||||
SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
|
|
||||||
|
|
||||||
# source getopt function
|
|
||||||
# shellcheck source=lib_getopt
|
|
||||||
. "$SCRIPT_DIR/lib_getopt"
|
|
||||||
}
|
|
||||||
|
|
||||||
function common::parse_cmdline {
|
|
||||||
# common global arrays.
|
|
||||||
# Populated via `common::parse_cmdline` and can be used inside hooks' functions
|
|
||||||
declare -g -a ARGS=() FILES=() HOOK_CONFIG=()
|
|
||||||
|
|
||||||
local argv
|
|
||||||
argv=$(getopt -o a:,h: --long args:,hook-config: -- "$@") || return
|
|
||||||
eval "set -- $argv"
|
|
||||||
|
|
||||||
for argv; do
|
|
||||||
case $argv in
|
|
||||||
-a | --args)
|
|
||||||
shift
|
|
||||||
ARGS+=("$1")
|
|
||||||
shift
|
|
||||||
;;
|
|
||||||
-h | --hook-config)
|
|
||||||
shift
|
|
||||||
HOOK_CONFIG+=("$1;")
|
|
||||||
shift
|
|
||||||
;;
|
|
||||||
--)
|
|
||||||
shift
|
|
||||||
FILES=("$@")
|
|
||||||
break
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
function terraform_docs_ {
|
function terraform_docs_ {
|
||||||
local -r hook_config="$1"
|
local -r hook_config="$1"
|
||||||
local -r args="$2"
|
local -r args="$2"
|
||||||
|
|
@ -1,52 +1,18 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -eo pipefail
|
set -eo pipefail
|
||||||
|
|
||||||
|
# shellcheck disable=SC2155 # No way to assign to readonly variable in separate lines
|
||||||
|
readonly SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
|
||||||
|
# shellcheck source=_common.sh
|
||||||
|
. "$SCRIPT_DIR/_common.sh"
|
||||||
|
|
||||||
function main {
|
function main {
|
||||||
common::initialize
|
common::initialize "$SCRIPT_DIR"
|
||||||
common::parse_cmdline "$@"
|
common::parse_cmdline "$@"
|
||||||
|
# shellcheck disable=SC2153 # False positive
|
||||||
terraform_fmt_ "${ARGS[*]}" "${FILES[@]}"
|
terraform_fmt_ "${ARGS[*]}" "${FILES[@]}"
|
||||||
}
|
}
|
||||||
|
|
||||||
function common::initialize {
|
|
||||||
local SCRIPT_DIR
|
|
||||||
# get directory containing this script
|
|
||||||
SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
|
|
||||||
|
|
||||||
# source getopt function
|
|
||||||
# shellcheck source=lib_getopt
|
|
||||||
. "$SCRIPT_DIR/lib_getopt"
|
|
||||||
}
|
|
||||||
|
|
||||||
function common::parse_cmdline {
|
|
||||||
# common global arrays.
|
|
||||||
# Populated via `common::parse_cmdline` and can be used inside hooks' functions
|
|
||||||
declare -g -a ARGS=() FILES=() HOOK_CONFIG=()
|
|
||||||
|
|
||||||
local argv
|
|
||||||
argv=$(getopt -o a:,h: --long args:,hook-config: -- "$@") || return
|
|
||||||
eval "set -- $argv"
|
|
||||||
|
|
||||||
for argv; do
|
|
||||||
case $argv in
|
|
||||||
-a | --args)
|
|
||||||
shift
|
|
||||||
ARGS+=("$1")
|
|
||||||
shift
|
|
||||||
;;
|
|
||||||
-h | --hook-config)
|
|
||||||
shift
|
|
||||||
HOOK_CONFIG+=("$1;")
|
|
||||||
shift
|
|
||||||
;;
|
|
||||||
--)
|
|
||||||
shift
|
|
||||||
FILES=("$@")
|
|
||||||
break
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
function terraform_fmt_ {
|
function terraform_fmt_ {
|
||||||
local -r args="$1"
|
local -r args="$1"
|
||||||
shift 1
|
shift 1
|
||||||
42
hooks/terraform_providers_lock.sh
Executable file
42
hooks/terraform_providers_lock.sh
Executable file
|
|
@ -0,0 +1,42 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
set -eo pipefail
|
||||||
|
|
||||||
|
# shellcheck disable=SC2155 # No way to assign to readonly variable in separate lines
|
||||||
|
readonly SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
|
||||||
|
# shellcheck source=_common.sh
|
||||||
|
. "$SCRIPT_DIR/_common.sh"
|
||||||
|
|
||||||
|
function main {
|
||||||
|
common::initialize "$SCRIPT_DIR"
|
||||||
|
common::parse_cmdline "$@"
|
||||||
|
# shellcheck disable=SC2153 # False positive
|
||||||
|
common::per_dir_hook "${ARGS[*]}" "${FILES[@]}"
|
||||||
|
}
|
||||||
|
|
||||||
|
function per_dir_hook_unique_part {
|
||||||
|
# common logic located in common::per_dir_hook
|
||||||
|
local -r args="$1"
|
||||||
|
local -r dir_path="$2"
|
||||||
|
|
||||||
|
if [ ! -d ".terraform" ]; then
|
||||||
|
init_output=$(terraform init -backend=false 2>&1)
|
||||||
|
init_code=$?
|
||||||
|
|
||||||
|
if [ $init_code -ne 0 ]; then
|
||||||
|
common::colorify "red" "Init before validation failed: $dir_path"
|
||||||
|
common::colorify "red" "$init_output"
|
||||||
|
exit $init_code
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# pass the arguments to hook
|
||||||
|
# shellcheck disable=SC2068 # hook fails when quoting is used ("$arg[@]")
|
||||||
|
terraform providers lock ${args[@]}
|
||||||
|
|
||||||
|
# return exit code to common::per_dir_hook
|
||||||
|
local exit_code=$?
|
||||||
|
return $exit_code
|
||||||
|
}
|
||||||
|
|
||||||
|
[ "${BASH_SOURCE[0]}" != "$0" ] || main "$@"
|
||||||
39
hooks/terraform_tflint.sh
Executable file
39
hooks/terraform_tflint.sh
Executable file
|
|
@ -0,0 +1,39 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
set -eo pipefail
|
||||||
|
|
||||||
|
# shellcheck disable=SC2155 # No way to assign to readonly variable in separate lines
|
||||||
|
readonly SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
|
||||||
|
# shellcheck source=_common.sh
|
||||||
|
. "$SCRIPT_DIR/_common.sh"
|
||||||
|
|
||||||
|
function main {
|
||||||
|
common::initialize "$SCRIPT_DIR"
|
||||||
|
common::parse_cmdline "$@"
|
||||||
|
# Support for setting PATH to repo root.
|
||||||
|
# shellcheck disable=SC2178 # It's the simplest syntax for that case
|
||||||
|
ARGS=${ARGS[*]/__GIT_WORKING_DIR__/$(pwd)\/}
|
||||||
|
# shellcheck disable=SC2128 # It's the simplest syntax for that case
|
||||||
|
common::per_dir_hook "$ARGS" "${FILES[@]}"
|
||||||
|
}
|
||||||
|
|
||||||
|
function per_dir_hook_unique_part {
|
||||||
|
# common logic located in common::per_dir_hook
|
||||||
|
local -r args="$1"
|
||||||
|
local -r dir_path="$2"
|
||||||
|
|
||||||
|
# Print checked PATH **only** if TFLint have any messages
|
||||||
|
# shellcheck disable=SC2091,SC2068 # Suppress error output
|
||||||
|
$(tflint ${args[@]} 2>&1) 2> /dev/null || {
|
||||||
|
common::colorify "yellow" "TFLint in $dir_path/:"
|
||||||
|
|
||||||
|
# shellcheck disable=SC2068 # hook fails when quoting is used ("$arg[@]")
|
||||||
|
tflint ${args[@]}
|
||||||
|
}
|
||||||
|
|
||||||
|
# return exit code to common::per_dir_hook
|
||||||
|
local exit_code=$?
|
||||||
|
return $exit_code
|
||||||
|
}
|
||||||
|
|
||||||
|
[ "${BASH_SOURCE[0]}" != "$0" ] || main "$@"
|
||||||
34
hooks/terraform_tfsec.sh
Executable file
34
hooks/terraform_tfsec.sh
Executable file
|
|
@ -0,0 +1,34 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
set -eo pipefail
|
||||||
|
|
||||||
|
# shellcheck disable=SC2155 # No way to assign to readonly variable in separate lines
|
||||||
|
readonly SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
|
||||||
|
# shellcheck source=_common.sh
|
||||||
|
. "$SCRIPT_DIR/_common.sh"
|
||||||
|
|
||||||
|
function main {
|
||||||
|
common::initialize "$SCRIPT_DIR"
|
||||||
|
common::parse_cmdline "$@"
|
||||||
|
# Support for setting PATH to repo root.
|
||||||
|
# shellcheck disable=SC2178 # It's the simplest syntax for that case
|
||||||
|
ARGS=${ARGS[*]/__GIT_WORKING_DIR__/$(pwd)\/}
|
||||||
|
# shellcheck disable=SC2128 # It's the simplest syntax for that case
|
||||||
|
common::per_dir_hook "$ARGS" "${FILES[@]}"
|
||||||
|
}
|
||||||
|
|
||||||
|
function per_dir_hook_unique_part {
|
||||||
|
# common logic located in common::per_dir_hook
|
||||||
|
local -r args="$1"
|
||||||
|
# shellcheck disable=SC2034 # Unused var.
|
||||||
|
local -r dir_path="$2"
|
||||||
|
|
||||||
|
# pass the arguments to hook
|
||||||
|
# shellcheck disable=SC2068 # hook fails when quoting is used ("$arg[@]")
|
||||||
|
tfsec ${args[@]}
|
||||||
|
|
||||||
|
# return exit code to common::per_dir_hook
|
||||||
|
local exit_code=$?
|
||||||
|
return $exit_code
|
||||||
|
}
|
||||||
|
|
||||||
|
[ "${BASH_SOURCE[0]}" != "$0" ] || main "$@"
|
||||||
|
|
@ -1,25 +1,20 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -eo pipefail
|
set -eo pipefail
|
||||||
|
|
||||||
|
# shellcheck disable=SC2155 # No way to assign to readonly variable in separate lines
|
||||||
|
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
|
# `terraform validate` requires this env variable to be set
|
||||||
export AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION:-us-east-1}
|
export AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION:-us-east-1}
|
||||||
|
|
||||||
function main {
|
function main {
|
||||||
common::initialize
|
common::initialize "$SCRIPT_DIR"
|
||||||
parse_cmdline_ "$@"
|
parse_cmdline_ "$@"
|
||||||
terraform_validate_
|
terraform_validate_
|
||||||
}
|
}
|
||||||
|
|
||||||
function common::initialize {
|
|
||||||
local SCRIPT_DIR
|
|
||||||
# get directory containing this script
|
|
||||||
SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
|
|
||||||
|
|
||||||
# source getopt function
|
|
||||||
# shellcheck source=lib_getopt
|
|
||||||
. "$SCRIPT_DIR/lib_getopt"
|
|
||||||
}
|
|
||||||
|
|
||||||
function parse_cmdline_ {
|
function parse_cmdline_ {
|
||||||
declare argv
|
declare argv
|
||||||
argv=$(getopt -o e:i:a: --long envs:,init-args:,args: -- "$@") || return
|
argv=$(getopt -o e:i:a: --long envs:,init-args:,args: -- "$@") || return
|
||||||
|
|
@ -119,9 +114,7 @@ function terraform_validate_ {
|
||||||
}
|
}
|
||||||
|
|
||||||
# global arrays
|
# global arrays
|
||||||
declare -a ARGS
|
|
||||||
declare -a INIT_ARGS
|
declare -a INIT_ARGS
|
||||||
declare -a ENVS
|
declare -a ENVS
|
||||||
declare -a FILES
|
|
||||||
|
|
||||||
[ "${BASH_SOURCE[0]}" != "$0" ] || main "$@"
|
[ "${BASH_SOURCE[0]}" != "$0" ] || main "$@"
|
||||||
31
hooks/terragrunt_fmt.sh
Executable file
31
hooks/terragrunt_fmt.sh
Executable file
|
|
@ -0,0 +1,31 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
set -eo pipefail
|
||||||
|
|
||||||
|
# shellcheck disable=SC2155 # No way to assign to readonly variable in separate lines
|
||||||
|
readonly SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
|
||||||
|
# shellcheck source=_common.sh
|
||||||
|
. "$SCRIPT_DIR/_common.sh"
|
||||||
|
|
||||||
|
function main {
|
||||||
|
common::initialize "$SCRIPT_DIR"
|
||||||
|
common::parse_cmdline "$@"
|
||||||
|
# shellcheck disable=SC2153 # False positive
|
||||||
|
common::per_dir_hook "${ARGS[*]}" "${FILES[@]}"
|
||||||
|
}
|
||||||
|
|
||||||
|
function per_dir_hook_unique_part {
|
||||||
|
# common logic located in common::per_dir_hook
|
||||||
|
local -r args="$1"
|
||||||
|
# shellcheck disable=SC2034 # Unused var.
|
||||||
|
local -r dir_path="$2"
|
||||||
|
|
||||||
|
# pass the arguments to hook
|
||||||
|
# shellcheck disable=SC2068 # hook fails when quoting is used ("$arg[@]")
|
||||||
|
terragrunt hclfmt ${args[@]}
|
||||||
|
|
||||||
|
# return exit code to common::per_dir_hook
|
||||||
|
local exit_code=$?
|
||||||
|
return $exit_code
|
||||||
|
}
|
||||||
|
|
||||||
|
[ "${BASH_SOURCE[0]}" != "$0" ] || main "$@"
|
||||||
31
hooks/terragrunt_validate.sh
Executable file
31
hooks/terragrunt_validate.sh
Executable file
|
|
@ -0,0 +1,31 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
set -eo pipefail
|
||||||
|
|
||||||
|
# shellcheck disable=SC2155 # No way to assign to readonly variable in separate lines
|
||||||
|
readonly SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
|
||||||
|
# shellcheck source=_common.sh
|
||||||
|
. "$SCRIPT_DIR/_common.sh"
|
||||||
|
|
||||||
|
function main {
|
||||||
|
common::initialize "$SCRIPT_DIR"
|
||||||
|
common::parse_cmdline "$@"
|
||||||
|
# shellcheck disable=SC2153 # False positive
|
||||||
|
common::per_dir_hook "${ARGS[*]}" "${FILES[@]}"
|
||||||
|
}
|
||||||
|
|
||||||
|
function per_dir_hook_unique_part {
|
||||||
|
# common logic located in common::per_dir_hook
|
||||||
|
local -r args="$1"
|
||||||
|
# shellcheck disable=SC2034 # Unused var.
|
||||||
|
local -r dir_path="$2"
|
||||||
|
|
||||||
|
# pass the arguments to hook
|
||||||
|
# shellcheck disable=SC2068 # hook fails when quoting is used ("$arg[@]")
|
||||||
|
terragrunt validate ${args[@]}
|
||||||
|
|
||||||
|
# return exit code to common::per_dir_hook
|
||||||
|
local exit_code=$?
|
||||||
|
return $exit_code
|
||||||
|
}
|
||||||
|
|
||||||
|
[ "${BASH_SOURCE[0]}" != "$0" ] || main "$@"
|
||||||
31
hooks/terrascan.sh
Executable file
31
hooks/terrascan.sh
Executable file
|
|
@ -0,0 +1,31 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
set -eo pipefail
|
||||||
|
|
||||||
|
# shellcheck disable=SC2155 # No way to assign to readonly variable in separate lines
|
||||||
|
readonly SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
|
||||||
|
# shellcheck source=_common.sh
|
||||||
|
. "$SCRIPT_DIR/_common.sh"
|
||||||
|
|
||||||
|
function main {
|
||||||
|
common::initialize "$SCRIPT_DIR"
|
||||||
|
common::parse_cmdline "$@"
|
||||||
|
# shellcheck disable=SC2153 # False positive
|
||||||
|
common::per_dir_hook "${ARGS[*]}" "${FILES[@]}"
|
||||||
|
}
|
||||||
|
|
||||||
|
function per_dir_hook_unique_part {
|
||||||
|
# common logic located in common::per_dir_hook
|
||||||
|
local -r args="$1"
|
||||||
|
# shellcheck disable=SC2034 # Unused var.
|
||||||
|
local -r dir_path="$2"
|
||||||
|
|
||||||
|
# pass the arguments to hook
|
||||||
|
# shellcheck disable=SC2068 # hook fails when quoting is used ("$arg[@]")
|
||||||
|
terrascan scan -i terraform ${args[@]}
|
||||||
|
|
||||||
|
# return exit code to common::per_dir_hook
|
||||||
|
local exit_code=$?
|
||||||
|
return $exit_code
|
||||||
|
}
|
||||||
|
|
||||||
|
[ "${BASH_SOURCE[0]}" != "$0" ] || main "$@"
|
||||||
2
setup.py
2
setup.py
|
|
@ -27,7 +27,7 @@ setup(
|
||||||
],
|
],
|
||||||
entry_points={
|
entry_points={
|
||||||
'console_scripts': [
|
'console_scripts': [
|
||||||
'terraform_docs_replace = pre_commit_hooks.terraform_docs_replace:main',
|
'terraform_docs_replace = hooks.terraform_docs_replace:main',
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -1,141 +0,0 @@
|
||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
set -eo pipefail
|
|
||||||
|
|
||||||
function main {
|
|
||||||
common::initialize
|
|
||||||
common::parse_cmdline "$@"
|
|
||||||
common::per_dir_hook "${ARGS[*]}" "${FILES[@]}"
|
|
||||||
}
|
|
||||||
|
|
||||||
function common::colorify {
|
|
||||||
# shellcheck disable=SC2034
|
|
||||||
local -r red="\e[0m\e[31m"
|
|
||||||
# shellcheck disable=SC2034
|
|
||||||
local -r green="\e[0m\e[32m"
|
|
||||||
# shellcheck disable=SC2034
|
|
||||||
local -r yellow="\e[0m\e[33m"
|
|
||||||
# Color reset
|
|
||||||
local -r RESET="\e[0m"
|
|
||||||
|
|
||||||
# Params start #
|
|
||||||
local COLOR="${!1}"
|
|
||||||
local -r TEXT=$2
|
|
||||||
# Params end #
|
|
||||||
|
|
||||||
if [ "$PRE_COMMIT_COLOR" = "never" ]; then
|
|
||||||
COLOR=$RESET
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo -e "${COLOR}${TEXT}${RESET}"
|
|
||||||
}
|
|
||||||
|
|
||||||
function common::initialize {
|
|
||||||
local SCRIPT_DIR
|
|
||||||
# get directory containing this script
|
|
||||||
SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
|
|
||||||
|
|
||||||
# source getopt function
|
|
||||||
# shellcheck source=lib_getopt
|
|
||||||
. "$SCRIPT_DIR/lib_getopt"
|
|
||||||
}
|
|
||||||
|
|
||||||
function common::parse_cmdline {
|
|
||||||
# common global arrays.
|
|
||||||
# Populated via `common::parse_cmdline` and can be used inside hooks' functions
|
|
||||||
declare -g -a ARGS=() FILES=() HOOK_CONFIG=()
|
|
||||||
|
|
||||||
local argv
|
|
||||||
argv=$(getopt -o a:,h: --long args:,hook-config: -- "$@") || return
|
|
||||||
eval "set -- $argv"
|
|
||||||
|
|
||||||
for argv; do
|
|
||||||
case $argv in
|
|
||||||
-a | --args)
|
|
||||||
shift
|
|
||||||
ARGS+=("$1")
|
|
||||||
shift
|
|
||||||
;;
|
|
||||||
-h | --hook-config)
|
|
||||||
shift
|
|
||||||
HOOK_CONFIG+=("$1;")
|
|
||||||
shift
|
|
||||||
;;
|
|
||||||
--)
|
|
||||||
shift
|
|
||||||
FILES=("$@")
|
|
||||||
break
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
function common::per_dir_hook {
|
|
||||||
local -r args="$1"
|
|
||||||
shift 1
|
|
||||||
local -a -r files=("$@")
|
|
||||||
|
|
||||||
# consume modified files passed from pre-commit so that
|
|
||||||
# hook runs against only those relevant directories
|
|
||||||
local index=0
|
|
||||||
for file_with_path in "${files[@]}"; do
|
|
||||||
file_with_path="${file_with_path// /__REPLACED__SPACE__}"
|
|
||||||
|
|
||||||
dir_paths[index]=$(dirname "$file_with_path")
|
|
||||||
|
|
||||||
((index += 1))
|
|
||||||
done
|
|
||||||
|
|
||||||
# preserve errexit status
|
|
||||||
shopt -qo errexit && ERREXIT_IS_SET=true
|
|
||||||
# allow hook to continue if exit_code is greater than 0
|
|
||||||
set +e
|
|
||||||
local final_exit_code=0
|
|
||||||
|
|
||||||
# run hook for each path
|
|
||||||
for dir_path in $(echo "${dir_paths[*]}" | tr ' ' '\n' | sort -u); do
|
|
||||||
dir_path="${dir_path//__REPLACED__SPACE__/ }"
|
|
||||||
pushd "$dir_path" > /dev/null || continue
|
|
||||||
|
|
||||||
per_dir_hook_unique_part "$args" "$dir_path"
|
|
||||||
|
|
||||||
local exit_code=$?
|
|
||||||
if [ $exit_code -ne 0 ]; then
|
|
||||||
final_exit_code=$exit_code
|
|
||||||
fi
|
|
||||||
|
|
||||||
popd > /dev/null
|
|
||||||
done
|
|
||||||
|
|
||||||
# restore errexit if it was set before the "for" loop
|
|
||||||
[[ $ERREXIT_IS_SET ]] && set -e
|
|
||||||
# return the hook final exit_code
|
|
||||||
exit $final_exit_code
|
|
||||||
}
|
|
||||||
|
|
||||||
function per_dir_hook_unique_part {
|
|
||||||
# common logic located in common::per_dir_hook
|
|
||||||
local -r args="$1"
|
|
||||||
local -r dir_path="$2"
|
|
||||||
|
|
||||||
if [ ! -d ".terraform" ]; then
|
|
||||||
init_output=$(terraform init -backend=false 2>&1)
|
|
||||||
init_code=$?
|
|
||||||
|
|
||||||
if [ $init_code -ne 0 ]; then
|
|
||||||
common::colorify "red" "Init before validation failed: $dir_path"
|
|
||||||
common::colorify "red" "$init_output"
|
|
||||||
exit $init_code
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
# pass the arguments to hook
|
|
||||||
# shellcheck disable=SC2068 # hook fails when quoting is used ("$arg[@]")
|
|
||||||
terraform providers lock ${args[@]}
|
|
||||||
|
|
||||||
# return exit code to common::per_dir_hook
|
|
||||||
local exit_code=$?
|
|
||||||
return $exit_code
|
|
||||||
}
|
|
||||||
|
|
||||||
[ "${BASH_SOURCE[0]}" != "$0" ] || main "$@"
|
|
||||||
|
|
@ -1,137 +0,0 @@
|
||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
set -eo pipefail
|
|
||||||
|
|
||||||
function main {
|
|
||||||
common::initialize
|
|
||||||
common::parse_cmdline "$@"
|
|
||||||
# Support for setting PATH to repo root.
|
|
||||||
ARGS=${ARGS[*]/__GIT_WORKING_DIR__/$(pwd)\/}
|
|
||||||
common::per_dir_hook "$ARGS" "${FILES[@]}"
|
|
||||||
}
|
|
||||||
|
|
||||||
function common::colorify {
|
|
||||||
# shellcheck disable=SC2034
|
|
||||||
local -r red="\e[0m\e[31m"
|
|
||||||
# shellcheck disable=SC2034
|
|
||||||
local -r green="\e[0m\e[32m"
|
|
||||||
# shellcheck disable=SC2034
|
|
||||||
local -r yellow="\e[0m\e[33m"
|
|
||||||
# Color reset
|
|
||||||
local -r RESET="\e[0m"
|
|
||||||
|
|
||||||
# Params start #
|
|
||||||
local COLOR="${!1}"
|
|
||||||
local -r TEXT=$2
|
|
||||||
# Params end #
|
|
||||||
|
|
||||||
if [ "$PRE_COMMIT_COLOR" = "never" ]; then
|
|
||||||
COLOR=$RESET
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo -e "${COLOR}${TEXT}${RESET}"
|
|
||||||
}
|
|
||||||
|
|
||||||
function common::initialize {
|
|
||||||
local SCRIPT_DIR
|
|
||||||
# get directory containing this script
|
|
||||||
SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
|
|
||||||
|
|
||||||
# source getopt function
|
|
||||||
# shellcheck source=lib_getopt
|
|
||||||
. "$SCRIPT_DIR/lib_getopt"
|
|
||||||
}
|
|
||||||
|
|
||||||
function common::parse_cmdline {
|
|
||||||
# common global arrays.
|
|
||||||
# Populated via `common::parse_cmdline` and can be used inside hooks' functions
|
|
||||||
declare -g -a ARGS=() FILES=() HOOK_CONFIG=()
|
|
||||||
|
|
||||||
local argv
|
|
||||||
argv=$(getopt -o a:,h: --long args:,hook-config: -- "$@") || return
|
|
||||||
eval "set -- $argv"
|
|
||||||
|
|
||||||
for argv; do
|
|
||||||
case $argv in
|
|
||||||
-a | --args)
|
|
||||||
shift
|
|
||||||
ARGS+=("$1")
|
|
||||||
shift
|
|
||||||
;;
|
|
||||||
-h | --hook-config)
|
|
||||||
shift
|
|
||||||
HOOK_CONFIG+=("$1;")
|
|
||||||
shift
|
|
||||||
;;
|
|
||||||
--)
|
|
||||||
shift
|
|
||||||
FILES=("$@")
|
|
||||||
break
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
function common::per_dir_hook {
|
|
||||||
local -r args="$1"
|
|
||||||
shift 1
|
|
||||||
local -a -r files=("$@")
|
|
||||||
|
|
||||||
# consume modified files passed from pre-commit so that
|
|
||||||
# hook runs against only those relevant directories
|
|
||||||
local index=0
|
|
||||||
for file_with_path in "${files[@]}"; do
|
|
||||||
file_with_path="${file_with_path// /__REPLACED__SPACE__}"
|
|
||||||
|
|
||||||
dir_paths[index]=$(dirname "$file_with_path")
|
|
||||||
|
|
||||||
((index += 1))
|
|
||||||
done
|
|
||||||
|
|
||||||
# preserve errexit status
|
|
||||||
shopt -qo errexit && ERREXIT_IS_SET=true
|
|
||||||
# allow hook to continue if exit_code is greater than 0
|
|
||||||
set +e
|
|
||||||
local final_exit_code=0
|
|
||||||
|
|
||||||
# run hook for each path
|
|
||||||
for dir_path in $(echo "${dir_paths[*]}" | tr ' ' '\n' | sort -u); do
|
|
||||||
dir_path="${dir_path//__REPLACED__SPACE__/ }"
|
|
||||||
pushd "$dir_path" > /dev/null || continue
|
|
||||||
|
|
||||||
per_dir_hook_unique_part "$args" "$dir_path"
|
|
||||||
|
|
||||||
local exit_code=$?
|
|
||||||
if [ $exit_code -ne 0 ]; then
|
|
||||||
final_exit_code=$exit_code
|
|
||||||
fi
|
|
||||||
|
|
||||||
popd > /dev/null
|
|
||||||
done
|
|
||||||
|
|
||||||
# restore errexit if it was set before the "for" loop
|
|
||||||
[[ $ERREXIT_IS_SET ]] && set -e
|
|
||||||
# return the hook final exit_code
|
|
||||||
exit $final_exit_code
|
|
||||||
}
|
|
||||||
|
|
||||||
function per_dir_hook_unique_part {
|
|
||||||
# common logic located in common::per_dir_hook
|
|
||||||
local -r args="$1"
|
|
||||||
local -r dir_path="$2"
|
|
||||||
|
|
||||||
# Print checked PATH **only** if TFLint have any messages
|
|
||||||
# shellcheck disable=SC2091,SC2068 # Suppress error output
|
|
||||||
$(tflint ${args[@]} 2>&1) 2> /dev/null || {
|
|
||||||
common::colorify "yellow" "TFLint in $dir_path/:"
|
|
||||||
|
|
||||||
# shellcheck disable=SC2068 # hook fails when quoting is used ("$arg[@]")
|
|
||||||
tflint ${args[@]}
|
|
||||||
}
|
|
||||||
|
|
||||||
# return exit code to common::per_dir_hook
|
|
||||||
local exit_code=$?
|
|
||||||
return $exit_code
|
|
||||||
}
|
|
||||||
|
|
||||||
[ "${BASH_SOURCE[0]}" != "$0" ] || main "$@"
|
|
||||||
|
|
@ -1,109 +0,0 @@
|
||||||
#!/usr/bin/env bash
|
|
||||||
set -eo pipefail
|
|
||||||
|
|
||||||
function main {
|
|
||||||
common::initialize
|
|
||||||
common::parse_cmdline "$@"
|
|
||||||
# Support for setting PATH to repo root.
|
|
||||||
ARGS=${ARGS[*]/__GIT_WORKING_DIR__/$(pwd)\/}
|
|
||||||
common::per_dir_hook "$ARGS" "${FILES[@]}"
|
|
||||||
}
|
|
||||||
|
|
||||||
function common::initialize {
|
|
||||||
local SCRIPT_DIR
|
|
||||||
# get directory containing this script
|
|
||||||
SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
|
|
||||||
|
|
||||||
# source getopt function
|
|
||||||
# shellcheck source=lib_getopt
|
|
||||||
. "$SCRIPT_DIR/lib_getopt"
|
|
||||||
}
|
|
||||||
|
|
||||||
function common::parse_cmdline {
|
|
||||||
# common global arrays.
|
|
||||||
# Populated via `common::parse_cmdline` and can be used inside hooks' functions
|
|
||||||
declare -g -a ARGS=() FILES=() HOOK_CONFIG=()
|
|
||||||
|
|
||||||
local argv
|
|
||||||
argv=$(getopt -o a:,h: --long args:,hook-config: -- "$@") || return
|
|
||||||
eval "set -- $argv"
|
|
||||||
|
|
||||||
for argv; do
|
|
||||||
case $argv in
|
|
||||||
-a | --args)
|
|
||||||
shift
|
|
||||||
ARGS+=("$1")
|
|
||||||
shift
|
|
||||||
;;
|
|
||||||
-h | --hook-config)
|
|
||||||
shift
|
|
||||||
HOOK_CONFIG+=("$1;")
|
|
||||||
shift
|
|
||||||
;;
|
|
||||||
--)
|
|
||||||
shift
|
|
||||||
FILES=("$@")
|
|
||||||
break
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
function common::per_dir_hook {
|
|
||||||
local -r args="$1"
|
|
||||||
shift 1
|
|
||||||
local -a -r files=("$@")
|
|
||||||
|
|
||||||
# consume modified files passed from pre-commit so that
|
|
||||||
# hook runs against only those relevant directories
|
|
||||||
local index=0
|
|
||||||
for file_with_path in "${files[@]}"; do
|
|
||||||
file_with_path="${file_with_path// /__REPLACED__SPACE__}"
|
|
||||||
|
|
||||||
dir_paths[index]=$(dirname "$file_with_path")
|
|
||||||
|
|
||||||
((index += 1))
|
|
||||||
done
|
|
||||||
|
|
||||||
# preserve errexit status
|
|
||||||
shopt -qo errexit && ERREXIT_IS_SET=true
|
|
||||||
# allow hook to continue if exit_code is greater than 0
|
|
||||||
set +e
|
|
||||||
local final_exit_code=0
|
|
||||||
|
|
||||||
# run hook for each path
|
|
||||||
for dir_path in $(echo "${dir_paths[*]}" | tr ' ' '\n' | sort -u); do
|
|
||||||
dir_path="${dir_path//__REPLACED__SPACE__/ }"
|
|
||||||
pushd "$dir_path" > /dev/null || continue
|
|
||||||
|
|
||||||
per_dir_hook_unique_part "$args" "$dir_path"
|
|
||||||
|
|
||||||
local exit_code=$?
|
|
||||||
if [ $exit_code -ne 0 ]; then
|
|
||||||
final_exit_code=$exit_code
|
|
||||||
fi
|
|
||||||
|
|
||||||
popd > /dev/null
|
|
||||||
done
|
|
||||||
|
|
||||||
# restore errexit if it was set before the "for" loop
|
|
||||||
[[ $ERREXIT_IS_SET ]] && set -e
|
|
||||||
# return the hook final exit_code
|
|
||||||
exit $final_exit_code
|
|
||||||
}
|
|
||||||
|
|
||||||
function per_dir_hook_unique_part {
|
|
||||||
# common logic located in common::per_dir_hook
|
|
||||||
local -r args="$1"
|
|
||||||
local -r dir_path="$2"
|
|
||||||
|
|
||||||
# pass the arguments to hook
|
|
||||||
# shellcheck disable=SC2068 # hook fails when quoting is used ("$arg[@]")
|
|
||||||
tfsec ${args[@]}
|
|
||||||
|
|
||||||
# return exit code to common::per_dir_hook
|
|
||||||
local exit_code=$?
|
|
||||||
return $exit_code
|
|
||||||
}
|
|
||||||
|
|
||||||
[ "${BASH_SOURCE[0]}" != "$0" ] || main "$@"
|
|
||||||
|
|
@ -1,107 +0,0 @@
|
||||||
#!/usr/bin/env bash
|
|
||||||
set -eo pipefail
|
|
||||||
|
|
||||||
function main {
|
|
||||||
common::initialize
|
|
||||||
common::parse_cmdline "$@"
|
|
||||||
common::per_dir_hook "${ARGS[*]}" "${FILES[@]}"
|
|
||||||
}
|
|
||||||
|
|
||||||
function common::initialize {
|
|
||||||
local SCRIPT_DIR
|
|
||||||
# get directory containing this script
|
|
||||||
SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
|
|
||||||
|
|
||||||
# source getopt function
|
|
||||||
# shellcheck source=lib_getopt
|
|
||||||
. "$SCRIPT_DIR/lib_getopt"
|
|
||||||
}
|
|
||||||
|
|
||||||
function common::parse_cmdline {
|
|
||||||
# common global arrays.
|
|
||||||
# Populated via `common::parse_cmdline` and can be used inside hooks' functions
|
|
||||||
declare -g -a ARGS=() FILES=() HOOK_CONFIG=()
|
|
||||||
|
|
||||||
local argv
|
|
||||||
argv=$(getopt -o a:,h: --long args:,hook-config: -- "$@") || return
|
|
||||||
eval "set -- $argv"
|
|
||||||
|
|
||||||
for argv; do
|
|
||||||
case $argv in
|
|
||||||
-a | --args)
|
|
||||||
shift
|
|
||||||
ARGS+=("$1")
|
|
||||||
shift
|
|
||||||
;;
|
|
||||||
-h | --hook-config)
|
|
||||||
shift
|
|
||||||
HOOK_CONFIG+=("$1;")
|
|
||||||
shift
|
|
||||||
;;
|
|
||||||
--)
|
|
||||||
shift
|
|
||||||
FILES=("$@")
|
|
||||||
break
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
function common::per_dir_hook {
|
|
||||||
local -r args="$1"
|
|
||||||
shift 1
|
|
||||||
local -a -r files=("$@")
|
|
||||||
|
|
||||||
# consume modified files passed from pre-commit so that
|
|
||||||
# hook runs against only those relevant directories
|
|
||||||
local index=0
|
|
||||||
for file_with_path in "${files[@]}"; do
|
|
||||||
file_with_path="${file_with_path// /__REPLACED__SPACE__}"
|
|
||||||
|
|
||||||
dir_paths[index]=$(dirname "$file_with_path")
|
|
||||||
|
|
||||||
((index += 1))
|
|
||||||
done
|
|
||||||
|
|
||||||
# preserve errexit status
|
|
||||||
shopt -qo errexit && ERREXIT_IS_SET=true
|
|
||||||
# allow hook to continue if exit_code is greater than 0
|
|
||||||
set +e
|
|
||||||
local final_exit_code=0
|
|
||||||
|
|
||||||
# run hook for each path
|
|
||||||
for dir_path in $(echo "${dir_paths[*]}" | tr ' ' '\n' | sort -u); do
|
|
||||||
dir_path="${dir_path//__REPLACED__SPACE__/ }"
|
|
||||||
pushd "$dir_path" > /dev/null || continue
|
|
||||||
|
|
||||||
per_dir_hook_unique_part "$args" "$dir_path"
|
|
||||||
|
|
||||||
local exit_code=$?
|
|
||||||
if [ $exit_code -ne 0 ]; then
|
|
||||||
final_exit_code=$exit_code
|
|
||||||
fi
|
|
||||||
|
|
||||||
popd > /dev/null
|
|
||||||
done
|
|
||||||
|
|
||||||
# restore errexit if it was set before the "for" loop
|
|
||||||
[[ $ERREXIT_IS_SET ]] && set -e
|
|
||||||
# return the hook final exit_code
|
|
||||||
exit $final_exit_code
|
|
||||||
}
|
|
||||||
|
|
||||||
function per_dir_hook_unique_part {
|
|
||||||
# common logic located in common::per_dir_hook
|
|
||||||
local -r args="$1"
|
|
||||||
local -r dir_path="$2"
|
|
||||||
|
|
||||||
# pass the arguments to hook
|
|
||||||
# shellcheck disable=SC2068 # hook fails when quoting is used ("$arg[@]")
|
|
||||||
terragrunt validate ${args[@]}
|
|
||||||
|
|
||||||
# return exit code to common::per_dir_hook
|
|
||||||
local exit_code=$?
|
|
||||||
return $exit_code
|
|
||||||
}
|
|
||||||
|
|
||||||
[ "${BASH_SOURCE[0]}" != "$0" ] || main "$@"
|
|
||||||
107
terrascan.sh
107
terrascan.sh
|
|
@ -1,107 +0,0 @@
|
||||||
#!/usr/bin/env bash
|
|
||||||
set -eo pipefail
|
|
||||||
|
|
||||||
function main {
|
|
||||||
common::initialize
|
|
||||||
common::parse_cmdline "$@"
|
|
||||||
common::per_dir_hook "${ARGS[*]}" "${FILES[@]}"
|
|
||||||
}
|
|
||||||
|
|
||||||
function common::initialize {
|
|
||||||
local SCRIPT_DIR
|
|
||||||
# get directory containing this script
|
|
||||||
SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
|
|
||||||
|
|
||||||
# source getopt function
|
|
||||||
# shellcheck source=lib_getopt
|
|
||||||
. "$SCRIPT_DIR/lib_getopt"
|
|
||||||
}
|
|
||||||
|
|
||||||
function common::parse_cmdline {
|
|
||||||
# common global arrays.
|
|
||||||
# Populated via `common::parse_cmdline` and can be used inside hooks' functions
|
|
||||||
declare -g -a ARGS=() FILES=() HOOK_CONFIG=()
|
|
||||||
|
|
||||||
local argv
|
|
||||||
argv=$(getopt -o a:,h: --long args:,hook-config: -- "$@") || return
|
|
||||||
eval "set -- $argv"
|
|
||||||
|
|
||||||
for argv; do
|
|
||||||
case $argv in
|
|
||||||
-a | --args)
|
|
||||||
shift
|
|
||||||
ARGS+=("$1")
|
|
||||||
shift
|
|
||||||
;;
|
|
||||||
-h | --hook-config)
|
|
||||||
shift
|
|
||||||
HOOK_CONFIG+=("$1;")
|
|
||||||
shift
|
|
||||||
;;
|
|
||||||
--)
|
|
||||||
shift
|
|
||||||
FILES=("$@")
|
|
||||||
break
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
function common::per_dir_hook {
|
|
||||||
local -r args="$1"
|
|
||||||
shift 1
|
|
||||||
local -a -r files=("$@")
|
|
||||||
|
|
||||||
# consume modified files passed from pre-commit so that
|
|
||||||
# hook runs against only those relevant directories
|
|
||||||
local index=0
|
|
||||||
for file_with_path in "${files[@]}"; do
|
|
||||||
file_with_path="${file_with_path// /__REPLACED__SPACE__}"
|
|
||||||
|
|
||||||
dir_paths[index]=$(dirname "$file_with_path")
|
|
||||||
|
|
||||||
((index += 1))
|
|
||||||
done
|
|
||||||
|
|
||||||
# preserve errexit status
|
|
||||||
shopt -qo errexit && ERREXIT_IS_SET=true
|
|
||||||
# allow hook to continue if exit_code is greater than 0
|
|
||||||
set +e
|
|
||||||
local final_exit_code=0
|
|
||||||
|
|
||||||
# run hook for each path
|
|
||||||
for dir_path in $(echo "${dir_paths[*]}" | tr ' ' '\n' | sort -u); do
|
|
||||||
dir_path="${dir_path//__REPLACED__SPACE__/ }"
|
|
||||||
pushd "$dir_path" > /dev/null || continue
|
|
||||||
|
|
||||||
per_dir_hook_unique_part "$args" "$dir_path"
|
|
||||||
|
|
||||||
local exit_code=$?
|
|
||||||
if [ $exit_code -ne 0 ]; then
|
|
||||||
final_exit_code=$exit_code
|
|
||||||
fi
|
|
||||||
|
|
||||||
popd > /dev/null
|
|
||||||
done
|
|
||||||
|
|
||||||
# restore errexit if it was set before the "for" loop
|
|
||||||
[[ $ERREXIT_IS_SET ]] && set -e
|
|
||||||
# return the hook final exit_code
|
|
||||||
exit $final_exit_code
|
|
||||||
}
|
|
||||||
|
|
||||||
function per_dir_hook_unique_part {
|
|
||||||
# common logic located in common::per_dir_hook
|
|
||||||
local -r args="$1"
|
|
||||||
local -r dir_path="$2"
|
|
||||||
|
|
||||||
# pass the arguments to hook
|
|
||||||
# shellcheck disable=SC2068 # hook fails when quoting is used ("$arg[@]")
|
|
||||||
terrascan scan -i terraform ${args[@]}
|
|
||||||
|
|
||||||
# return exit code to common::per_dir_hook
|
|
||||||
local exit_code=$?
|
|
||||||
return $exit_code
|
|
||||||
}
|
|
||||||
|
|
||||||
[ "${BASH_SOURCE[0]}" != "$0" ] || main "$@"
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue