2018-01-24 12:13:51 +01:00
|
|
|
#!/usr/bin/env bash
|
2018-01-24 13:34:34 +01:00
|
|
|
set -e
|
2018-01-24 12:13:51 +01:00
|
|
|
|
2018-01-24 13:34:34 +01:00
|
|
|
declare -a paths
|
|
|
|
|
index=0
|
2019-02-21 19:38:50 +11:00
|
|
|
error=0
|
2018-01-24 13:34:34 +01:00
|
|
|
|
|
|
|
|
for file_with_path in "$@"; do
|
2018-04-21 11:22:47 +02:00
|
|
|
file_with_path="${file_with_path// /__REPLACED__SPACE__}"
|
|
|
|
|
|
2018-01-24 13:34:34 +01:00
|
|
|
paths[index]=$(dirname "$file_with_path")
|
2020-01-21 11:54:13 +01:00
|
|
|
(("index+=1"))
|
2018-01-24 13:34:34 +01:00
|
|
|
done
|
|
|
|
|
|
|
|
|
|
for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do
|
2018-04-21 11:22:47 +02:00
|
|
|
path_uniq="${path_uniq//__REPLACED__SPACE__/ }"
|
|
|
|
|
|
2020-04-04 02:17:25 -04:00
|
|
|
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
|
2019-02-21 19:38:50 +11:00
|
|
|
error=1
|
2018-05-24 21:10:49 +01:00
|
|
|
echo
|
|
|
|
|
echo "Failed path: $path_uniq"
|
|
|
|
|
echo "================================"
|
|
|
|
|
fi
|
2020-04-04 02:17:25 -04:00
|
|
|
cd "$starting_path"
|
2018-01-24 13:48:44 +01:00
|
|
|
fi
|
2018-01-24 12:13:51 +01:00
|
|
|
done
|
2019-02-21 19:38:50 +11:00
|
|
|
|
2020-01-21 11:54:13 +01:00
|
|
|
if [[ "${error}" -ne 0 ]]; then
|
2019-02-21 19:38:50 +11:00
|
|
|
exit 1
|
|
|
|
|
fi
|