"""Admin commands — da admin.""" import json import typer from cli.client import api_get, api_post, api_delete admin_app = typer.Typer(help="Admin operations (requires admin role)") @admin_app.command("add-user") def add_user( email: str = typer.Argument(..., help="User email"), name: str = typer.Option("", help="User display name"), role: str = typer.Option("analyst", help="Role: viewer, analyst, admin, km_admin"), ): """Add a new user.""" resp = api_post("/api/users", json={"email": email, "name": name or email.split("@")[0], "role": role}) if resp.status_code == 201: data = resp.json() typer.echo(f"Created user: {data['email']} (id: {data['id']}, role: {data['role']})") else: typer.echo(f"Failed: {resp.json().get('detail', resp.text)}", err=True) raise typer.Exit(1) @admin_app.command("list-users") def list_users(as_json: bool = typer.Option(False, "--json")): """List all users.""" resp = api_get("/api/users") if resp.status_code != 200: typer.echo(f"Failed: {resp.json().get('detail', resp.text)}", err=True) raise typer.Exit(1) users = resp.json() if as_json: typer.echo(json.dumps(users, indent=2)) else: for u in users: typer.echo(f" {u['email']:30s} role={u['role']:10s} id={u['id'][:8]}") @admin_app.command("remove-user") def remove_user(user_id: str = typer.Argument(..., help="User ID to remove")): """Remove a user.""" resp = api_delete(f"/api/users/{user_id}") if resp.status_code == 204: typer.echo("User removed.") else: typer.echo(f"Failed: {resp.text}", err=True) raise typer.Exit(1)