|
1 | 1 | import subprocess |
2 | | -from typing import List |
| 2 | +from typing import Optional |
3 | 3 |
|
4 | 4 | import typer |
| 5 | +from rich import print |
5 | 6 |
|
6 | 7 | import taskbadger as tb |
| 8 | +from taskbadger.config import get_config, write_config |
| 9 | +from taskbadger.exceptions import ConfigurationError |
7 | 10 |
|
8 | 11 | app = typer.Typer() |
9 | 12 |
|
10 | | -tb.init_from_env() |
11 | 13 |
|
| 14 | +def _configure_api(ctx): |
| 15 | + config = ctx.meta["tb_config"] |
| 16 | + try: |
| 17 | + config.init_api() |
| 18 | + except ConfigurationError as e: |
| 19 | + print(f"[red]{str(e)}[/red]") |
| 20 | + raise typer.Exit(code=1) |
12 | 21 |
|
13 | | -@app.command() |
14 | | -def monitor(name: str, command: List[str] = typer.Argument(None)): |
| 22 | + |
| 23 | +@app.command(context_settings={"allow_extra_args": True, "ignore_unknown_options": True}) |
| 24 | +def monitor(ctx: typer.Context, name: str): |
| 25 | + _configure_api(ctx) |
15 | 26 | task = tb.Task.create(name) |
16 | | - result = subprocess.run(command, env={"TASKBADGER_TASK_ID": task.id}) |
| 27 | + result = subprocess.run(ctx.args, env={"TASKBADGER_TASK_ID": task.id}) |
17 | 28 | if result.returncode != 0: |
18 | 29 | task.success() |
19 | 30 | else: |
20 | 31 | task.error(data={"return_code": result.returncode}) |
21 | 32 |
|
22 | 33 |
|
| 34 | +@app.command() |
| 35 | +def configure(ctx: typer.Context): |
| 36 | + config = ctx.meta["tb_config"] |
| 37 | + config.token = typer.prompt(f"Token", default=config.token) |
| 38 | + config.organization_slug = typer.prompt(f"Organization slug", default=config.organization_slug) |
| 39 | + config.project_slug = typer.prompt(f"Project slug", default=config.project_slug) |
| 40 | + path = write_config(config) |
| 41 | + print(f"Config written to [green]{path}[/green]") |
| 42 | + |
| 43 | + |
| 44 | +@app.command() |
| 45 | +def docs(): |
| 46 | + typer.launch("https://docs.taskbadger.net") |
| 47 | + |
| 48 | + |
| 49 | +@app.command() |
| 50 | +def info(ctx: typer.Context): |
| 51 | + config = ctx.meta["tb_config"] |
| 52 | + print(f"Default Organization: {config.organization_slug or '-'}") |
| 53 | + print(f"Default Project: {config.project_slug or '-'}") |
| 54 | + print(f"Auth Token: {config.token or '-'}") |
| 55 | + |
| 56 | + |
| 57 | +@app.callback() |
| 58 | +def main( |
| 59 | + ctx: typer.Context, |
| 60 | + org: Optional[str] = typer.Option(None, "--org", "-o", metavar="ORG"), |
| 61 | + project: Optional[str] = typer.Option(None, "--project", "-p", show_envvar=False, metavar="PROJECT"), |
| 62 | +): |
| 63 | + """ |
| 64 | + Manage users in the awesome CLI app. |
| 65 | + """ |
| 66 | + config = get_config(org=org, project=project) |
| 67 | + ctx.meta["tb_config"] = config |
| 68 | + |
| 69 | + |
23 | 70 | if __name__ == "__main__": |
24 | 71 | app() |
0 commit comments