From fcc8773b22acd6a9776e5c1c10212355bb6f1c69 Mon Sep 17 00:00:00 2001 From: Anton Babenko Date: Tue, 22 Sep 2020 13:11:34 +0200 Subject: [PATCH 1/7] WIP --- terraform_tflint.sh | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/terraform_tflint.sh b/terraform_tflint.sh index e7a212b89..f36f40d18 100755 --- a/terraform_tflint.sh +++ b/terraform_tflint.sh @@ -1,8 +1,12 @@ #!/usr/bin/env bash -set -e +#set -eo pipefail +set -eo pipefail main() { + echo "====" + echo "$@" + initialize_ parse_cmdline_ "$@" tflint_ @@ -31,10 +35,19 @@ parse_cmdline_() { argv=$(getopt -o a: --long args: -- "$@") || return eval "set -- $argv" + echo "fff" + echo "${argv[@]}" + echo "${argv}" for argv; do case $argv in -a | --args) shift + # echo "ddd" + # echo "aaa=$1" + # expanded_arg="${1//__GIT_REPO_DIR__/pwd}" + # expanded_arg="${1//__GIT_REPO_DIR__/pwd}" + # echo "bbb=$expanded_arg" + # ARGS+=("$expanded_arg") ARGS+=("$1") shift ;; @@ -45,6 +58,9 @@ parse_cmdline_() { ;; esac done + + # ARGS+=("--config=$PWD/.tflint.hcl") + } tflint_() { @@ -57,6 +73,9 @@ tflint_() { ((index += 1)) done + #echo "ARGS=====" + #echo "${ARGS[@]}" + for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do path_uniq="${path_uniq//__REPLACED__SPACE__/ }" @@ -70,4 +89,7 @@ tflint_() { declare -a ARGS declare -a FILES +#echo "ABBBBBBSSSS=" +#pwd + [[ ${BASH_SOURCE[0]} != "$0" ]] || main "$@" From 4e67308708d113ff53aca0fbb22f61b41ccad102 Mon Sep 17 00:00:00 2001 From: Anton Babenko Date: Tue, 22 Sep 2020 13:15:28 +0200 Subject: [PATCH 2/7] WIP --- terraform_tflint.sh | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/terraform_tflint.sh b/terraform_tflint.sh index f36f40d18..44b0ddd3c 100755 --- a/terraform_tflint.sh +++ b/terraform_tflint.sh @@ -44,10 +44,9 @@ parse_cmdline_() { shift # echo "ddd" # echo "aaa=$1" - # expanded_arg="${1//__GIT_REPO_DIR__/pwd}" - # expanded_arg="${1//__GIT_REPO_DIR__/pwd}" - # echo "bbb=$expanded_arg" - # ARGS+=("$expanded_arg") + expanded_arg="${1//__GIT_WORKING_DIR__/$PWD}" + echo "bbb=$expanded_arg" +# ARGS+=("$expanded_arg") ARGS+=("$1") shift ;; From 39401841423e2c630a40d137267e412625b953eb Mon Sep 17 00:00:00 2001 From: Anton Babenko Date: Tue, 22 Sep 2020 13:16:46 +0200 Subject: [PATCH 3/7] WIP --- terraform_tflint.sh | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/terraform_tflint.sh b/terraform_tflint.sh index 44b0ddd3c..d119f9691 100755 --- a/terraform_tflint.sh +++ b/terraform_tflint.sh @@ -42,12 +42,10 @@ parse_cmdline_() { case $argv in -a | --args) shift - # echo "ddd" - # echo "aaa=$1" - expanded_arg="${1//__GIT_WORKING_DIR__/$PWD}" - echo "bbb=$expanded_arg" -# ARGS+=("$expanded_arg") - ARGS+=("$1") + expanded_arg="${1//__GIT_WORKING_DIR__/$PWD}" +# echo "bbb=$expanded_arg" + ARGS+=("$expanded_arg") +# ARGS+=("$1") shift ;; --) From 9428b52bd16aec97b3fe3147d930328095d594f4 Mon Sep 17 00:00:00 2001 From: Anton Babenko Date: Tue, 22 Sep 2020 13:18:47 +0200 Subject: [PATCH 4/7] WIP --- terraform_tflint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/terraform_tflint.sh b/terraform_tflint.sh index d119f9691..037b0016e 100755 --- a/terraform_tflint.sh +++ b/terraform_tflint.sh @@ -76,7 +76,7 @@ tflint_() { for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do path_uniq="${path_uniq//__REPLACED__SPACE__/ }" - pushd "$path_uniq" > /dev/null + pushd "$path_uniq" # > /dev/null tflint "${ARGS[@]}" popd > /dev/null done From 68f7b0240e311f5a01d519256ddbf5ee801e08e9 Mon Sep 17 00:00:00 2001 From: Anton Babenko Date: Tue, 22 Sep 2020 13:19:44 +0200 Subject: [PATCH 5/7] WIP --- terraform_tflint.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/terraform_tflint.sh b/terraform_tflint.sh index 037b0016e..ede0f3fad 100755 --- a/terraform_tflint.sh +++ b/terraform_tflint.sh @@ -76,6 +76,8 @@ tflint_() { for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do path_uniq="${path_uniq//__REPLACED__SPACE__/ }" + env + pushd "$path_uniq" # > /dev/null tflint "${ARGS[@]}" popd > /dev/null From b02bc8d6481ea7e1ea01c9d533a8c87012db700d Mon Sep 17 00:00:00 2001 From: Anton Babenko Date: Tue, 22 Sep 2020 13:28:56 +0200 Subject: [PATCH 6/7] WIP --- terraform_tflint.sh | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-) diff --git a/terraform_tflint.sh b/terraform_tflint.sh index ede0f3fad..670e860ad 100755 --- a/terraform_tflint.sh +++ b/terraform_tflint.sh @@ -1,12 +1,8 @@ #!/usr/bin/env bash -#set -eo pipefail set -eo pipefail main() { - echo "====" - echo "$@" - initialize_ parse_cmdline_ "$@" tflint_ @@ -35,17 +31,12 @@ parse_cmdline_() { argv=$(getopt -o a: --long args: -- "$@") || return eval "set -- $argv" - echo "fff" - echo "${argv[@]}" - echo "${argv}" for argv; do case $argv in -a | --args) shift expanded_arg="${1//__GIT_WORKING_DIR__/$PWD}" -# echo "bbb=$expanded_arg" ARGS+=("$expanded_arg") -# ARGS+=("$1") shift ;; --) @@ -56,8 +47,6 @@ parse_cmdline_() { esac done - # ARGS+=("--config=$PWD/.tflint.hcl") - } tflint_() { @@ -70,15 +59,10 @@ tflint_() { ((index += 1)) done - #echo "ARGS=====" - #echo "${ARGS[@]}" - for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do path_uniq="${path_uniq//__REPLACED__SPACE__/ }" - env - - pushd "$path_uniq" # > /dev/null + pushd "$path_uniq" > /dev/null tflint "${ARGS[@]}" popd > /dev/null done @@ -88,7 +72,4 @@ tflint_() { declare -a ARGS declare -a FILES -#echo "ABBBBBBSSSS=" -#pwd - [[ ${BASH_SOURCE[0]} != "$0" ]] || main "$@" From 433a0771c3a51e9526527f3aae5a828e24d04f87 Mon Sep 17 00:00:00 2001 From: Anton Babenko Date: Tue, 22 Sep 2020 14:16:24 +0200 Subject: [PATCH 7/7] Updated documentation with info about shared .tflint.hcl --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 53d14b916..c54a5ec56 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,16 @@ if they are present in `README.md`. - '--args=--enable-rule=terraform_documented_variables' ``` +1. When you have multiple directories and want to run `tflint` in all of them and share single config file it is impractical to hard-code the path to `.tflint.hcl` file. The solution is to use `__GIT_WORKING_DIR__` placeholder which will be replaced by `terraform_tflint` hooks with Git working directory (repo root) at run time. For example: + + ```yaml + hooks: + - id: terraform_tflint + args: + - '--args=--config=__GIT_WORKING_DIR__/.tflint.hcl' + ``` + + ## Notes about terraform_tfsec hooks 1. `terraform_tfsec` will consume modified files that pre-commit