|
1 | | -from taskiq.cli.args import TaskiqArgs |
2 | | -from taskiq.cli.worker import run_worker |
| 1 | +import argparse |
| 2 | +import sys |
| 3 | +from typing import Dict |
3 | 4 |
|
| 5 | +from importlib_metadata import entry_points, version |
4 | 6 |
|
5 | | -def main() -> None: |
6 | | - """Main entrypoint for CLI.""" |
7 | | - args = TaskiqArgs.from_cli() |
8 | | - run_worker(args) |
| 7 | +from taskiq.abc.cmd import TaskiqCMD |
| 8 | + |
| 9 | + |
| 10 | +def main() -> None: # noqa: C901, WPS210 |
| 11 | + """ |
| 12 | + Main entrypoint of the taskiq. |
| 13 | +
|
| 14 | + This function collects all python entrypoints |
| 15 | + and assembles a final argument parser. |
| 16 | +
|
| 17 | + All found entrypoints are used as subcommands. |
| 18 | + All arguments are passed to them as it was a normal |
| 19 | + call. |
| 20 | + """ |
| 21 | + plugins = entry_points().select(group="taskiq-cli") |
| 22 | + found_plugins = len(plugins) |
| 23 | + parser = argparse.ArgumentParser( |
| 24 | + description=f""" |
| 25 | + CLI for taskiq. Distributed task queue. |
| 26 | +
|
| 27 | + This is a meta CLI. It searches for installed plugins |
| 28 | + using python entrypoints |
| 29 | + and passes all arguments to them. |
| 30 | +
|
| 31 | + We found {found_plugins} installed plugins. |
| 32 | + """, |
| 33 | + ) |
| 34 | + parser.add_argument( |
| 35 | + "-V", |
| 36 | + "--version", |
| 37 | + dest="version", |
| 38 | + action="store_true", |
| 39 | + help="print current taskiq version and exit", |
| 40 | + ) |
| 41 | + subcommands: Dict[str, TaskiqCMD] = {} |
| 42 | + subparsers = parser.add_subparsers( |
| 43 | + title="Available subcommands", |
| 44 | + metavar="", |
| 45 | + dest="subcommand", |
| 46 | + ) |
| 47 | + for entrypoint in entry_points().select(group="taskiq-cli"): |
| 48 | + try: |
| 49 | + cmd_class = entrypoint.load() |
| 50 | + except ImportError: |
| 51 | + continue |
| 52 | + if issubclass(cmd_class, TaskiqCMD): |
| 53 | + subparsers.add_parser( |
| 54 | + entrypoint.name, |
| 55 | + help=cmd_class.short_help, |
| 56 | + add_help=False, |
| 57 | + ) |
| 58 | + subcommands[entrypoint.name] = cmd_class() |
| 59 | + |
| 60 | + args, _ = parser.parse_known_args() |
| 61 | + |
| 62 | + if args.version: |
| 63 | + print(version("taskiq")) # noqa: WPS421 |
| 64 | + return |
| 65 | + |
| 66 | + if args.subcommand is None: |
| 67 | + parser.print_help() |
| 68 | + return |
| 69 | + |
| 70 | + command = subcommands[args.subcommand] |
| 71 | + sys.argv.pop(0) |
| 72 | + command.exec(sys.argv[1:]) |
9 | 73 |
|
10 | 74 |
|
11 | 75 | if __name__ == "__main__": |
|
0 commit comments