|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import Optional |
| 4 | + |
| 5 | +import typer |
| 6 | +from rich.console import Console |
| 7 | +from rich.table import Table |
| 8 | + |
| 9 | +from ..async_typer import AsyncTyper |
| 10 | +from ..task.manager import TaskFilter |
| 11 | +from ..task.models import Task, TaskState |
| 12 | +from .client import initialize_client |
| 13 | +from .parameters import CONFIG_PARAM |
| 14 | +from .utils import catch_exception, init_logging |
| 15 | + |
| 16 | +app = AsyncTyper() |
| 17 | +console = Console() |
| 18 | + |
| 19 | + |
| 20 | +@app.callback() |
| 21 | +def callback() -> None: |
| 22 | + """Manage Infrahub tasks.""" |
| 23 | + |
| 24 | + |
| 25 | +def _parse_states(states: list[str] | None) -> list[TaskState] | None: |
| 26 | + if not states: |
| 27 | + return None |
| 28 | + |
| 29 | + parsed_states: list[TaskState] = [] |
| 30 | + for state in states: |
| 31 | + normalized_state = state.strip().upper() |
| 32 | + try: |
| 33 | + parsed_states.append(TaskState(normalized_state)) |
| 34 | + except ValueError as exc: # pragma: no cover - typer will surface this as CLI error |
| 35 | + raise typer.BadParameter( |
| 36 | + f"Unsupported state '{state}'. Available states: {', '.join(item.value.lower() for item in TaskState)}" |
| 37 | + ) from exc |
| 38 | + |
| 39 | + return parsed_states |
| 40 | + |
| 41 | + |
| 42 | +def _render_table(tasks: list[Task]) -> None: |
| 43 | + table = Table(title="Infrahub Tasks", box=None) |
| 44 | + table.add_column("ID", style="cyan", overflow="fold") |
| 45 | + table.add_column("Title", style="magenta", overflow="fold") |
| 46 | + table.add_column("State", style="green") |
| 47 | + table.add_column("Progress", justify="right") |
| 48 | + table.add_column("Workflow", overflow="fold") |
| 49 | + table.add_column("Branch", overflow="fold") |
| 50 | + table.add_column("Updated") |
| 51 | + |
| 52 | + if not tasks: |
| 53 | + table.add_row("-", "No tasks found", "-", "-", "-", "-", "-") |
| 54 | + console.print(table) |
| 55 | + return |
| 56 | + |
| 57 | + for task in tasks: |
| 58 | + progress = f"{task.progress:.0%}" if task.progress is not None else "-" |
| 59 | + table.add_row( |
| 60 | + task.id, |
| 61 | + task.title, |
| 62 | + task.state.value, |
| 63 | + progress, |
| 64 | + task.workflow or "-", |
| 65 | + task.branch or "-", |
| 66 | + task.updated_at.isoformat(), |
| 67 | + ) |
| 68 | + |
| 69 | + console.print(table) |
| 70 | + |
| 71 | + |
| 72 | +@app.command(name="list") |
| 73 | +@catch_exception(console=console) |
| 74 | +async def list_tasks( |
| 75 | + state: list[str] = typer.Option( |
| 76 | + None, "--state", "-s", help="Filter by task state. Can be provided multiple times." |
| 77 | + ), |
| 78 | + limit: Optional[int] = typer.Option(None, help="Maximum number of tasks to retrieve."), |
| 79 | + offset: Optional[int] = typer.Option(None, help="Offset for pagination."), |
| 80 | + include_related_nodes: bool = typer.Option(False, help="Include related nodes in the output."), |
| 81 | + include_logs: bool = typer.Option(False, help="Include task logs in the output."), |
| 82 | + json_output: bool = typer.Option(False, "--json", help="Output the result as JSON."), |
| 83 | + debug: bool = False, |
| 84 | + _: str = CONFIG_PARAM, |
| 85 | +) -> None: |
| 86 | + """List Infrahub tasks.""" |
| 87 | + |
| 88 | + init_logging(debug=debug) |
| 89 | + |
| 90 | + client = initialize_client() |
| 91 | + filters = TaskFilter() |
| 92 | + parsed_states = _parse_states(state) |
| 93 | + if parsed_states: |
| 94 | + filters.state = parsed_states |
| 95 | + |
| 96 | + tasks = await client.task.filter( |
| 97 | + filter=filters, |
| 98 | + limit=limit, |
| 99 | + offset=offset, |
| 100 | + include_related_nodes=include_related_nodes, |
| 101 | + include_logs=include_logs, |
| 102 | + ) |
| 103 | + |
| 104 | + if json_output: |
| 105 | + console.print_json( |
| 106 | + data=[task.model_dump(mode="json") for task in tasks], indent=2, sort_keys=True, highlight=False |
| 107 | + ) |
| 108 | + return |
| 109 | + |
| 110 | + _render_table(tasks) |
0 commit comments