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

fix: Change terraform_validate hook functionality for subdirectories with terraform files #100

Merged
merged 5 commits into from
Apr 4, 2020
Merged
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
2 changes: 1 addition & 1 deletion .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
exclude: \.terraform\/.*$

- id: terraform_validate
name: Terraform validate without variables
name: Terraform validate
description: Validates all Terraform configuration files.
entry: terraform_validate.sh
language: script
Expand Down
24 changes: 22 additions & 2 deletions terraform_validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,33 @@ done
for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do
path_uniq="${path_uniq//__REPLACED__SPACE__/ }"

if [[ -n "$(find . -maxdepth 1 -name '*.tf' -print -quit)" ]]; then
if ! terraform validate $path_uniq; then
if [[ -n "$(find $path_uniq -maxdepth 1 -name '*.tf' -print -quit)" ]]; then

starting_path=$(realpath "$path_uniq")
terraform_path="$path_uniq"

# Find the relevant .terraform directory (indicating a 'terraform init'),
# but fall through to the current directory.
while [[ "$terraform_path" != "." ]]; do
if [[ -d "$terraform_path/.terraform" ]]; then
break
else
terraform_path=$(dirname "$terraform_path")
fi
done

validate_path="${path_uniq#"$terraform_path"}"

# Change to the directory that has been initialized, run validation, then
# change back to the starting directory.
cd "$(realpath "$terraform_path")"
if ! terraform validate $validate_path; then
error=1
echo
echo "Failed path: $path_uniq"
echo "================================"
fi
cd "$starting_path"
fi
done

Expand Down