This repository contains a comprehensive set of Git pre-commit hooks that are specifically designed
to work seamlessly with the pre-commit framework. These hooks aim to automate critical tasks such
as code formatting, validation, linting, and other quality assurance checks, ensuring that your code
adheres to best practices and project standards before it is committed to the repository.
By integrating these hooks into your workflow, you can catch and address common issues early in the development process, reducing the likelihood of bugs and inconsistencies making their way into your codebase. The pre-commit framework makes it straightforward to configure and enforce these checks across your team, promoting consistency and improving overall code quality.
The hooks included in this repository cover a wide range of use cases and technologies, supporting multiple file types and tools. Whether you are working with Terraform, Golang, Python, Markdown, or Bash scripts, you will find hooks tailored to your needs. From formatting and linting to validating configurations, these hooks help streamline your development process and enforce coding standards.
The currently supported hooks are listed below, categorized by technology and file type, to make it easier to identify the tools most relevant to your project requirements.
- gofmt: Runs
gofmton all Go (*.gofiles). - golangci-lint: Runs
golangci-linton all Go (*.go) files. - goimports: Runs
goimportson all Go (*.go) files. - go build: Verify that
go buildruns successfully for all your Go project. - go mod tidy: Runs
go mod tidyto ensurego.modandgo.sumare up-to-date.
- yapf: Runs
yapfon all Python (*.py) files.
- terraform-fmt: Runs
terraform fmton all Terraform (*.tf) files. - terraform-validate: Runs
terraform validateon all Terraform (*.tf) files. - terragrunt-hclfmt: Runs
terragrunt hclfmton all Terragrunt files. - tofu-fmt: Runs
tofu fmton all OpenTofu (*.tf,*.terraform) files. - tofu-validate: Runs
tofu validateon all OpenTofu (*.tf,*.terraform) files.
- packer-fmt:
- packer-validate: Runs
packer validateon all Packer (*.pkr.*)files.
- markdown-link-check: Runs
markdown-link-checkon markdown (*.md) files.
- shellcheck: Run
shellcheckto lint files that contain a bash shebang.
In each of your repos, add a file called .pre-commit-config.yaml with the following contents:
repos:
- repo: https://github.com/tenthirtyam/pre-commit
rev: x.y.z # Example only; get the latest from: https://github.com/tenthirtyam/pre-commit/releases
hooks:
- id: terraform-fmt
- id: terraform-validate
- id: shellcheck
- id: gofmt
- id: golintNext:
-
Install pre-commit on your system:
brew install pre-commit
-
In your repository, run the following command:
pre-commit install
Each time you make a commit, the hooks defined in the hooks: configuration will run.
Tip
Formatting All Files
If you'd like to format all of your code at once (rather than one file at a time), you can run:
pre-commit run terraform-fmt --all-filesTip
Formatting All Files in a CI
To ensure all hooks are enforced, configure your CI build to fail if the code does not pass the checks by adding the following to your build scripts:
pip install pre-commit
pre-commit install
pre-commit run --all-filesIf all hooks succeed, the final command will exit with an exit code 0.
However, if any hooks make changes (e.g., due to unformatted files), this will result in a failure and exit with an exit code 1.
Copyright © Ryan Johnson
Licensed under the MIT License.