Skip to content

Commit

Permalink
(feat): hook-config enable delegation of pushing/poping
Browse files Browse the repository at this point in the history
  • Loading branch information
lexton committed Apr 28, 2023
1 parent e1d78ae commit 61256b5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,14 @@ To replicate functionality in `terraform_docs` hook:
- --args=--config=__GIT_WORKING_DIR__/.tflint.hcl
```

3. When you want the root relative paths to the linting error messages rather than just module relative paths. You need to use TFlint native `--chdir` functionality. Note: this requires TFlint version 0.44.0 or later. For example:

```yaml
- id: terraform_tflint
args:
- --hook-config=--skip-chdir
```


### terraform_tfsec

Expand Down
29 changes: 27 additions & 2 deletions hooks/_common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,25 @@ function common::per_dir_hook {
((index += 1))
done

# Lookup hook-config for modifiers that impact common behavior
local SKIP_CHDIR=false
IFS=";" read -r -a configs <<< "${HOOK_CONFIG[*]}"
for c in "${configs[@]}"; do
IFS="=" read -r -a config <<< "$c"
key=${config[0]}
value=${config[1]}

case $key in
--skip-chdir)
# this flag will skip pushing and popping directories
# delegating the responsibility to the hooked plugin/binary
if [ "$value" != "false" ]; then
SKIP_CHDIR=true
fi
;;
esac
done

# preserve errexit status
shopt -qo errexit && ERREXIT_IS_SET=true
# allow hook to continue if exit_code is greater than 0
Expand All @@ -226,7 +245,10 @@ function common::per_dir_hook {
# 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

if ! $SKIP_CHDIR; then
pushd "$dir_path" > /dev/null || continue
fi

per_dir_hook_unique_part "$dir_path" "${args[@]}"

Expand All @@ -235,7 +257,10 @@ function common::per_dir_hook {
final_exit_code=$exit_code
fi

popd > /dev/null
if ! $SKIP_CHDIR; then
popd > /dev/null
fi

done

# restore errexit if it was set before the "for" loop
Expand Down
27 changes: 20 additions & 7 deletions hooks/terraform_tflint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,29 @@ function per_dir_hook_unique_part {
shift
local -a -r args=("$@")

# Print checked PATH **only** if TFLint have any messages
# shellcheck disable=SC2091,SC2068 # Suppress error output
$(tflint ${args[@]} 2>&1) 2> /dev/null || {
common::colorify "yellow" "TFLint in $dir_path/:"
IFS=";" read -r -a configs <<< "${HOOK_CONFIG[*]}"
for c in "${configs[@]}"; do
IFS="=" read -r -a config <<< "$c"
key=${config[0]}
value=${config[1]}

tflint "${args[@]}"
}
case $key in
--skip-chdir)
if [ "$value" != "false" ]; then
DIR_ARGS="--chdir=$dir_path"
fi
;;
esac
done

# return exit code to common::per_dir_hook
COMMAND_OUTPUT=$({ tflint "${DIR_ARGS}" "${args[@]}"; } 2>&1)
local exit_code=$?

if [ $exit_code -ne 0 ]; then
common::colorify "yellow" "TFLint in $dir_path/:"
echo "$COMMAND_OUTPUT"
fi

return $exit_code
}

Expand Down

0 comments on commit 61256b5

Please sign in to comment.