Skip to content

Commit 0dd8e6d

Browse files
committed
1 parent c5f2a61 commit 0dd8e6d

11 files changed

+191
-2
lines changed

hooks/_common.sh

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
#!/usr/bin/env bash
22
set -eo pipefail
33

4+
#######################################################################
5+
# Init arguments parser
6+
# Arguments:
7+
# script_dir - absolute path to hook dir location
8+
#######################################################################
49
function common::initialize {
510
local -r script_dir=$1
611
# source getopt function
7-
# shellcheck source=lib_getopt
12+
# shellcheck source=../lib_getopt
813
. "$script_dir/../lib_getopt"
914
}
1015

16+
#######################################################################
17+
# Parse provided to script args and filenames and populate each to
18+
# appropriate Global
19+
# Globals (init and populate):
20+
# ARGS (array) arguments that configure wrapped tool behavior
21+
# HOOK_CONFIG (array) arguments that configure hook behavior
22+
# FILES (array) filenames to check
23+
# Arguments:
24+
# $@ (array) all specified in `hooks.[].args` in
25+
# `.pre-commit-config.yaml` and filenames.
26+
#######################################################################
1127
function common::parse_cmdline {
1228
# common global arrays.
1329
# Populated via `common::parse_cmdline` and can be used inside hooks' functions
@@ -39,6 +55,17 @@ function common::parse_cmdline {
3955
done
4056
}
4157

58+
#######################################################################
59+
# Hook execution boilerplate logic that common for hooks, that run on
60+
# per dir basis.
61+
# 1. Because hook run on whole dir, reduce file paths to uniq dir paths
62+
# 2. Run for each dir `per_dir_hook_unique_part`, on all paths
63+
# 2.1. If at least 1 check failed - change exit code to non-zero
64+
# 3. Complete hook execution and return exit code
65+
# Arguments:
66+
# args (string with array) arguments that configure wrapped tool behavior
67+
# files (array) filenames to check
68+
#######################################################################
4269
function common::per_dir_hook {
4370
local -r args="$1"
4471
shift 1
@@ -82,6 +109,16 @@ function common::per_dir_hook {
82109
exit $final_exit_code
83110
}
84111

112+
#######################################################################
113+
# Colorify provided string and print out it to stdout
114+
# Environment variables:
115+
# PRE_COMMIT_COLOR (string) If set to `never` - do not colorify string
116+
# Arguments:
117+
# COLOR (string) Color name that will be used for colorify
118+
# TEXT (string)
119+
# Outputs:
120+
# Print out provided text to stdout
121+
#######################################################################
85122
function common::colorify {
86123
# shellcheck disable=SC2034
87124
local -r red="\e[0m\e[31m"

hooks/infracost_breakdown.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ function main {
1313
infracost_breakdown_ "${HOOK_CONFIG[*]}" "${ARGS[*]}"
1414
}
1515

16+
#######################################################################
17+
# Wrapper around `infracost breakdown` tool that check and compare
18+
# infra cost by provided hook_config
19+
# Environment variables:
20+
# PRE_COMMIT_COLOR (string) If set to `never` - force tool output to
21+
# plain text
22+
# Arguments:
23+
# hook_config (string with array) arguments that configure hook behavior
24+
# args (string with array) arguments that configure wrapped tool behavior
25+
# Outputs:
26+
# Print out hook checks status (Passed/Failed), total monthly cost and
27+
# diff, summary about infracost check (non-supported resources etc.)
28+
#######################################################################
1629
function infracost_breakdown_ {
1730
local -r hook_config="$1"
1831
local args

hooks/terraform_docs.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ function main {
1717
terraform_docs_ "${HOOK_CONFIG[*]}" "$ARGS" "${FILES[@]}"
1818
}
1919

20+
#######################################################################
21+
# Functions that prepares hacks for old versions of `terraform`` and
22+
# `terraform-docs` that them call `terraform_docs`
23+
# Arguments:
24+
# hook_config (string with array) arguments that configure hook behavior
25+
# args (string with array) arguments that configure wrapped tool behavior
26+
# files (array) filenames to check
27+
#######################################################################
2028
function terraform_docs_ {
2129
local -r hook_config="$1"
2230
local -r args="$2"
@@ -61,6 +69,18 @@ function terraform_docs_ {
6169
fi
6270
}
6371

72+
#######################################################################
73+
# Wrapper around `terraform-docs` tool that check and change/create
74+
# (depends on on provided hook_config) terraform documentation in
75+
# markdown format
76+
# Arguments:
77+
# terraform_docs_awk_file (string) filename where awk hack for old
78+
# `terraform-docs` populated. Needed for tf 0.12+.
79+
# Hack skipped when `terraform_docs_awk_file == "0"`
80+
# hook_config (string with array) arguments that configure hook behavior
81+
# args (string with array) arguments that configure wrapped tool behavior
82+
# files (array) filenames to check
83+
#######################################################################
6484
function terraform_docs {
6585
local -r terraform_docs_awk_file="$1"
6686
local -r hook_config="$2"
@@ -183,6 +203,12 @@ function terraform_docs {
183203
done
184204
}
185205

206+
#######################################################################
207+
# Functions that create file with awk hacks for old versions of
208+
# `terraform-docs`
209+
# Arguments:
210+
# output_file (string) filename where hack will be written
211+
#######################################################################
186212
function terraform_docs_awk {
187213
local -r output_file=$1
188214

hooks/terraform_fmt.sh

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ function main {
1313
terraform_fmt_ "${ARGS[*]}" "${FILES[@]}"
1414
}
1515

16+
#######################################################################
17+
# Hook execution boilerplate logic that common for hooks, that run on
18+
# per dir basis. Little bit extended than `common::per_dir_hook`
19+
# 1. Because hook run on whole dir, reduce file paths to uniq dir paths
20+
# (uniq) 1.1. Collect paths to *.tfvars files to separate variable
21+
# 2. Run for each dir `per_dir_hook_unique_part`, on all paths
22+
# (uniq) 2.1. Run `terraform fmt` on each *.tfvars file
23+
# 2.2. If at least 1 check failed - change exit code to non-zero
24+
# 3. Complete hook execution and return exit code
25+
# Arguments:
26+
# args (string with array) arguments that configure wrapped tool behavior
27+
# files (array) filenames to check
28+
#######################################################################
1629
function terraform_fmt_ {
1730
local -r args="$1"
1831
shift 1
@@ -72,8 +85,17 @@ function terraform_fmt_ {
7285

7386
}
7487

88+
#######################################################################
89+
# Uniq part of `common::per_dir_hook`. That function executes in loop
90+
# on each provided dir path. Run wrapped tool with specified arguments
91+
# Arguments:
92+
# args (string with array) arguments that configure wrapped tool behavior
93+
# dir_path (string) PATH to dir from git repo root. Can be used in
94+
# error logging
95+
# Outputs:
96+
# If failed - print out hook checks status
97+
#######################################################################
7598
function per_dir_hook_unique_part {
76-
# common logic located in common::per_dir_hook
7799
local -r args="$1"
78100
local -r dir_path="$2"
79101

hooks/terraform_providers_lock.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ function main {
1414
common::per_dir_hook "${ARGS[*]}" "${FILES[@]}"
1515
}
1616

17+
#######################################################################
18+
# Uniq part of `common::per_dir_hook`. That function executes in loop
19+
# on each provided dir path. Run wrapped tool with specified arguments
20+
# Arguments:
21+
# args (string with array) arguments that configure wrapped tool behavior
22+
# dir_path (string) PATH to dir from git repo root. Can be used in
23+
# error logging
24+
# Outputs:
25+
# If failed - print out hook checks status
26+
#######################################################################
1727
function per_dir_hook_unique_part {
1828
# common logic located in common::per_dir_hook
1929
local -r args="$1"

hooks/terraform_tflint.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ function main {
1717
common::per_dir_hook "$ARGS" "${FILES[@]}"
1818
}
1919

20+
#######################################################################
21+
# Uniq part of `common::per_dir_hook`. That function executes in loop
22+
# on each provided dir path. Run wrapped tool with specified arguments
23+
# Arguments:
24+
# args (string with array) arguments that configure wrapped tool behavior
25+
# dir_path (string) PATH to dir from git repo root. Can be used in
26+
# error logging
27+
# Outputs:
28+
# If failed - print out hook checks status
29+
#######################################################################
2030
function per_dir_hook_unique_part {
2131
# common logic located in common::per_dir_hook
2232
local -r args="$1"

hooks/terraform_tfsec.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ function main {
1616
common::per_dir_hook "$ARGS" "${FILES[@]}"
1717
}
1818

19+
#######################################################################
20+
# Uniq part of `common::per_dir_hook`. That function executes in loop
21+
# on each provided dir path. Run wrapped tool with specified arguments
22+
# Arguments:
23+
# args (string with array) arguments that configure wrapped tool behavior
24+
# dir_path (string) PATH to dir from git repo root. Can be used in
25+
# error logging
26+
# Outputs:
27+
# If failed - print out hook checks status
28+
#######################################################################
1929
function per_dir_hook_unique_part {
2030
# common logic located in common::per_dir_hook
2131
local -r args="$1"

hooks/terraform_validate.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ function main {
1515
terraform_validate_
1616
}
1717

18+
#######################################################################
19+
# Parse provided to script args and filenames and populate each to
20+
# appropriate Global
21+
# Globals (init and populate):
22+
# ARGS (array) arguments that configure wrapped tool behavior
23+
# INIT_ARGS (array) arguments for `terraform init` command`
24+
# ENVS (array) environment variables that will be used with
25+
# `terraform` commands
26+
# FILES (array) filenames to check
27+
# Arguments:
28+
# $@ (array) all specified in `hooks.[].args` in
29+
# `.pre-commit-config.yaml` and filenames.
30+
#######################################################################
1831
function parse_cmdline_ {
1932
declare argv
2033
argv=$(getopt -o e:i:a: --long envs:,init-args:,args: -- "$@") || return
@@ -46,6 +59,24 @@ function parse_cmdline_ {
4659
done
4760
}
4861

62+
#######################################################################
63+
# Wrapper around `terraform validate` tool that check is code are valid
64+
# 1. Export provided envs to environment
65+
# 2. Because hook run on whole dir, reduce file paths to uniq dir paths
66+
# 3. In each dir that have *.tf files:
67+
# 3.1. Check is `.terraform` exist and if not - run `terraform init`
68+
# 3.2. Run `terraform validate`
69+
# 3.3. If at least 1 check failed - change exit code to non-zero
70+
# 4. Complete hook execution and return exit code
71+
# Globals:
72+
# ARGS (array) arguments that configure wrapped tool behavior
73+
# INIT_ARGS (array) arguments for `terraform init` command`
74+
# ENVS (array) environment variables that will be used with
75+
# `terraform` commands
76+
# FILES (array) filenames to check
77+
# Outputs:
78+
# If failed - print out hook checks status
79+
#######################################################################
4980
function terraform_validate_ {
5081

5182
# Setup environment variables

hooks/terragrunt_fmt.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ function main {
1313
common::per_dir_hook "${ARGS[*]}" "${FILES[@]}"
1414
}
1515

16+
#######################################################################
17+
# Uniq part of `common::per_dir_hook`. That function executes in loop
18+
# on each provided dir path. Run wrapped tool with specified arguments
19+
# Arguments:
20+
# args (string with array) arguments that configure wrapped tool behavior
21+
# dir_path (string) PATH to dir from git repo root. Can be used in
22+
# error logging
23+
# Outputs:
24+
# If failed - print out hook checks status
25+
#######################################################################
1626
function per_dir_hook_unique_part {
1727
# common logic located in common::per_dir_hook
1828
local -r args="$1"

hooks/terragrunt_validate.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ function main {
1313
common::per_dir_hook "${ARGS[*]}" "${FILES[@]}"
1414
}
1515

16+
#######################################################################
17+
# Uniq part of `common::per_dir_hook`. That function executes in loop
18+
# on each provided dir path. Run wrapped tool with specified arguments
19+
# Arguments:
20+
# args (string with array) arguments that configure wrapped tool behavior
21+
# dir_path (string) PATH to dir from git repo root. Can be used in
22+
# error logging
23+
# Outputs:
24+
# If failed - print out hook checks status
25+
#######################################################################
1626
function per_dir_hook_unique_part {
1727
# common logic located in common::per_dir_hook
1828
local -r args="$1"

0 commit comments

Comments
 (0)