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: Google Shell Style Compliance #326

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 4 additions & 4 deletions hooks/_common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set -eo pipefail
# Arguments:
# script_dir - absolute path to hook dir location
#######################################################################
function common::initialize {
common::initialize() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name() to function name change was an effort by @MaxymVlasov
Please negotiate this with Max.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, please return function word. Google style guide do not determine which syntax should be used

About () - not sure is it needed. It looks like apendix, during bash not be able to achieve function function_name(var1, var2)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, George mentined that () will not works within function
#326 (comment)

local -r script_dir=$1
# source getopt function
# shellcheck source=../lib_getopt
Expand All @@ -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=()
Expand Down Expand Up @@ -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=("$@")
Expand Down Expand Up @@ -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
Expand Down
24 changes: 18 additions & 6 deletions hooks/infracost_breakdown.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
#######################################
Comment on lines +9 to +17
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too obvious. Not needed

main() {
common::initialize "$SCRIPT_DIR"
common::parse_cmdline "$@"
# shellcheck disable=SC2153 # False positive
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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 "$@"
25 changes: 19 additions & 6 deletions hooks/terraform_docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -368,4 +378,7 @@ EOF

}




[ "${BASH_SOURCE[0]}" != "$0" ] || main "$@"
20 changes: 16 additions & 4 deletions hooks/terraform_fmt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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=("$@")
Expand Down Expand Up @@ -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
Expand All @@ -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"

Expand All @@ -108,4 +117,7 @@ function per_dir_hook_unique_part {
return $exit_code
}




[ "${BASH_SOURCE[0]}" != "$0" ] || main "$@"
14 changes: 12 additions & 2 deletions hooks/terraform_providers_lock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"

Expand All @@ -48,4 +57,5 @@ function per_dir_hook_unique_part {
return $exit_code
}


[ "${BASH_SOURCE[0]}" != "$0" ] || main "$@"
15 changes: 13 additions & 2 deletions hooks/terraform_tflint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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"

Expand All @@ -45,4 +54,6 @@ function per_dir_hook_unique_part {
return $exit_code
}



[ "${BASH_SOURCE[0]}" != "$0" ] || main "$@"
15 changes: 13 additions & 2 deletions hooks/terraform_tfsec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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"
Expand All @@ -40,4 +49,6 @@ function per_dir_hook_unique_part {
return $exit_code
}



[ "${BASH_SOURCE[0]}" != "$0" ] || main "$@"
21 changes: 15 additions & 6 deletions hooks/terraform_validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -144,6 +151,8 @@ function terraform_validate_ {
fi
}



# global arrays
declare -a INIT_ARGS
declare -a ENVS
Expand Down
13 changes: 11 additions & 2 deletions hooks/terragrunt_fmt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
Loading