forked from antonbabenko/pre-commit-terraform
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Honor excludes; don't run against entire directories
Fixes antonbabenko#121
- Loading branch information
1 parent
2e40ade
commit f3c0a3b
Showing
1 changed file
with
2 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,9 @@ | ||
#!/usr/bin/env bash | ||
set -e | ||
|
||
declare -a paths | ||
declare -a tfvars_files | ||
|
||
index=0 | ||
|
||
for file_with_path in "$@"; do | ||
file_with_path="${file_with_path// /__REPLACED__SPACE__}" | ||
|
||
paths[index]=$(dirname "$file_with_path") | ||
|
||
if [[ "$file_with_path" == *".tfvars" ]]; then | ||
tfvars_files+=("$file_with_path") | ||
fi | ||
|
||
let "index+=1" | ||
done | ||
|
||
for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do | ||
path_uniq="${path_uniq//__REPLACED__SPACE__/ }" | ||
|
||
pushd "$path_uniq" > /dev/null | ||
terraform fmt | ||
pushd $(dirname "$file_with_path") | ||
terraform fmt $(basename "$file_with_path") | ||
popd > /dev/null | ||
done | ||
|
||
# terraform.tfvars are excluded by `terraform fmt` | ||
for tfvars_file in "${tfvars_files[@]}"; do | ||
tfvars_file="${tfvars_file//__REPLACED__SPACE__/ }" | ||
|
||
terraform fmt "$tfvars_file" | ||
done |