How antonbabenko/pre-commit-terraform works
Project type and purpose
• This is an open-source CLI tool and Docker image providing a collection of Terraform/Terragrunt Git hooks.
• It plugs into the pre-commit framework, orchestrates checks (formatting, validation, security, docs, cost) via Python code and shell scripts, and can be consumed locally, in CI, or as a Docker container.
File structure
• Top-level:
• src/pre_commit_terraform/ : Python package (CLI entrypoint, subcommands, parsing, error handling, structs).
• hooks/ : Bash wrapper scripts for each hook (terraform_fmt, terraform_validate, etc.).
• .pre-commit-hooks.yaml & .pre-commit-config.yaml (in readme) define hook IDs, entrypoints, args.
• Dockerfile & tools/entrypoint.sh : build and run container, set up user‐mapping.
• tests/ : unit tests for Python code and performance tests for hooks.
• .github/workflows/* : CI/CD pipelines for building, testing, releasing the image and Python package.
• pyproject.toml, hatch.toml, tox.ini, pytest.ini, and various linters’ configs: packaging, build, and static checks.
README for additional insights
• Describes each hook’s purpose (terraform_fmt, terraform_docs, terraform_tflint, terraform_trivy, infracost_breakdown, etc.).
• Shows install options (local via pip, Docker), execution flows, environment variable overrides, and CI/CD examples on GitHub Actions.
Detail without over-complicating
• Break the system into the maximum meaningful components listed above.
• Every essential interaction (Git → pre-commit → CLI → hooks → tools).
[Developer Workstation]
│
▼
[pre-commit framework]──reads──>[.pre-commit-hooks.yaml]
│
▼
[pre_commit_terraform Python CLI]──invokes──>[hook script: terraform_fmt.sh]
│ ▲
▼ │
… other hooks … │
│ │
▼ │
[External Tool: terraform binary] └─ common.sh library
│
▼
Results → back to CLI → back to pre-commit → Git commit allowed/blocked
Plus side panels for:
• Docker Image (Dockerfile, entrypoint)
• CI/CD (GitHub Actions)
• External APIs (Infracost)