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

feat: Add support for relative path for tflint #264

Closed
wants to merge 1 commit into from
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,12 @@ Example:
- --args=--enable-rule=terraform_documented_variables
```

2. When you have multiple directories and want to run `tflint` in all of them and share a single config file, it is impractical to hard-code the path to `.tflint.hcl` file. The solution is to use the `__GIT_WORKING_DIR__` placeholder which will be replaced by `terraform_tflint` hooks with Git working directory (repo root) at run time. For example:
2. When you have multiple directories and want to run `tflint` in all of them and share a single config file, it is impractical to hard-code the path to `.tflint.hcl` file. The solution is to use relative path to file which will be replaced by `terraform_tflint` hooks with Git working directory (repo root) at run time. For example:

```yaml
- id: terraform_tflint
args:
- --args=--config=__GIT_WORKING_DIR__/.tflint.hcl
- --args=--config=.tflint.hcl
```


Expand Down
71 changes: 43 additions & 28 deletions terraform_tflint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,56 @@

set -eo pipefail

main() {
initialize_
parse_cmdline_ "$@"
tflint_
function main {
common::initialize
common::parse_cmdline "$@"
#! Avoiding breaking changes crutch. Will be simplified on
#! https://github.com/antonbabenko/pre-commit-terraform/issues/262
crutch="$(echo "${ARGS[*]}" | grep -oe '--config=/' || exit 0)"
if [ "$crutch" != "" ]; then
ARGS="${ARGS[*]}"
else
# Support for setting relative PATH to config.
ARGS=${ARGS[*]/--config=/--config=$(pwd)\/}
fi

tflint_ "$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: -- "$@") || return
# common global arrays.
# Populated in `parse_cmdline` and can used in hooks functions
declare -a ARGS=()
declare -a HOOK_CONFIG=()
declare -a FILES=()
function common::parse_cmdline {
local argv
argv=$(getopt -o a:,h: --long args:,hook-config: -- "$@") || return
eval "set -- $argv"

for argv; do
case $argv in
-a | --args)
shift
#! Avoiding breaking changes crutch. Will be removed in
#! https://github.com/antonbabenko/pre-commit-terraform/issues/262
expanded_arg="${1//__GIT_WORKING_DIR__/$PWD}"
ARGS+=("$expanded_arg")
# ARGS+=("$1")
shift
;;
-h | --hook-config)
shift
HOOK_CONFIG+=("$1;")
shift
;;
--)
Expand All @@ -46,12 +61,16 @@ parse_cmdline_() {
;;
esac
done

}

tflint_() {
function tflint_ {
local args
read -r -a args <<< "$1"
local files
read -r -a files <<< "$2"

local index=0
for file_with_path in "${FILES[@]}"; do
for file_with_path in "${files[@]}"; do
file_with_path="${file_with_path// /__REPLACED__SPACE__}"

paths[index]=$(dirname "$file_with_path")
Expand All @@ -65,17 +84,13 @@ tflint_() {

# Print checked PATH **only** if TFLint have any messages
# shellcheck disable=SC2091 # Suppress error output
$(tflint "${ARGS[@]}" 2>&1) 2> /dev/null || {
$(tflint "${args[@]}" 2>&1) 2> /dev/null || {
echo >&2 -e "\033[1;31m\nERROR in $path_uniq/:\033[0m"
tflint "${ARGS[@]}"
tflint "${args[@]}"
}

popd > /dev/null
done
}

# global arrays
declare -a ARGS
declare -a FILES

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