|
| 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. Additionally, |
| 11 | +the modules within can import the neighboring ones using relative imports that |
| 12 | +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` to invoke the CLI. This is the primary method of |
| 16 | +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 | + Additionally, define a module-level constant |
| 36 | + `CLI_SUBCOMMAND_NAME: Final[str] = 'subcommand-x'`. |
| 37 | +3. Edit [`_cli_subcommands.py`], importing `subcommand_x` as a relative module |
| 38 | + and add it into the `SUBCOMMAND_MODULES` list. |
| 39 | +4. Edit [`.pre-commit-hooks.yaml`], adding a new hook that invokes |
| 40 | + `python -m pre_commit_terraform subcommand-x`. |
| 41 | + |
| 42 | +## Manual testing |
| 43 | + |
| 44 | +Usually, having a development virtualenv where you `pip install -e .` is enough |
| 45 | +to make it possible to invoke the CLI app. Do so first. Most source code |
| 46 | +updates do not require running it again. But sometimes, it's needed. |
| 47 | + |
| 48 | +Once done, you can run `python -m pre_commit_terraform` and/or |
| 49 | +`python -m pre_commit_terraform subcommand-x` to see how it behaves. There's |
| 50 | +`--help` and all other typical conventions one would usually expect from a |
| 51 | +POSIX-inspired CLI app. |
| 52 | + |
| 53 | +## DX/UX considerations |
| 54 | + |
| 55 | +Since it's an app that can be executed outside the [`pre-commit` framework], |
| 56 | +it is useful to check out and follow these [CLI guidelines][clig]. |
| 57 | + |
| 58 | +## Subcommand development |
| 59 | + |
| 60 | +`populate_argument_parser()` accepts a regular instance of |
| 61 | +[`argparse.ArgumentParser`]. Call its methods to extend the CLI arguments that |
| 62 | +would be specific for the subcommand you are creating. Those arguments will be |
| 63 | +available later, as an argument to the `invoke_cli_app()` function — through an |
| 64 | +instance of [`argparse.Namespace`]. For the `CLI_SUBCOMMAND_NAME` constant, |
| 65 | +choose `kebab-space-sub-command-style`, it does not need to be `snake_case`. |
| 66 | + |
| 67 | +Make sure to return a `ReturnCode` instance or an integer from |
| 68 | +`invoke_cli_app()`. Returning a non-zero value will result in the CLI app |
| 69 | +exiting with a return code typically interpreted as an error while zero means |
| 70 | +success. You can `import errno` to use typical POSIX error codes through their |
| 71 | +human-readable identifiers. |
| 72 | + |
| 73 | +Another way to interrupt the CLI app control flow is by raising an instance of |
| 74 | +one of the in-app errors. `raise PreCommitTerraformExit` for a successful exit, |
| 75 | +but it can be turned into an error outcome via |
| 76 | +`raise PreCommitTerraformExit(1)`. |
| 77 | +`raise PreCommitTerraformRuntimeError('The world is broken')` to indicate |
| 78 | +problems within the runtime. The framework will intercept any exceptions |
| 79 | +inheriting `PreCommitTerraformBaseError`, so they won't be presented to the |
| 80 | +end-users. |
| 81 | + |
| 82 | +[`.pre-commit-hooks.yaml`]: ../../.pre-commit-hooks.yaml |
| 83 | +[`_cli_parsing.py`]: ./_cli_parsing.py |
| 84 | +[`_cli_subcommands.py`]: ./_cli_subcommands.py |
| 85 | +[`argparse.ArgumentParser`]: |
| 86 | +https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser |
| 87 | +[`argparse.Namespace`]: |
| 88 | +https://docs.python.org/3/library/argparse.html#argparse.Namespace |
| 89 | +[clig]: https://clig.dev |
| 90 | +[importable package]: https://docs.python.org/3/tutorial/modules.html#packages |
| 91 | +[import package]: https://packaging.python.org/en/latest/glossary/#term-Import-Package |
| 92 | +[`pre-commit` framework]: https://pre-commit.com |
| 93 | +[runpy interface]: https://docs.python.org/3/library/__main__.html |
0 commit comments