Created
October 18, 2016 21:54
-
-
Save jamtur01/a567078b7ba545c3492f7cd32a65450d to your computer and use it in GitHub Desktop.
A Terraform validation and formatting pre-commit hook
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
#!/usr/bin/env bash | |
set -e | |
# Formats any *.tf files according to the hashicorp convention | |
files=$(git diff --cached --name-only) | |
for f in $files | |
do | |
if [ -e "$f" ] && [[ $f == *.tf ]]; then | |
#terraform validate `dirname $f` | |
terraform fmt $f | |
git add $f | |
fi | |
done |
Add --diff-filter=d
to the git diff
command to filter deleted files, can't run fmt
on those.
Format tfvars files [[ "${f##*.}" =~ ^(tf|tfvars)$ ]]
Format tfvars files
[[ "${f##*.}" =~ ^(tf|tfvars)$ ]]
can you explain this regex?
I like to avoid magic git add
as well.
Here is @eschwartz suggested version with extra error message
#!/usr/bin/env bash
set -e
# Formats any *.tf files according to the hashicorp convention
files=$(git diff --cached --name-only)
for f in $files
do
if [ -e "$f" ] && [[ "${f##*.}" =~ ^(tf|tfvars)$ ]]; then
terraform fmt -check=true $f || (echo "pre-hook: Please apply 'terraform fmt' for file $f before commiting or remove this file from stage" && false )
fi
done
Extending the previous ideas: avoid automatically running git add
, however automatically run terraform fmt
and let the user the responsibility to stage changes or not.
#!/usr/bin/env bash
set -e
# Formats any *.tf files according to the hashicorp convention
files=$(git diff --cached --name-only)
for f in $files
do
if [ -e "$f" ] && [[ $f == *.tf ]]; then
terraform fmt -check=true $f > /dev/null || {
echo "pre-hook: Incorrect formatting, let me fix this for you"
cmd="terraform fmt -recursive"
echo "Running '$cmd'..."
$cmd
echo "Done. You may now stage changes."
}
fi
done
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is great, thanks!
The one issue I see is that it will fully stage any files output by
git diff
If you have any partially staged files,
this hook will fully stage them.
I might suggest something like this:
which will
exit 1
if any files are not properly formatted.Not as automagical, but it will also prevent any unintended consequences