All Articles

Format the code automatically before commit

There is a situation where we forget to format our code to one standard before submitting changes. But what if git does it for us?

There are a couple of solutions, like plugging directly into git hooks commands, or using a framework like https://pre-commit.com/ which will visually help us check out the project, and add code checking plugins for us.

# Format code with terraform & terragrunt standard
terragrunt hclfmt
terraform fmt --recursive

# Add missing new-line in files
git ls-files -z | while IFS= read -rd '' f; do tail -c1 < "$f" | read -r _ || echo >> "$f"; done

It’s especially useful when we need to run a lot of commands responsible for testing, linter code and checking integration