|
| 1 | +# AGENTS.md — pre-commit-terraform |
| 2 | + |
| 3 | +## What this repo is |
| 4 | + |
| 5 | +Collection of Git hooks for Terraform/OpenTofu/Terragrunt to be used with the [pre-commit framework](https://pre-commit.com/). |
| 6 | +Hooks enforce formatting, validation, documentation, and security scanning across Terraform codebases. |
| 7 | + |
| 8 | +## Directory layout |
| 9 | + |
| 10 | +- `hooks/` — Bash entry points for each hook (`<hook_name>.sh`) + shared `_common.sh` |
| 11 | +- `src/pre_commit_terraform/` — Python package: CLI parsing, env expansion, `__GIT_WORKING_DIR__` substitution, `terraform_docs_replace` logic |
| 12 | +- `tests/pytest/` — Python unit tests (`pytest`); `tests/Dockerfile` and `tests/hooks_performance_test.sh` for integration/perf testing |
| 13 | +- `tools/entrypoint.sh` — Docker container entrypoint (user/group setup) |
| 14 | +- `tools/install/` — per-tool install scripts used during Docker image build (one `.sh` per tool) |
| 15 | +- `dependencies/lock-files/` — pinned Python dependency constraints for reproducible Docker builds |
| 16 | +- `.pre-commit-hooks.yaml` — hook definitions consumed by the pre-commit framework |
| 17 | +- `pyproject.toml` / `hatch.toml` — Python project config and build system |
| 18 | +- `tox.ini` — test environment matrix |
| 19 | + |
| 20 | +## Hook implementation pattern |
| 21 | + |
| 22 | +Two distinct hook types exist: |
| 23 | + |
| 24 | +**Shell-based hooks** (majority) — `hooks/<hook_name>.sh`: |
| 25 | +- Sources `_common.sh` for shared logic: cmdline parsing (`--args`, `--hook-config`, `--env-vars`), env var expansion, `__GIT_WORKING_DIR__` substitution, parallelism, `terraform init` |
| 26 | +- Defines `per_dir_hook_unique_part()` — the hook-specific per-directory logic |
| 27 | +- `common::per_dir_hook` orchestrates parallelism and calls it for each changed dir |
| 28 | + |
| 29 | +**Python-based hooks** — invoked as `python -m pre_commit_terraform <subcommand>`: |
| 30 | +- `src/pre_commit_terraform/` is a standalone CLI app, not a helper library for bash |
| 31 | +- Each subcommand is a module with `invoke_cli_app()` + `populate_argument_parser()` + `CLI_SUBCOMMAND_NAME` |
| 32 | +- Currently only `terraform-docs-replace` subcommand exists |
| 33 | +- Adding a new Python subcommand: create module → register in `_cli_subcommands.py` → add hook entry in `.pre-commit-hooks.yaml` |
| 34 | + |
| 35 | +New shell hooks: use `_common.sh` helpers, define `per_dir_hook_unique_part`, never ad-hoc argument splitting. |
| 36 | + |
| 37 | +## Adding a new hook |
| 38 | + |
| 39 | +Read `.github/CONTRIBUTING.md` — it covers the full checklist (Dockerfile, container structure tests, `.pre-commit-hooks.yaml`, hook file, docs). |
| 40 | + |
| 41 | +## Testing |
| 42 | + |
| 43 | +- Tests live in `tests/pytest/` and use `pytest`, run via `tox` |
| 44 | +- Coverage config: `.coveragerc` |
| 45 | +- For how to run tests and pre-commit checks locally, see `.github/CONTRIBUTING.md` |
| 46 | + |
| 47 | +## Linters |
| 48 | + |
| 49 | +This repo enforces style via pre-commit (`.pre-commit-config.yaml`) — install and run it before committing. Linters cover: |
| 50 | +- **Python**: `ruff` (single quotes, type hints required) + `mypy` |
| 51 | +- **Shell**: `shfmt` + `shellcheck` — no suppression without comment |
| 52 | + |
| 53 | +## Repo rules |
| 54 | + |
| 55 | +- Hook args parsing: use `_common.sh` helpers, never ad-hoc argument splitting |
| 56 | +- Do not hand-edit `CHANGELOG.md` — managed by release tooling (`.releaserc.json`) |
| 57 | + |
| 58 | +## CI |
| 59 | + |
| 60 | +PRs trigger `.github/workflows/ci-cd.yml`: |
| 61 | +- Runs pre-commit hooks |
| 62 | +- Runs pytest via tox across Python versions |
| 63 | +- Builds and tests Docker image |
| 64 | +- Release cut on merge to `master` via semantic-release |
| 65 | + |
| 66 | +## Skill routing |
| 67 | + |
| 68 | +Load these skills when their triggers match. |
| 69 | + |
| 70 | +| Task | Skill | |
| 71 | +| --- | --- | |
| 72 | +| Creating a new skill: registering in AGENTS.md, choosing script vs SKILL.md | `adding-skills` | |
| 73 | +| Create a commit, GitHub issue, or PR | `git-workflow` | |
0 commit comments