From 83bbdbfa637e21e5da3cba5e888208abc03eedf1 Mon Sep 17 00:00:00 2001 From: Robin Bowes Date: Mon, 6 Jul 2020 15:26:47 +0100 Subject: [PATCH] Refactor/fixup terraform_tflint --- terraform_tflint.sh | 56 +++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/terraform_tflint.sh b/terraform_tflint.sh index 426a7b197..60a66447c 100755 --- a/terraform_tflint.sh +++ b/terraform_tflint.sh @@ -1,31 +1,11 @@ #!/usr/bin/env bash + set -e main() { initialize_ - declare argv - argv=$(getopt -o a: --long args: -- "$@") || return - eval "set -- $argv" - - declare args - declare -a files - - for argv; do - case $argv in - -a | --args) - shift - args="$1" - shift - ;; - --) - shift - read -r -a files <<<"$@" - break - ;; - esac - done - - tflint_ "$args" "${files[@]}" + parse_cmdline_ "$@" + tflint_ } initialize_() { @@ -46,8 +26,30 @@ initialize_() { . "$_SCRIPT_DIR/lib_getopt" } +parse_cmdline_() { + declare argv + argv=$(getopt -o a: --long args: -- "$@") || return + eval "set -- $argv" + + for argv; do + case $argv in + -a | --args) + shift + ARGS+=("$1") + shift + ;; + --) + shift + FILES=("$@") + break + ;; + esac + done +} + tflint_() { - for file_with_path in "${files[@]}"; do + local index=0 + for file_with_path in "${FILES[@]}"; do file_with_path="${file_with_path// /__REPLACED__SPACE__}" paths[index]=$(dirname "$file_with_path") @@ -59,9 +61,13 @@ tflint_() { path_uniq="${path_uniq//__REPLACED__SPACE__/ }" pushd "$path_uniq" > /dev/null - tflint "$args" + tflint "${ARGS[@]}" popd > /dev/null done } +# global arrays +declare -a ARGS +declare -a FILES + [[ ${BASH_SOURCE[0]} != "$0" ]] || main "$@"