Skip to content

Commit

Permalink
Validate terraform in src/core (Azure#36)
Browse files Browse the repository at this point in the history
* validate terraform in src/core

* check if src/core exists

Co-authored-by: Glenn Musa <[email protected]>
  • Loading branch information
glennmusa and glennmusa authored Feb 25, 2021
1 parent bdca9cc commit 46e72d4
Showing 1 changed file with 31 additions and 25 deletions.
56 changes: 31 additions & 25 deletions build/validate_tf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,38 @@ if ! command -v terraform &> /dev/null; then
exit 1
fi

# Navigate to the root of the repository
cd .. || exit
rootdir=$(pwd)
full_path=$(realpath "${0}")
repo_path=$(dirname "$(dirname "${full_path}")")
core_path="${repo_path}/src/core"

# Validate all .tf files
program_log "Validating all terraform"
for i in $(find . -name "*.tf" -printf "%h\n" | sort --unique)
do
cd "${i}" || exit
echo "validating ${i}..."
terraform init -backend=false >> /dev/null || exit 1
terraform validate >> /dev/null || exit 1
cd "${rootdir}" || exit
done
program_log "All terraform validated successfully"

# Check formatting for all .tf files
program_log "Linting all terraform"
if terraform fmt -check -recursive >> /dev/null;
if [ -d "$core_path" ];
then
program_log "All terraform linted successfully"
else
linting_results=$(terraform fmt -check -recursive)
for j in $linting_results
# Validate all .tf and their dependencies in core_path
program_log "Validating Terraform..."
cd "${core_path}" || exit
for i in $(find . -name "*.tf" -printf "%h\n" | sort --unique)
do
error_log "please format '${j}' with the command 'terraform fmt'"
cd "${i}" || exit
echo "validating ${i}..."
terraform init -backend=false >> /dev/null || exit 1
terraform validate >> /dev/null || exit 1
cd "${core_path}" || exit
done
exit 1;
fi
program_log "Terraform validated successfully!"

# Check formatting in all .tf files in repo
program_log "Linting Terraform..."
cd "${repo_path}" || exit
if terraform fmt -check -recursive >> /dev/null;
then
program_log "Terraform linted successfully!"
else
linting_results=$(terraform fmt -check -recursive)
for j in $linting_results
do
error_log "please format '${j}' with the command 'terraform fmt'"
done
program_log "alternatively, you can run 'terraform fmt -recursive' to format all *.tf in a directory"
exit 1;
fi
fi

0 comments on commit 46e72d4

Please sign in to comment.