|
| 1 | +"""CLI commands for tag management.""" |
| 2 | + |
| 3 | +import typer |
| 4 | +from rich import print |
| 5 | + |
| 6 | +from ..api.client import get_client, QualyticsAPIError |
| 7 | +from ..api.tags import create_tag, delete_tag, get_tag, list_all_tags |
| 8 | +from ..utils.serialization import OutputFormat, format_for_display |
| 9 | + |
| 10 | +from . import add_suggestion_callback |
| 11 | +from .progress import status |
| 12 | + |
| 13 | +tags_app = typer.Typer( |
| 14 | + name="tags", |
| 15 | + help="Manage tags", |
| 16 | +) |
| 17 | +add_suggestion_callback(tags_app, "tags") |
| 18 | + |
| 19 | + |
| 20 | +@tags_app.command("list") |
| 21 | +def tags_list( |
| 22 | + fmt: OutputFormat = typer.Option( |
| 23 | + OutputFormat.YAML, "--format", "-f", help="Output format: yaml or json" |
| 24 | + ), |
| 25 | +): |
| 26 | + """List all tags.""" |
| 27 | + client = get_client() |
| 28 | + |
| 29 | + with status("[bold cyan]Fetching tags...[/bold cyan]"): |
| 30 | + all_tags = list_all_tags(client) |
| 31 | + |
| 32 | + if not all_tags: |
| 33 | + print("[yellow]No tags found.[/yellow]") |
| 34 | + raise typer.Exit() |
| 35 | + |
| 36 | + print(f"[bold]Found {len(all_tags)} tag(s).[/bold]\n") |
| 37 | + print(format_for_display(all_tags, fmt)) |
| 38 | + |
| 39 | + |
| 40 | +@tags_app.command("get") |
| 41 | +def tags_get( |
| 42 | + tag_id: int = typer.Option(..., "--id", help="Tag ID"), |
| 43 | + fmt: OutputFormat = typer.Option( |
| 44 | + OutputFormat.YAML, "--format", "-f", help="Output format: yaml or json" |
| 45 | + ), |
| 46 | +): |
| 47 | + """Get a single tag by ID.""" |
| 48 | + client = get_client() |
| 49 | + |
| 50 | + with status("[bold cyan]Fetching tag...[/bold cyan]"): |
| 51 | + result = get_tag(client, tag_id) |
| 52 | + |
| 53 | + print(format_for_display(result, fmt)) |
| 54 | + |
| 55 | + |
| 56 | +@tags_app.command("create") |
| 57 | +def tags_create( |
| 58 | + name: str = typer.Option(..., "--name", "-n", help="Tag name"), |
| 59 | + fmt: OutputFormat = typer.Option( |
| 60 | + OutputFormat.YAML, "--format", "-f", help="Output format: yaml or json" |
| 61 | + ), |
| 62 | +): |
| 63 | + """Create a new tag.""" |
| 64 | + client = get_client() |
| 65 | + payload = {"name": name} |
| 66 | + |
| 67 | + result = create_tag(client, payload) |
| 68 | + print(f"[green]Tag created successfully![/green]") |
| 69 | + print(f"[green]Tag ID: {result.get('id')}[/green]\n") |
| 70 | + print(format_for_display(result, fmt)) |
| 71 | + |
| 72 | + |
| 73 | +@tags_app.command("delete") |
| 74 | +def tags_delete( |
| 75 | + tag_id: int = typer.Option(..., "--id", help="Tag ID to delete"), |
| 76 | +): |
| 77 | + """Delete a tag by ID.""" |
| 78 | + client = get_client() |
| 79 | + |
| 80 | + try: |
| 81 | + delete_tag(client, tag_id) |
| 82 | + print(f"[green]Tag {tag_id} deleted successfully.[/green]") |
| 83 | + except QualyticsAPIError as e: |
| 84 | + print(f"[red]Failed to delete tag {tag_id}: {e.message}[/red]") |
| 85 | + raise typer.Exit(code=1) |
0 commit comments