Skip to content

Commit

Permalink
chore: Improved code structure (moved hooks into a separate dir) (#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxymVlasov authored Jan 6, 2022
1 parent 3045dd5 commit c5f2a61
Show file tree
Hide file tree
Showing 20 changed files with 274 additions and 799 deletions.
24 changes: 12 additions & 12 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
- id: infracost_breakdown
name: Infracost breakdown
description: Check terraform infrastructure cost
entry: infracost_breakdown.sh
entry: hooks/infracost_breakdown.sh
language: script
require_serial: true
files: \.(tf(vars)?|hcl)$
Expand All @@ -10,7 +10,7 @@
- id: terraform_fmt
name: Terraform fmt
description: Rewrites all Terraform configuration files to a canonical format.
entry: terraform_fmt.sh
entry: hooks/terraform_fmt.sh
language: script
files: (\.tf|\.tfvars)$
exclude: \.terraform\/.*$
Expand All @@ -19,7 +19,7 @@
name: Terraform docs
description: Inserts input and output documentation into README.md (using terraform-docs).
require_serial: true
entry: terraform_docs.sh
entry: hooks/terraform_docs.sh
language: script
files: (\.tf|\.terraform\.lock\.hcl)$
exclude: \.terraform\/.*$
Expand All @@ -28,7 +28,7 @@
name: Terraform docs (without aggregate type defaults)
description: Inserts input and output documentation into README.md (using terraform-docs). Identical to terraform_docs.
require_serial: true
entry: terraform_docs.sh
entry: hooks/terraform_docs.sh
language: script
files: (\.tf)$
exclude: \.terraform\/.*$
Expand All @@ -46,7 +46,7 @@
name: Terraform validate
description: Validates all Terraform configuration files.
require_serial: true
entry: terraform_validate.sh
entry: hooks/terraform_validate.sh
language: script
files: (\.tf|\.tfvars)$
exclude: \.terraform\/.*$
Expand All @@ -55,7 +55,7 @@
name: Lock terraform provider versions
description: Updates provider signatures in dependency lock files.
require_serial: true
entry: terraform_providers_lock.sh
entry: hooks/terraform_providers_lock.sh
language: script
files: (\.terraform\.lock\.hcl)$
exclude: \.terraform\/.*$
Expand All @@ -64,23 +64,23 @@
name: Terraform validate with tflint
description: Validates all Terraform configuration files with TFLint.
require_serial: true
entry: terraform_tflint.sh
entry: hooks/terraform_tflint.sh
language: script
files: (\.tf|\.tfvars)$
exclude: \.terraform\/.*$

- id: terragrunt_fmt
name: Terragrunt fmt
description: Rewrites all Terragrunt configuration files to a canonical format.
entry: terragrunt_fmt.sh
entry: hooks/terragrunt_fmt.sh
language: script
files: (\.hcl)$
exclude: \.terraform\/.*$

- id: terragrunt_validate
name: Terragrunt validate
description: Validates all Terragrunt configuration files.
entry: terragrunt_validate.sh
entry: hooks/terragrunt_validate.sh
language: script
files: (\.hcl)$
exclude: \.terraform\/.*$
Expand All @@ -89,13 +89,13 @@
name: Terraform validate with tfsec
description: Static analysis of Terraform templates to spot potential security issues.
require_serial: true
entry: terraform_tfsec.sh
entry: hooks/terraform_tfsec.sh
language: script

- id: checkov
name: Checkov
description: Runs checkov on Terraform templates.
entry: checkov -d .
entry: hooks/checkov -d .
language: python
pass_filenames: false
always_run: false
Expand All @@ -107,7 +107,7 @@
name: terrascan
description: Runs terrascan on Terraform templates.
language: script
entry: terrascan.sh
entry: hooks/terrascan.sh
files: \.tf$
exclude: \.terraform\/.*$
require_serial: true
File renamed without changes.
50 changes: 24 additions & 26 deletions terragrunt_fmt.sh → hooks/_common.sh
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
#!/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]}")")"

local -r script_dir=$1
# source getopt function
# shellcheck source=lib_getopt
. "$SCRIPT_DIR/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=()
declare -g -a ARGS=() HOOK_CONFIG=() FILES=()

local argv
argv=$(getopt -o a:,h: --long args:,hook-config: -- "$@") || return
Expand All @@ -40,6 +31,7 @@ function common::parse_cmdline {
;;
--)
shift
# shellcheck disable=SC2034 # Variable is used
FILES=("$@")
break
;;
Expand Down Expand Up @@ -90,18 +82,24 @@ function common::per_dir_hook {
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 hclfmt ${args[@]}

# return exit code to common::per_dir_hook
local exit_code=$?
return $exit_code
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}"
}

[ "${BASH_SOURCE[0]}" != "$0" ] || main "$@"
71 changes: 7 additions & 64 deletions infracost_breakdown.sh → hooks/infracost_breakdown.sh
Original file line number Diff line number Diff line change
@@ -1,75 +1,18 @@
#!/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
common::initialize "$SCRIPT_DIR"
common::parse_cmdline "$@"
# shellcheck disable=SC2153 # False positive
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_ {
local -r hook_config="$1"
local args
Expand Down
50 changes: 9 additions & 41 deletions terraform_docs.sh → hooks/terraform_docs.sh
Original file line number Diff line number Diff line change
@@ -1,54 +1,22 @@
#!/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
common::initialize "$SCRIPT_DIR"
common::parse_cmdline "$@"
# 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)\/}
# shellcheck disable=SC2128 # It's the simplest syntax for that case
# shellcheck disable=SC2153 # False positive
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_ {
local -r hook_config="$1"
local -r args="$2"
Expand Down
File renamed without changes.
48 changes: 7 additions & 41 deletions terraform_fmt.sh → hooks/terraform_fmt.sh
Original file line number Diff line number Diff line change
@@ -1,52 +1,18 @@
#!/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
common::initialize "$SCRIPT_DIR"
common::parse_cmdline "$@"
# shellcheck disable=SC2153 # False positive
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_ {
local -r args="$1"
shift 1
Expand Down
Loading

0 comments on commit c5f2a61

Please sign in to comment.