Skip to content

project

project

project subcommand group — all per-project operations.

register(subparsers)

Register the project subcommand group.

Source code in src/terok/cli/commands/project.py
def register(subparsers: argparse._SubParsersAction[argparse.ArgumentParser]) -> None:
    """Register the ``project`` subcommand group."""
    p = subparsers.add_parser("project", help="Create, configure, and manage projects")
    sub = p.add_subparsers(dest="project_cmd", required=True)

    # list
    sub.add_parser("list", help="List all known projects")

    # wizard
    sub.add_parser(
        "wizard",
        help="Interactive wizard to create a new project configuration",
    )

    # derive
    p_derive = sub.add_parser(
        "derive",
        help="Create a new project derived from an existing one (shared infra, fresh agent config)",
    )
    set_completer(
        p_derive.add_argument("source_id", help="Source project name to derive from"),
        _complete_project_names,
    )
    p_derive.add_argument("new_id", help="New project name")

    # normalize-name
    p_normalize = sub.add_parser(
        "normalize-name",
        help="Repair project.yml so project.name matches the project directory name",
    )
    _add_project_arg(
        p_normalize,
        help=(
            "Project directory name to write into project.yml "
            "(works even when that project is currently broken)"
        ),
    )

    # delete
    p_delete = sub.add_parser(
        "delete",
        help="Delete a project and all its associated data (non-recoverable)",
    )
    _add_project_arg(p_delete, help="Project name to delete")
    p_delete.add_argument("--force", action="store_true", help="Skip confirmation prompt")

    # init — full setup
    p_init = sub.add_parser(
        "init",
        help="Full project setup: ssh-init + generate + build + gate-sync",
    )
    _add_project_arg(p_init)

    # generate
    p_gen = sub.add_parser("generate", help="Generate Dockerfiles for a project")
    _add_project_arg(p_gen)

    # build
    p_build = sub.add_parser("build", help="Build images for a project")
    _add_project_arg(p_build)
    p_build.add_argument(
        "--refresh-agents",
        dest="refresh_agents",
        action="store_true",
        help="Rebuild from L1 with fresh agent installs (cache bust)",
    )
    p_build.add_argument(
        "--agents",
        dest="agents",
        default=None,
        metavar="LIST",
        help=(
            'Comma-separated roster entries to install in L1, or "all". '
            "Overrides the project's image.agents for this build only."
        ),
    )
    p_build.add_argument(
        "--full-rebuild",
        action="store_true",
        help="Rebuild from L0 (no cache) (includes base image pull and apt packages)",
    )
    p_build.add_argument(
        "--dev",
        action="store_true",
        help="Also build a manual dev image from L0 (tagged as <project>:l2-dev)",
    )

    # ssh-init
    p_ssh = sub.add_parser(
        "ssh-init",
        help="Generate a vault-managed SSH keypair for a project",
    )
    _add_project_arg(p_ssh)
    p_ssh.add_argument(
        "--key-type",
        choices=["ed25519", "rsa"],
        default="ed25519",
        help="Key algorithm (default: ed25519)",
    )
    p_ssh.add_argument(
        "-c",
        "--comment",
        default=None,
        help=(
            "Comment embedded in the public key "
            "(new keys default to the next unused <project>-N; bare init reuses the default key)"
        ),
    )
    p_ssh.add_argument(
        "--force",
        action="store_true",
        help="Rotate — unassign existing keys from the project and generate fresh",
    )

    # gate-path
    p_gate_path = sub.add_parser(
        "gate-path",
        help=(
            "Print the project's host-side git gate mirror as a file:// URL "
            "(suitable for IDEs and host-side git tools)"
        ),
    )
    _add_project_arg(p_gate_path)

    # gate-sync
    p_gate = sub.add_parser(
        "gate-sync",
        help=(
            "Sync the host-side git gate for a project (creates it if missing). "
            "For SSH upstreams this uses ONLY the vault-managed key from "
            "'project ssh-init' — never the user's ~/.ssh — unless "
            "--use-personal-ssh is passed."
        ),
    )
    _add_project_arg(p_gate)
    p_gate.add_argument(
        "--force-reinit",
        dest="force_reinit",
        action="store_true",
        help="Recreate the mirror from scratch",
    )
    p_gate.add_argument(
        "--use-personal-ssh",
        dest="use_personal_ssh",
        action="store_true",
        help="Fall through to the user's ~/.ssh keys instead of the vault",
    )
    p_gate.add_argument(
        "--destructive",
        dest="destructive",
        action="store_true",
        help=(
            "Apply the pending destructive branch changes (deletes, force-updates) "
            "without the interactive prompt — old tips are saved as backup refs first"
        ),
    )

    # gate-backups
    p_backups = sub.add_parser(
        "gate-backups",
        help=(
            "List the gate's backup refs (old branch tips saved before destructive "
            "sync ops); --prune expires them"
        ),
    )
    _add_project_arg(p_backups)
    # List (no flag), prune, restore, and delete are the four modes — at most
    # one at a time.  ``--older-than`` only modifies ``--prune``.
    backups_ops = p_backups.add_mutually_exclusive_group()
    backups_ops.add_argument(
        "--prune",
        dest="prune",
        action="store_true",
        help="Delete backups older than the retention window instead of listing",
    )
    backups_ops.add_argument(
        "--restore",
        dest="restore",
        metavar="REF",
        default=None,
        help="Restore a branch to a backup ref (backs up the tip it replaces first)",
    )
    backups_ops.add_argument(
        "--delete",
        dest="delete",
        metavar="REF",
        default=None,
        help="Delete a single backup ref ahead of its retention expiry",
    )
    p_backups.add_argument(
        "--older-than",
        dest="older_than",
        type=int,
        metavar="DAYS",
        default=None,
        help="Override the project's retention window for this prune",
    )

    # agents — subgroup mirroring `terok agents` but scoped per-project
    p_agents = sub.add_parser(
        "agents",
        help="Inspect or set the per-project image.agents override",
    )
    agents_sub = p_agents.add_subparsers(dest="agents_cmd", required=True)
    p_agents_set = agents_sub.add_parser(
        "set",
        help="Write image.agents in the project's project.yml (interactive when no arg)",
        description=(
            "Set the agent selection baked into this project's L1 image, "
            "overriding the global default.  Validated against the installed "
            "roster.  Interactive picker when SELECTION is omitted."
        ),
    )
    _add_project_arg(p_agents_set)
    p_agents_set.add_argument(
        "selection",
        nargs="?",
        default=None,
        help=(
            'Agent selection in the executor\'s canonical grammar: "all", '
            'a comma list ("claude,vibe"), or "all,-name" to exclude one '
            '("all,-vibe").  Interactive picker when omitted.'
        ),
    )

dispatch(args)

Handle the project group. Returns True if handled.

Source code in src/terok/cli/commands/project.py
def dispatch(args: argparse.Namespace) -> bool:
    """Handle the ``project`` group.  Returns True if handled."""
    if args.cmd != "project":
        return False
    match args.project_cmd:
        case "list":
            _cmd_project_list()
        case "wizard":
            run_wizard(init_fn=cmd_project_init)
        case "derive":
            _cmd_project_derive(args.source_id, args.new_id)
        case "normalize-name":
            _cmd_project_normalize_name(args.project_name)
        case "delete":
            _cmd_project_delete(args.project_name, force=args.force)
        case "init":
            cmd_project_init(args.project_name)
        case "generate":
            generate_dockerfiles(args.project_name)
        case "build":
            with tee_output("build", project=args.project_name):
                build_images(
                    args.project_name,
                    include_dev=getattr(args, "dev", False),
                    refresh_agents=getattr(args, "refresh_agents", False),
                    full_rebuild=getattr(args, "full_rebuild", False),
                    agents=getattr(args, "agents", None),
                )
        case "ssh-init":
            _cmd_ssh_init(args)
        case "gate-path":
            _cmd_gate_path(args.project_name)
        case "gate-sync":
            _cmd_gate_sync(args)
        case "gate-backups":
            _cmd_gate_backups(args)
        case "agents":
            if args.agents_cmd == "set":
                _cmd_agents_set(args.project_name, getattr(args, "selection", None))
        case _:  # pragma: no cover - required=True makes argparse enforce this
            return False
    return True