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 3 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 {
function common::initialize() {
mohitsaxenaknoldus marked this conversation as resolved.
Show resolved Hide resolved
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 {
function 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 {
function 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 {
function common::colorify() {
# shellcheck disable=SC2034
local -r red="\e[0m\e[31m"
# shellcheck disable=SC2034
Expand Down
50 changes: 30 additions & 20 deletions hooks/infracost_breakdown.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ 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
infracost_breakdown_ "${HOOK_CONFIG[*]}" "${ARGS[*]}"
}

#######################################################################
# Wrapper around `infracost breakdown` tool which checks and compares
# infra cost based on provided hook_config
Expand All @@ -25,13 +18,13 @@ 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_ {
function infracost_breakdown_() {
local -r hook_config="$1"
local args
read -r -a args <<< "$2"
read -r -a args <<<"$2"
Copy link
Collaborator

Choose a reason for hiding this comment

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

The whitespace reduction changes is what shellcheck GH action refuses to accept.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Would you please pet linter by reverting such whitespace change along with fix other linter errors and warnings.


# Get hook settings
IFS=";" read -r -a checks <<< "$hook_config"
IFS=";" read -r -a checks <<<"$hook_config"

if [ "$PRE_COMMIT_COLOR" = "never" ]; then
args+=("--no-color")
Expand All @@ -40,15 +33,15 @@ function infracost_breakdown_ {
local RESULTS
RESULTS="$(infracost breakdown "${args[@]}" --format json)"
local API_VERSION
API_VERSION="$(jq -r .version <<< "$RESULTS")"
API_VERSION="$(jq -r .version <<<"$RESULTS")"

if [ "$API_VERSION" != "0.2" ]; then
common::colorify "yellow" "WARNING: Hook supports Infracost API version \"0.2\", got \"$API_VERSION\""
common::colorify "yellow" " Some things may not work as expected"
fi

local dir
dir="$(jq '.projects[].metadata.vcsSubPath' <<< "$RESULTS")"
dir="$(jq '.projects[].metadata.vcsSubPath' <<<"$RESULTS")"
echo -e "\nRunning in $dir"

local have_failed_checks=false
Expand Down Expand Up @@ -81,12 +74,12 @@ function infracost_breakdown_ {
# [.projects[].diff.totalMonthlyCost | select (.!=null) | tonumber] | add > 1000
operation=${operations[-1]}

IFS="$operation" read -r -a jq_check <<< "$check"
real_value="$(jq "${jq_check[0]}" <<< "$RESULTS")"
IFS="$operation" read -r -a jq_check <<<"$check"
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 @@ -123,16 +116,33 @@ function infracost_breakdown_ {
done

# Fancy informational output
currency="$(jq -r '.currency' <<< "$RESULTS")"
currency="$(jq -r '.currency' <<<"$RESULTS")"

echo -e "\nSummary: $(jq -r '.summary' <<< "$RESULTS")"
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
}


#######################################
# main function
# Globals:
# ARGS
# HOOK_CONFIG
# SCRIPT_DIR
# Arguments:
# None
#######################################
function main() {
mohitsaxenaknoldus marked this conversation as resolved.
Show resolved Hide resolved
common::initialize "$SCRIPT_DIR"
common::parse_cmdline "$@"
# shellcheck disable=SC2153 # False positive
infracost_breakdown_ "${HOOK_CONFIG[*]}" "${ARGS[*]}"
}

[ "${BASH_SOURCE[0]}" != "$0" ] || main "$@"
71 changes: 41 additions & 30 deletions hooks/terraform_docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,6 @@ 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 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 which prepares hacks for old versions of `terraform` and
# `terraform-docs` that them call `terraform_docs`
Expand All @@ -25,14 +14,14 @@ function main {
# args (string with array) arguments that configure wrapped tool behavior
# files (array) filenames to check
#######################################################################
function terraform_docs_ {
function terraform_docs_() {
local -r hook_config="$1"
local -r args="$2"
shift 2
local -a -r files=("$@")

# Get hook settings
IFS=";" read -r -a configs <<< "$hook_config"
IFS=";" read -r -a configs <<<"$hook_config"

local hack_terraform_docs
hack_terraform_docs=$(terraform version | sed -n 1p | grep -c 0.12) || true
Expand Down Expand Up @@ -81,7 +70,7 @@ function terraform_docs_ {
# args (string with array) arguments that configure wrapped tool behavior
# files (array) filenames to check
#######################################################################
function terraform_docs {
function terraform_docs() {
local -r terraform_docs_awk_file="$1"
local -r hook_config="$2"
local -r args="$3"
Expand Down Expand Up @@ -109,11 +98,11 @@ function terraform_docs {
local add_to_existing=false
local create_if_not_exist=false

read -r -a configs <<< "$hook_config"
read -r -a configs <<<"$hook_config"

for c in "${configs[@]}"; do

IFS="=" read -r -a config <<< "$c"
IFS="=" read -r -a config <<<"$c"
key=${config[0]}
value=${config[1]}

Expand All @@ -134,19 +123,19 @@ function terraform_docs {
for dir_path in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do
dir_path="${dir_path//__REPLACED__SPACE__/ }"

pushd "$dir_path" > /dev/null || continue
pushd "$dir_path" >/dev/null || continue

#
# Create file if it not exist and `--create-if-not-exist=true` provided
#
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
[ ! "$dir_have_tf_files" ] && popd > /dev/null && continue
[ ! "$dir_have_tf_files" ] && popd >/dev/null && continue

dir="$(dirname "$text_file")"

Expand All @@ -155,11 +144,11 @@ function terraform_docs {
echo -e "# ${PWD##*/}\n"
echo "<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->"
echo "<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->"
} >> "$text_file"
} >>"$text_file"
fi

# If file still not exist - skip dir
[[ ! -f "$text_file" ]] && popd > /dev/null && continue
[[ ! -f "$text_file" ]] && popd >/dev/null && continue

#
# If `--add-to-existing-file=true` set, check is in file exist "hook markers",
Expand All @@ -169,14 +158,14 @@ function terraform_docs {
HAVE_MARKER=$(grep -o '<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->' "$text_file" || exit 0)

if [ ! "$HAVE_MARKER" ]; then
echo "<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->" >> "$text_file"
echo "<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->" >> "$text_file"
echo "<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->" >>"$text_file"
echo "<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->" >>"$text_file"
fi
fi

if [[ "$terraform_docs_awk_file" == "0" ]]; then
# shellcheck disable=SC2086
terraform-docs md $args ./ > "$tmp_file"
terraform-docs md $args ./ >"$tmp_file"
else
# Can't append extension for mktemp, so renaming instead
local tmp_file_docs
Expand All @@ -185,9 +174,9 @@ function terraform_docs {
local tmp_file_docs_tf
tmp_file_docs_tf="$tmp_file_docs.tf"

awk -f "$terraform_docs_awk_file" ./*.tf > "$tmp_file_docs_tf"
awk -f "$terraform_docs_awk_file" ./*.tf >"$tmp_file_docs_tf"
# shellcheck disable=SC2086
terraform-docs md $args "$tmp_file_docs_tf" > "$tmp_file"
terraform-docs md $args "$tmp_file_docs_tf" >"$tmp_file"
rm -f "$tmp_file_docs_tf"
fi

Expand All @@ -199,7 +188,7 @@ function terraform_docs {

rm -f "$tmp_file"

popd > /dev/null
popd >/dev/null
done
}

Expand All @@ -209,10 +198,10 @@ function terraform_docs {
# Arguments:
# output_file (string) filename where hack will be written to
#######################################################################
function terraform_docs_awk {
function terraform_docs_awk() {
local -r output_file=$1

cat << "EOF" > "$output_file"
cat <<"EOF" >"$output_file"
# This script converts Terraform 0.12 variables/outputs to something suitable for `terraform-docs`
# As of terraform-docs v0.6.0, HCL2 is not supported. This script is a *dirty hack* to get around it.
# https://github.com/terraform-docs/terraform-docs/
Expand Down Expand Up @@ -368,4 +357,26 @@ EOF

}


#######################################
# main function
# Globals:
# ARGS
# FILES
# HOOK_CONFIG
# SCRIPT_DIR
# Arguments:
# None
#######################################
function main() {
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[@]}"
}

[ "${BASH_SOURCE[0]}" != "$0" ] || main "$@"
34 changes: 22 additions & 12 deletions hooks/terraform_fmt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ 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
terraform_fmt_ "${ARGS[*]}" "${FILES[@]}"
}

#######################################################################
# Hook execution boilerplate logic which is common to hooks, that run
# on per dir basis. Little bit extended than `common::per_dir_hook`
Expand All @@ -26,7 +19,7 @@ function main {
# args (string with array) arguments that configure wrapped tool behavior
# files (array) filenames to check
#######################################################################
function terraform_fmt_ {
function terraform_fmt_() {
local -r args="$1"
shift 1
local -a -r files=("$@")
Expand Down Expand Up @@ -54,7 +47,7 @@ function terraform_fmt_ {
# 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
pushd "$dir_path" >/dev/null || continue

per_dir_hook_unique_part "$args" "$dir_path"

Expand All @@ -63,14 +56,14 @@ function terraform_fmt_ {
final_exit_code=$exit_code
fi

popd > /dev/null
popd >/dev/null
done

# TODO: Unique part
# 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 +88,7 @@ function terraform_fmt_ {
# Outputs:
# If failed - print out hook checks status
#######################################################################
function per_dir_hook_unique_part {
function per_dir_hook_unique_part() {
local -r args="$1"
local -r dir_path="$2"

Expand All @@ -108,4 +101,21 @@ function per_dir_hook_unique_part {
return $exit_code
}


#######################################
# main function
# Globals:
# ARGS
# FILES
# SCRIPT_DIR
# Arguments:
# None
#######################################
function main() {
common::initialize "$SCRIPT_DIR"
common::parse_cmdline "$@"
# shellcheck disable=SC2153 # False positive
terraform_fmt_ "${ARGS[*]}" "${FILES[@]}"
}

[ "${BASH_SOURCE[0]}" != "$0" ] || main "$@"
Loading