Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Document functions (based on google style guide) #317

Merged
merged 4 commits into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion hooks/_common.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
#!/usr/bin/env bash
set -eo pipefail

#######################################################################
# Init arguments parser
# Arguments:
# script_dir - absolute path to hook dir location
#######################################################################
function common::initialize {
local -r script_dir=$1
# source getopt function
# shellcheck source=lib_getopt
# shellcheck source=../lib_getopt
. "$script_dir/../lib_getopt"
}

#######################################################################
# Parse provided to script args and filenames and populate each to
# appropriate Global
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# Globals (init and populate):
# ARGS (array) arguments that configure wrapped tool behavior
# HOOK_CONFIG (array) arguments that configure hook behavior
# FILES (array) filenames to check
# Arguments:
# $@ (array) all specified in `hooks.[].args` in
# `.pre-commit-config.yaml` and filenames.
#######################################################################
function common::parse_cmdline {
# common global arrays.
# Populated via `common::parse_cmdline` and can be used inside hooks' functions
Expand Down Expand Up @@ -39,6 +55,17 @@ function common::parse_cmdline {
done
}

#######################################################################
# Hook execution boilerplate logic that common for hooks, that run on
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# per dir basis.
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# 1. Because hook run on whole dir, reduce file paths to uniq dir paths
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# 2. Run for each dir `per_dir_hook_unique_part`, on all paths
# 2.1. If at least 1 check failed - change exit code to non-zero
# 3. Complete hook execution and return exit code
# Arguments:
# args (string with array) arguments that configure wrapped tool behavior
# files (array) filenames to check
#######################################################################
function common::per_dir_hook {
local -r args="$1"
shift 1
Expand Down Expand Up @@ -82,6 +109,16 @@ function common::per_dir_hook {
exit $final_exit_code
}

#######################################################################
# Colorify provided string and print out it to stdout
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# Environment variables:
# PRE_COMMIT_COLOR (string) If set to `never` - do not colorify string
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# Arguments:
# COLOR (string) Color name that will be used for colorify
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# TEXT (string)
# Outputs:
# Print out provided text to stdout
#######################################################################
function common::colorify {
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# shellcheck disable=SC2034
local -r red="\e[0m\e[31m"
Expand Down
13 changes: 13 additions & 0 deletions hooks/infracost_breakdown.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ function main {
infracost_breakdown_ "${HOOK_CONFIG[*]}" "${ARGS[*]}"
}

#######################################################################
# Wrapper around `infracost breakdown` tool that check and compare
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# infra cost by provided hook_config
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# Environment variables:
# PRE_COMMIT_COLOR (string) If set to `never` - force tool output to
# plain text
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# Arguments:
# hook_config (string with array) arguments that configure hook behavior
# args (string with array) arguments that configure wrapped tool behavior
# Outputs:
# Print out hook checks status (Passed/Failed), total monthly cost and
# diff, summary about infracost check (non-supported resources etc.)
#######################################################################
function infracost_breakdown_ {
local -r hook_config="$1"
local args
Expand Down
26 changes: 26 additions & 0 deletions hooks/terraform_docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ function main {
terraform_docs_ "${HOOK_CONFIG[*]}" "$ARGS" "${FILES[@]}"
}

#######################################################################
# Functions that prepares hacks for old versions of `terraform`` and
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# `terraform-docs` that them call `terraform_docs`
yermulnik marked this conversation as resolved.
Show resolved Hide resolved
# Arguments:
# hook_config (string with array) arguments that configure hook behavior
# args (string with array) arguments that configure wrapped tool behavior
# files (array) filenames to check
#######################################################################
function terraform_docs_ {
local -r hook_config="$1"
local -r args="$2"
Expand Down Expand Up @@ -61,6 +69,18 @@ function terraform_docs_ {
fi
}

#######################################################################
# Wrapper around `terraform-docs` tool that check and change/create
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# (depends on on provided hook_config) terraform documentation in
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# markdown format
# Arguments:
# terraform_docs_awk_file (string) filename where awk hack for old
# `terraform-docs` populated. Needed for tf 0.12+.
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# Hack skipped when `terraform_docs_awk_file == "0"`
# hook_config (string with array) arguments that configure hook behavior
# args (string with array) arguments that configure wrapped tool behavior
# files (array) filenames to check
#######################################################################
function terraform_docs {
local -r terraform_docs_awk_file="$1"
local -r hook_config="$2"
Expand Down Expand Up @@ -183,6 +203,12 @@ function terraform_docs {
done
}

#######################################################################
# Functions that create file with awk hacks for old versions of
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# `terraform-docs`
# Arguments:
# output_file (string) filename where hack will be written
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
#######################################################################
function terraform_docs_awk {
local -r output_file=$1

Expand Down
24 changes: 23 additions & 1 deletion hooks/terraform_fmt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ function main {
terraform_fmt_ "${ARGS[*]}" "${FILES[@]}"
}

#######################################################################
# Hook execution boilerplate logic that common for hooks, that run on
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# per dir basis. Little bit extended than `common::per_dir_hook`
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# 1. Because hook run on whole dir, reduce file paths to uniq dir paths
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# (uniq) 1.1. Collect paths to *.tfvars files to separate variable
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# 2. Run for each dir `per_dir_hook_unique_part`, on all paths
# (uniq) 2.1. Run `terraform fmt` on each *.tfvars file
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# 2.2. If at least 1 check failed - change exit code to non-zero
# 3. Complete hook execution and return exit code
# Arguments:
# args (string with array) arguments that configure wrapped tool behavior
# files (array) filenames to check
#######################################################################
function terraform_fmt_ {
local -r args="$1"
shift 1
Expand Down Expand Up @@ -72,8 +85,17 @@ function terraform_fmt_ {

}

#######################################################################
# Uniq part of `common::per_dir_hook`. That function executes in loop
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# on each provided dir path. Run wrapped tool with specified arguments
# Arguments:
# args (string with array) arguments that configure wrapped tool behavior
# dir_path (string) PATH to dir from git repo root. Can be used in
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# error logging
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# Outputs:
# If failed - print out hook checks status
#######################################################################
function per_dir_hook_unique_part {
# common logic located in common::per_dir_hook
local -r args="$1"
local -r dir_path="$2"

Expand Down
11 changes: 10 additions & 1 deletion hooks/terraform_providers_lock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,17 @@ function main {
common::per_dir_hook "${ARGS[*]}" "${FILES[@]}"
}

#######################################################################
# Uniq part of `common::per_dir_hook`. That function executes in loop
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# on each provided dir path. Run wrapped tool with specified arguments
# Arguments:
# args (string with array) arguments that configure wrapped tool behavior
# dir_path (string) PATH to dir from git repo root. Can be used in
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# error logging
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# Outputs:
# If failed - print out hook checks status
#######################################################################
function per_dir_hook_unique_part {
# common logic located in common::per_dir_hook
local -r args="$1"
local -r dir_path="$2"

Expand Down
11 changes: 10 additions & 1 deletion hooks/terraform_tflint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,17 @@ function main {
common::per_dir_hook "$ARGS" "${FILES[@]}"
}

#######################################################################
# Uniq part of `common::per_dir_hook`. That function executes in loop
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# on each provided dir path. Run wrapped tool with specified arguments
# Arguments:
# args (string with array) arguments that configure wrapped tool behavior
# dir_path (string) PATH to dir from git repo root. Can be used in
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# error logging
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# Outputs:
# If failed - print out hook checks status
#######################################################################
function per_dir_hook_unique_part {
# common logic located in common::per_dir_hook
local -r args="$1"
local -r dir_path="$2"

Expand Down
11 changes: 10 additions & 1 deletion hooks/terraform_tfsec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,17 @@ function main {
common::per_dir_hook "$ARGS" "${FILES[@]}"
}

#######################################################################
# Uniq part of `common::per_dir_hook`. That function executes in loop
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# on each provided dir path. Run wrapped tool with specified arguments
# Arguments:
# args (string with array) arguments that configure wrapped tool behavior
# dir_path (string) PATH to dir from git repo root. Can be used in
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# error logging
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# Outputs:
# If failed - print out hook checks status
#######################################################################
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"
Expand Down
31 changes: 31 additions & 0 deletions hooks/terraform_validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ function main {
terraform_validate_
}

#######################################################################
# Parse provided to script args and filenames and populate each to
# appropriate Global
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# Globals (init and populate):
# ARGS (array) arguments that configure wrapped tool behavior
# INIT_ARGS (array) arguments for `terraform init` command`
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# ENVS (array) environment variables that will be used with
# `terraform` commands
# FILES (array) filenames to check
# Arguments:
# $@ (array) all specified in `hooks.[].args` in
# `.pre-commit-config.yaml` and filenames.
#######################################################################
function parse_cmdline_ {
declare argv
argv=$(getopt -o e:i:a: --long envs:,init-args:,args: -- "$@") || return
Expand Down Expand Up @@ -46,6 +59,24 @@ function parse_cmdline_ {
done
}

#######################################################################
# Wrapper around `terraform validate` tool that check is code are valid
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# 1. Export provided envs to environment
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# 2. Because hook run on whole dir, reduce file paths to uniq dir paths
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# 3. In each dir that have *.tf files:
# 3.1. Check is `.terraform` exist and if not - run `terraform init`
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# 3.2. Run `terraform validate`
# 3.3. If at least 1 check failed - change exit code to non-zero
# 4. Complete hook execution and return exit code
# Globals:
# ARGS (array) arguments that configure wrapped tool behavior
# INIT_ARGS (array) arguments for `terraform init` command`
# ENVS (array) environment variables that will be used with
# `terraform` commands
# FILES (array) filenames to check
# Outputs:
# If failed - print out hook checks status
#######################################################################
function terraform_validate_ {

# Setup environment variables
Expand Down
11 changes: 10 additions & 1 deletion hooks/terragrunt_fmt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,17 @@ function main {
common::per_dir_hook "${ARGS[*]}" "${FILES[@]}"
}

#######################################################################
# Uniq part of `common::per_dir_hook`. That function executes in loop
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# on each provided dir path. Run wrapped tool with specified arguments
# Arguments:
# args (string with array) arguments that configure wrapped tool behavior
# dir_path (string) PATH to dir from git repo root. Can be used in
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# error logging
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# Outputs:
# If failed - print out hook checks status
#######################################################################
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"
Expand Down
11 changes: 10 additions & 1 deletion hooks/terragrunt_validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,17 @@ function main {
common::per_dir_hook "${ARGS[*]}" "${FILES[@]}"
}

#######################################################################
# Uniq part of `common::per_dir_hook`. That function executes in loop
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# on each provided dir path. Run wrapped tool with specified arguments
# Arguments:
# args (string with array) arguments that configure wrapped tool behavior
# dir_path (string) PATH to dir from git repo root. Can be used in
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# error logging
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# Outputs:
# If failed - print out hook checks status
#######################################################################
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"
Expand Down
11 changes: 10 additions & 1 deletion hooks/terrascan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,17 @@ function main {
common::per_dir_hook "${ARGS[*]}" "${FILES[@]}"
}

#######################################################################
# Uniq part of `common::per_dir_hook`. That function executes in loop
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# on each provided dir path. Run wrapped tool with specified arguments
# Arguments:
# args (string with array) arguments that configure wrapped tool behavior
# dir_path (string) PATH to dir from git repo root. Can be used in
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# error logging
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
# Outputs:
# If failed - print out hook checks status
#######################################################################
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"
Expand Down