Skip to content

Instantly share code, notes, and snippets.

@hermesespinola
Last active April 9, 2021 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hermesespinola/652b9145906518959632dab48360510c to your computer and use it in GitHub Desktop.
Save hermesespinola/652b9145906518959632dab48360510c to your computer and use it in GitHub Desktop.
Apply terraform fmt before a git commit
TF_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -e '\.tf$')
if [ ! -z $TF_FILES ]; then
echo $TF_FILES | xargs -I'{}' -n1 sh -c "terraform fmt -list=false '{}'"
if [ $? -eq 1 ]; then
echo '⚠️ Aborting commit due to terraform errors'
exit 1
else
echo '✨ Terraform files looking good'
echo $TF_FILES | xargs -I'{}' -n1 sh -c "git add '{}'"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment