from __future__ import annotations import json import click from rich.console import Console from rich.table import Table from browser_cli import BrowserCLI from browser_cli.commands import handle_errors from browser_cli.remote.registry import REMOTE_REGISTRY_PATH, load_remotes, save_remote_key console = Console() @click.group("remote") def remote_group(): """Manage remembered browser-cli remote endpoints.""" @remote_group.command("status") @click.argument("endpoint") @click.option("--key", default=None, help="Key spec/path to use for this probe") @handle_errors def remote_status(endpoint, key): """Probe a remote endpoint and show server/client status.""" client = BrowserCLI(remote=endpoint, key=key) clients = client.clients() table = Table(show_header=True, header_style="bold cyan") table.add_column("Profile") table.add_column("Browser") table.add_column("Extension") for item in clients: table.add_row(str(item.get("profile", "")), str(item.get("name", "")), str(item.get("extensionVersion", ""))) console.print(table) @remote_group.command("trust") @click.argument("endpoint") @click.argument("key_spec") def remote_trust(endpoint, key_spec): """Remember which key spec to use for ENDPOINT.""" save_remote_key(endpoint, key_spec) console.print(f"[green]Trusted remote {endpoint} with key {key_spec}[/green]") @remote_group.command("keys") def remote_keys(): """List remembered remote key specs.""" remotes = load_remotes() if not remotes: console.print("[yellow]No remembered remotes[/yellow]") return table = Table(show_header=True, header_style="bold cyan") table.add_column("Endpoint") table.add_column("Key") for endpoint, cfg in sorted(remotes.items()): table.add_row(endpoint, str(cfg.get("key", ""))) console.print(table) @remote_group.command("revoke") @click.argument("endpoint") def remote_revoke(endpoint): """Remove remembered key/config for ENDPOINT.""" remotes = load_remotes() if endpoint not in remotes: console.print(f"[yellow]Remote {endpoint} not remembered[/yellow]") return del remotes[endpoint] REMOTE_REGISTRY_PATH.parent.mkdir(parents=True, exist_ok=True) REMOTE_REGISTRY_PATH.write_text(json.dumps(remotes, indent=2, sort_keys=True) + "\n", encoding="utf-8") console.print(f"[green]Revoked {endpoint}[/green]")