chore: Refactor all hooks (#310)

This commit is contained in:
Maksym Vlasov 2022-01-06 13:21:52 +02:00 committed by GitHub
commit 1f16f09c6f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 675 additions and 340 deletions

View file

@ -1,35 +1,31 @@
#!/usr/bin/env bash
set -eo pipefail
main() {
initialize_
parse_cmdline_ "$@"
function main {
common::initialize
common::parse_cmdline "$@"
# Support for setting relative PATH to .terraform-docs.yml config.
ARGS=${ARGS[*]/--config=/--config=$(pwd)\/}
terraform_docs_ "${HOOK_CONFIG[*]}" "$ARGS" "${FILES[@]}"
}
initialize_() {
function common::initialize {
local SCRIPT_DIR
# get directory containing this script
local dir
local source
source="${BASH_SOURCE[0]}"
while [[ -L $source ]]; do # resolve $source until the file is no longer a symlink
dir="$(cd -P "$(dirname "$source")" > /dev/null && pwd)"
source="$(readlink "$source")"
# if $source was a relative symlink, we need to resolve it relative to the path where the symlink file was located
[[ $source != /* ]] && source="$dir/$source"
done
_SCRIPT_DIR="$(dirname "$source")"
SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
# source getopt function
# shellcheck source=lib_getopt
. "$_SCRIPT_DIR/lib_getopt"
. "$SCRIPT_DIR/lib_getopt"
}
parse_cmdline_() {
declare argv
argv=$(getopt -o a: --long args:,hook-config: -- "$@") || return
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
@ -39,9 +35,9 @@ parse_cmdline_() {
ARGS+=("$1")
shift
;;
--hook-config)
-h | --hook-config)
shift
HOOK_CONFIG+=("$1")
HOOK_CONFIG+=("$1;")
shift
;;
--)
@ -53,12 +49,15 @@ parse_cmdline_() {
done
}
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"
local hack_terraform_docs
hack_terraform_docs=$(terraform version | sed -n 1p | grep -c 0.12) || true
@ -72,7 +71,7 @@ terraform_docs_() {
if [[ -z "$is_old_terraform_docs" ]]; then # Using terraform-docs 0.8+ (preferred)
terraform_docs "0" "$hook_config" "$args" "${files[@]}"
terraform_docs "0" "${configs[*]}" "$args" "${files[@]}"
elif [[ "$hack_terraform_docs" == "1" ]]; then # Using awk script because terraform-docs is older than 0.8 and terraform 0.12 is used
@ -84,17 +83,17 @@ terraform_docs_() {
local tmp_file_awk
tmp_file_awk=$(mktemp "${TMPDIR:-/tmp}/terraform-docs-XXXXXXXXXX")
terraform_docs_awk "$tmp_file_awk"
terraform_docs "$tmp_file_awk" "$hook_config" "$args" "${files[@]}"
terraform_docs "$tmp_file_awk" "${configs[*]}" "$args" "${files[@]}"
rm -f "$tmp_file_awk"
else # Using terraform 0.11 and no awk script is needed for that
terraform_docs "0" "$hook_config" "$args" "${files[@]}"
terraform_docs "0" "${configs[*]}" "$args" "${files[@]}"
fi
}
terraform_docs() {
function terraform_docs {
local -r terraform_docs_awk_file="$1"
local -r hook_config="$2"
local -r args="$3"
@ -141,11 +140,11 @@ terraform_docs() {
esac
done
local path_uniq
for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do
path_uniq="${path_uniq//__REPLACED__SPACE__/ }"
local dir_path
for dir_path in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do
dir_path="${dir_path//__REPLACED__SPACE__/ }"
pushd "$path_uniq" > /dev/null
pushd "$dir_path" > /dev/null || continue
#
# Create file if it not exist and `--create-if-not-exist=true` provided
@ -212,7 +211,7 @@ terraform_docs() {
done
}
terraform_docs_awk() {
function terraform_docs_awk {
local -r output_file=$1
cat << "EOF" > "$output_file"
@ -371,9 +370,4 @@ EOF
}
# global arrays
declare -a ARGS=()
declare -a FILES=()
declare -a HOOK_CONFIG=()
[[ ${BASH_SOURCE[0]} != "$0" ]] || main "$@"
[ "${BASH_SOURCE[0]}" != "$0" ] || main "$@"