From ea794f4871e711ee6d9ee8f1172e8595b09d235c Mon Sep 17 00:00:00 2001 From: Simon Kelly Date: Thu, 14 Sep 2023 19:54:38 +0200 Subject: [PATCH] split cli into separate modules --- taskbadger/cli/__init__.py | 1 + taskbadger/{cli.py => cli/run.py} | 91 +------------------------------ taskbadger/cli/utils.py | 17 ++++++ taskbadger/cli_main.py | 81 +++++++++++++++++++++++++++ tests/test_cli_config.py | 2 +- tests/test_cli_run.py | 4 +- 6 files changed, 105 insertions(+), 91 deletions(-) create mode 100644 taskbadger/cli/__init__.py rename taskbadger/{cli.py => cli/run.py} (54%) create mode 100644 taskbadger/cli/utils.py create mode 100644 taskbadger/cli_main.py diff --git a/taskbadger/cli/__init__.py b/taskbadger/cli/__init__.py new file mode 100644 index 0000000..3ce5f2b --- /dev/null +++ b/taskbadger/cli/__init__.py @@ -0,0 +1 @@ +from .run import run diff --git a/taskbadger/cli.py b/taskbadger/cli/run.py similarity index 54% rename from taskbadger/cli.py rename to taskbadger/cli/run.py index 9faad7f..a75725f 100644 --- a/taskbadger/cli.py +++ b/taskbadger/cli/run.py @@ -1,39 +1,13 @@ -from typing import Optional, Tuple +from typing import Tuple import typer from rich import print -from rich.console import Console -from taskbadger import Action, DefaultMergeStrategy, Session, StatusEnum, Task, __version__, integrations -from taskbadger.config import get_config, write_config -from taskbadger.exceptions import ConfigurationError +from taskbadger import Action, DefaultMergeStrategy, Session, StatusEnum, Task, integrations +from taskbadger.cli.utils import _configure_api, err_console from taskbadger.process import ProcessRunner -app = typer.Typer( - rich_markup_mode="rich", - context_settings={"help_option_names": ["-h", "--help"]}, -) - -err_console = Console(stderr=True) - - -def version_callback(value: bool): - if value: - print(f"Task Badger CLI Version: {__version__}") - raise typer.Exit() - - -def _configure_api(ctx): - config = ctx.meta["tb_config"] - try: - config.init_api() - except ConfigurationError as e: - print(f"[red]{str(e)}[/red]") - raise typer.Exit(code=1) - - -@app.command(context_settings={"allow_extra_args": True, "ignore_unknown_options": False}) def run( ctx: typer.Context, name: str, @@ -109,62 +83,3 @@ def _update_task(task, status=None, **data_kwargs): task.update(status=status, data=data_kwargs or None, data_merge_strategy=merge_strategy) except Exception as e: err_console.print(f"Error updating task status: {e!r}") - - -@app.command() -def configure(ctx: typer.Context): - """Update CLI configuration.""" - config = ctx.meta["tb_config"] - config.organization_slug = typer.prompt(f"Organization slug", default=config.organization_slug) - config.project_slug = typer.prompt(f"Project slug", default=config.project_slug) - config.token = typer.prompt(f"API Key", default=config.token) - path = write_config(config) - print(f"Config written to [green]{path}[/green]") - - -@app.command() -def docs(): - """Open Task Badger docs in a browser.""" - typer.launch("https://docs.taskbadger.net") - - -@app.command() -def info(ctx: typer.Context): - """Show CLI configuration.""" - config = ctx.meta["tb_config"] - print(str(config)) - - -@app.callback() -def main( - ctx: typer.Context, - org: Optional[str] = typer.Option( - None, - "--org", - "-o", - metavar="TASKBADGER_ORG", - show_default=False, - help="Organization Slug. This will override values from the config file and environment variables.", - ), - project: Optional[str] = typer.Option( - None, - "--project", - "-p", - show_envvar=False, - metavar="TASKBADGER_PROJECT", - show_default=False, - help="Project Slug. This will override values from the config file and environment variables.", - ), - version: Optional[bool] = typer.Option( # noqa - None, "--version", callback=version_callback, is_eager=True, help="Show CLI Version" - ), -): - """ - Task Badger CLI - """ - config = get_config(org=org, project=project) - ctx.meta["tb_config"] = config - - -if __name__ == "__main__": - app() diff --git a/taskbadger/cli/utils.py b/taskbadger/cli/utils.py new file mode 100644 index 0000000..f973012 --- /dev/null +++ b/taskbadger/cli/utils.py @@ -0,0 +1,17 @@ +import typer +from rich import print +from rich.console import Console + +from taskbadger.exceptions import ConfigurationError + + +def _configure_api(ctx): + config = ctx.meta["tb_config"] + try: + config.init_api() + except ConfigurationError as e: + print(f"[red]{str(e)}[/red]") + raise typer.Exit(code=1) + + +err_console = Console(stderr=True) diff --git a/taskbadger/cli_main.py b/taskbadger/cli_main.py new file mode 100644 index 0000000..7861fec --- /dev/null +++ b/taskbadger/cli_main.py @@ -0,0 +1,81 @@ +from typing import Optional + +import typer +from rich import print + +from taskbadger import __version__ +from taskbadger.cli import run +from taskbadger.config import get_config, write_config + +app = typer.Typer( + rich_markup_mode="rich", + context_settings={"help_option_names": ["-h", "--help"]}, +) + + +app.command(context_settings={"allow_extra_args": True, "ignore_unknown_options": False})(run) + + +def version_callback(value: bool): + if value: + print(f"Task Badger CLI Version: {__version__}") + raise typer.Exit() + + +@app.command() +def configure(ctx: typer.Context): + """Update CLI configuration.""" + config = ctx.meta["tb_config"] + config.organization_slug = typer.prompt(f"Organization slug", default=config.organization_slug) + config.project_slug = typer.prompt(f"Project slug", default=config.project_slug) + config.token = typer.prompt(f"API Key", default=config.token) + path = write_config(config) + print(f"Config written to [green]{path}[/green]") + + +@app.command() +def docs(): + """Open Task Badger docs in a browser.""" + typer.launch("https://docs.taskbadger.net") + + +@app.command() +def info(ctx: typer.Context): + """Show CLI configuration.""" + config = ctx.meta["tb_config"] + print(str(config)) + + +@app.callback() +def main( + ctx: typer.Context, + org: Optional[str] = typer.Option( + None, + "--org", + "-o", + metavar="TASKBADGER_ORG", + show_default=False, + help="Organization Slug. This will override values from the config file and environment variables.", + ), + project: Optional[str] = typer.Option( + None, + "--project", + "-p", + show_envvar=False, + metavar="TASKBADGER_PROJECT", + show_default=False, + help="Project Slug. This will override values from the config file and environment variables.", + ), + version: Optional[bool] = typer.Option( # noqa + None, "--version", callback=version_callback, is_eager=True, help="Show CLI Version" + ), +): + """ + Task Badger CLI + """ + config = get_config(org=org, project=project) + ctx.meta["tb_config"] = config + + +if __name__ == "__main__": + app() diff --git a/tests/test_cli_config.py b/tests/test_cli_config.py index 060a6cb..2d80f6c 100644 --- a/tests/test_cli_config.py +++ b/tests/test_cli_config.py @@ -6,7 +6,7 @@ import tomlkit from typer.testing import CliRunner -from taskbadger.cli import app +from taskbadger.cli_main import app from taskbadger.config import Config, write_config runner = CliRunner() diff --git a/tests/test_cli_run.py b/tests/test_cli_run.py index 498628a..a5c6d6d 100644 --- a/tests/test_cli_run.py +++ b/tests/test_cli_run.py @@ -5,7 +5,7 @@ import pytest from typer.testing import CliRunner -from taskbadger.cli import app +from taskbadger.cli_main import app from taskbadger.internal.models import PatchedTaskRequest, PatchedTaskRequestData, StatusEnum, TaskRequest from taskbadger.internal.types import UNSET, Response from taskbadger.mug import Badger @@ -150,7 +150,7 @@ def _create(*args, **kwargs): with ( mock.patch("taskbadger.sdk.create_task", new=_create), mock.patch("taskbadger.sdk.update_task", new=_update), - mock.patch("taskbadger.cli.err_console") as err, + mock.patch("taskbadger.cli.run.err_console") as err, ): args = ["task_name"] result = runner.invoke(app, ["run"] + args + ["--"] + ["echo", "test"], catch_exceptions=False)