Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

terraform-fmt updates files, optionally skipped via flags #53

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,21 @@ Now when the pre-commit hook runs, it will call `helm lint` with both `linter_va
helm lint -f values.yaml -f linter_values.yaml .
```

## terraform-fmt arguments

By default, `terraform-fmt` will write changes back to the original file.
You can skip this by setting the `--no-autofix` flag.

```yaml
repos:
- repo: https://github.com/gruntwork-io/pre-commit
rev: <VERSION>
hooks:
- id: terraform-fmt
args: ["--no-autofix"]
```


## Shellcheck Arguments

To enable optional shellcheck features you can use the `--enable` flag.
Expand Down
36 changes: 34 additions & 2 deletions hooks/terraform-fmt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,43 @@ set -e
# workaround to allow GitHub Desktop to work, add this (hopefully harmless) setting here.
export PATH=$PATH:/usr/local/bin

write_changes=true
FILES=()

parse_arguments() {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inspired on shellcheck.sh's version:

parse_arguments() {

while (($# > 0)); do
# Grab param and value splitting on " " or "=" with parameter expansion
local PARAMETER="${1%[ =]*}"
local VALUE="${1#*[ =]}"
if [[ "$PARAMETER" == "$VALUE" ]]; then VALUE="$2"; fi
shift
case "$PARAMETER" in
--no-autofix)
write_changes=false
;;
-*)
echo "Error: Unknown option: $PARAMETER" >&2
exit 1
;;
*)
FILES+=("$PARAMETER")
;;
esac
done
}

parse_arguments "$@"

# Store and return last failure from fmt so this can validate every directory passed before exiting
FMT_ERROR=0

for file in "$@"; do
terraform fmt -diff -check "$file" || FMT_ERROR=$?
for file in "$FILES"; do
file=$(dirname "$file")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is required to be able to run the hook outside the repo folder. For example, pre-commit try-repo /path/to/local/hook terraform-fmt would invoke terraform fmt with files relative to that /path/to/local/hook. By providing the entire dirname, we can use the pre-commit tool for local development easily.

if [ "$write_changes" = true ]; then
terraform fmt "$file" || FMT_ERROR=$?
else
terraform fmt -diff -check "$file" || FMT_ERROR=$?
fi
done

exit ${FMT_ERROR}