Skip to content

Commit

Permalink
Update 1
Browse files Browse the repository at this point in the history
  • Loading branch information
baolsen committed Oct 28, 2022
1 parent a7e3d90 commit ca50529
Showing 1 changed file with 42 additions and 13 deletions.
55 changes: 42 additions & 13 deletions hooks/terraform_validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ function per_dir_hook_unique_part {
local -a -r args=("$@")

local exit_code

#
# Get hook settings
#
Expand All @@ -64,31 +63,61 @@ function per_dir_hook_unique_part {
esac
done

common::terraform_init 'terraform validate' "$dir_path" || {
exit_code=$?
return $exit_code
}

function do_validate {
validate_output=$(terraform validate "${args[@]}" 2>&1)
exit_code=$?
return $exit_code
}

function parse_validate {
# Requires jq
local exit_code
local validate_output
local valid
local summary

common::terraform_init 'terraform validate' "$dir_path" || {
exit_code=$?
return $exit_code
}

# pass the arguments to hook
validate_output=$(terraform validate "${args[@]}" 2>&1)
validate_output=$(terraform validate -json "${args[@]}" 2>&1)
exit_code=$?

valid="$(jq -rc '.valid' <<< "$validate_output")"

if [ "$valid" == "true" ]; then
return 0
fi

# Pretty-print error information
echo "$validate_output" | jq '.diagnostics[]'

# Parse error message
while IFS= read -r error_message; do
summary="$(jq -rc '.summary' <<< "$error_message")"
case $summary in
"missing or corrupted provider plugins")
return 10
;;
esac
done < <(jq -rc '.diagnostics[]' <<< "$validate_output")
return $exit_code
}

do_validate
exit_code=$?
if [ "$retry_once_with_cleanup" = true ]; then
parse_validate
exit_code=$?
else
do_validate
exit_code=$?
fi

if [ $exit_code -ne 0 ] && [ "$retry_once_with_cleanup" = true ]; then
if [ $exit_code -eq 10 ] && [ "$retry_once_with_cleanup" = true ]; then
if [ -d .terraform ]; then
# Will only be displayed if validation fails again.
common::colorify "yellow" "Validation failed. Re-initialising: $dir_path"
rm -r .terraform
common::colorify "yellow" "Validation failed. Removing .terraform from: $dir_path"
rm -rf .terraform
do_validate
exit_code=$?
fi
Expand Down

0 comments on commit ca50529

Please sign in to comment.