Skip to content

Instantly share code, notes, and snippets.

@algermissen
Created August 15, 2021 15:55
Show Gist options
  • Save algermissen/a59b40d714dea0e2f0663c17150bd291 to your computer and use it in GitHub Desktop.
Save algermissen/a59b40d714dea0e2f0663c17150bd291 to your computer and use it in GitHub Desktop.
#!/bin/sh
# To use, store as .git/hooks/pre-commit inside your repository and make sure
# it has execute permissions.
gofiles=$(git diff --cached --name-only --diff-filter=ACM | grep '.go$')
if [ ! -z "$gofiles" ]; then
unformatted=$(goimports -l $gofiles)
if [ ! -z "$unformatted" ] ; then
# Some files are not goimports'd. Print message and fail.
echo >&2 "Go files must be formatted with goimports. Please run:"
for fn in $unformatted; do
echo >&2 " goimports -w $PWD/$fn"
done
fi
fi
tffiles=$(git diff --cached --name-only --diff-filter=ACM | grep '.tf$')
if [ ! -z "$tffiles" ]; then
unformatted=$(terraform fmt -check $tffiles)
if [ ! -z "$unformatted" ] ; then
# Some files are not terraform fmt'd. Print message and fail.
echo >&2 "Terraform files must be formatted with goimports. Please run:"
for fn in $unformatted; do
echo >&2 " terraform fmt -write=true $PWD/$fn"
done
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment