|
| 1 | +"""Attach command implementation.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import Annotated |
| 6 | + |
| 7 | +import typer |
| 8 | + |
| 9 | +from vibepod.constants import CONTAINER_LABEL_MANAGED, EXIT_DOCKER_NOT_RUNNING |
| 10 | +from vibepod.core.docker import DockerClientError, DockerManager |
| 11 | +from vibepod.utils.console import error, info, warning |
| 12 | + |
| 13 | + |
| 14 | +def attach( |
| 15 | + container: Annotated[ |
| 16 | + str | None, |
| 17 | + typer.Argument( |
| 18 | + help=( |
| 19 | + "Container name or ID to attach to (see `vp list`). " |
| 20 | + "Omit when exactly one managed container is running." |
| 21 | + ), |
| 22 | + ), |
| 23 | + ] = None, |
| 24 | +) -> None: |
| 25 | + """Reattach your terminal to a running VibePod-managed container. |
| 26 | +
|
| 27 | + Use this to rejoin an agent session after the terminal that started it |
| 28 | + was closed. Find candidate containers with `vp list`. |
| 29 | + """ |
| 30 | + try: |
| 31 | + manager = DockerManager() |
| 32 | + except DockerClientError as exc: |
| 33 | + error(str(exc)) |
| 34 | + raise typer.Exit(EXIT_DOCKER_NOT_RUNNING) from exc |
| 35 | + |
| 36 | + if container is None: |
| 37 | + running = [ |
| 38 | + c |
| 39 | + for c in manager.list_managed() |
| 40 | + if getattr(c, "status", "") == "running" |
| 41 | + and (getattr(c, "labels", {}) or {}).get("vibepod.agent") |
| 42 | + ] |
| 43 | + if not running: |
| 44 | + error( |
| 45 | + "No running VibePod agent containers to attach to. " |
| 46 | + "Start one with `vp run`, or check `vp list --running`." |
| 47 | + ) |
| 48 | + raise typer.Exit(1) |
| 49 | + if len(running) > 1: |
| 50 | + names = ", ".join(sorted(c.name for c in running)) |
| 51 | + error( |
| 52 | + f"Multiple running containers: {names}. " |
| 53 | + "Specify one explicitly: `vp attach <container>`." |
| 54 | + ) |
| 55 | + raise typer.Exit(1) |
| 56 | + target = running[0] |
| 57 | + else: |
| 58 | + try: |
| 59 | + target = manager.get_container(container) |
| 60 | + except DockerClientError as exc: |
| 61 | + error(str(exc)) |
| 62 | + raise typer.Exit(1) from exc |
| 63 | + |
| 64 | + labels = getattr(target, "labels", {}) or {} |
| 65 | + if labels.get(CONTAINER_LABEL_MANAGED) != "true": |
| 66 | + error(f"Container '{container}' is not managed by VibePod.") |
| 67 | + raise typer.Exit(1) |
| 68 | + if getattr(target, "status", "") != "running": |
| 69 | + error( |
| 70 | + f"Container '{container}' is not running " |
| 71 | + f"(status: {getattr(target, 'status', 'unknown')})." |
| 72 | + ) |
| 73 | + raise typer.Exit(1) |
| 74 | + |
| 75 | + agent = (getattr(target, "labels", {}) or {}).get("vibepod.agent", "agent") |
| 76 | + info(f"Attaching to {target.name} ({agent})") |
| 77 | + warning( |
| 78 | + f"Close the terminal to leave it running, or stop it with `vp stop {target.name}`." |
| 79 | + ) |
| 80 | + try: |
| 81 | + manager.attach_interactive(target) |
| 82 | + except DockerClientError as exc: |
| 83 | + error(str(exc)) |
| 84 | + raise typer.Exit(1) from exc |
0 commit comments