|
| 1 | +# Maintainer's manual |
| 2 | + |
| 3 | +## Structure |
| 4 | + |
| 5 | +This folder is what's called an [importable package]. It's a top-level folder |
| 6 | +that ends up being installed into `site-packages/` of virtualenvs. |
| 7 | + |
| 8 | +When the Git repository is `pip install`ed, this [import package] becomes |
| 9 | +available for use within respective Python interpreter instance. It can be |
| 10 | +imported and sub-modules can be imported through the dot-syntax. |
| 11 | +Additionally, the modules within are able to import the neighboring ones |
| 12 | +using relative imports that have a leading dot in them. |
| 13 | + |
| 14 | +It additionally implements a [runpy interface], meaning that its name can |
| 15 | +be passed to `python -m` in order to invoke the CLI. This is the primary method |
| 16 | +of integration with the [`pre-commit` framework] and local development/testing. |
| 17 | + |
| 18 | +The layout allows for having several Python modules wrapping third-party tools, |
| 19 | +each having an argument parser and being a subcommand for the main CLI |
| 20 | +interface. |
| 21 | + |
| 22 | +## Control flow |
| 23 | + |
| 24 | +When `python -m pre_commit_terraform` is executed, it imports `__main__.py`. |
| 25 | +Which in turn, performs the initialization of the main argument parser and the |
| 26 | +parsers of subcommands, followed by executing the logic defined in dedicated |
| 27 | +subcommand modules. |
| 28 | + |
| 29 | +## Integrating a new subcommand |
| 30 | + |
| 31 | +1. Create a new module called `subcommand_x.py`. |
| 32 | +2. Within that module, define two functions — |
| 33 | + `invoke_cli_app(parsed_cli_args: Namespace) -> ReturnCodeType | int` and |
| 34 | + `populate_argument_parser(subcommand_parser: ArgumentParser) -> None`. |
| 35 | +3. Edit [`_cli_parsing.py`], importing `populate_argument_parser` from |
| 36 | + `subcommand_x` and adding it into `PARSER_MAP` with `subcommand-x` as |
| 37 | + a key. |
| 38 | +5. Edit [`_cli_subcommands.py`] `invoke_cli_app` from `subcommand_x` and |
| 39 | + adding it into `SUBCOMMAND_MAP` with `subcommand-x` as a key. |
| 40 | +6. Edit [`.pre-commit-hooks.yaml`], adding a new hook that invokes |
| 41 | + `python -m pre_commit_terraform subcommand-x`. |
| 42 | + |
| 43 | +## Manual testing |
| 44 | + |
| 45 | +Usually, having a development virtualenv where you `pip install -e .` is enough |
| 46 | +to make it possible to invoke the CLI app. Do so first. Most source code |
| 47 | +updates do not require running it again. But sometimes, it's needed. |
| 48 | + |
| 49 | +Once done, you can run `python -m pre_commit_terraform` and/or |
| 50 | +`python -m pre_commit_terraform subcommand-x` to see how it behaves. There's |
| 51 | +`--help` and all other typical conventions one would usually expect from a |
| 52 | +POSIX-inspired CLI app. |
| 53 | + |
| 54 | +## DX/UX considerations |
| 55 | + |
| 56 | +Since it's an app that can be executed outside of the [`pre-commit` framework], |
| 57 | +it is useful to check out and follow these [CLI guidelines][clig]. |
| 58 | + |
| 59 | +[`.pre-commit-hooks.yaml`]: ../../.pre-commit-hooks.yaml |
| 60 | +[`_cli_parsing.py`]: ./_cli_parsing.py |
| 61 | +[`_cli_subcommands.py`]: ./_cli_subcommands.py |
| 62 | +[clig]: https://clig.dev |
| 63 | +[importable package]: https://docs.python.org/3/tutorial/modules.html#packages |
| 64 | +[import package]: https://packaging.python.org/en/latest/glossary/#term-Import-Package |
| 65 | +[`pre-commit` framework]: https://pre-commit.com |
| 66 | +[runpy interface]: https://docs.python.org/3/library/__main__.html |
0 commit comments