Skip to content

Commit

Permalink
split cli into separate modules
Browse files Browse the repository at this point in the history
  • Loading branch information
snopoke committed Sep 14, 2023
1 parent e792600 commit ea794f4
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 91 deletions.
1 change: 1 addition & 0 deletions taskbadger/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .run import run
91 changes: 3 additions & 88 deletions taskbadger/cli.py → taskbadger/cli/run.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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()
17 changes: 17 additions & 0 deletions taskbadger/cli/utils.py
Original file line number Diff line number Diff line change
@@ -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)
81 changes: 81 additions & 0 deletions taskbadger/cli_main.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion tests/test_cli_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions tests/test_cli_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ea794f4

Please sign in to comment.