|
| 1 | +"""Catalog CLI commands for VGI. |
| 2 | +
|
| 3 | +This module provides CLI commands for catalog operations: |
| 4 | +- list: List available catalogs |
| 5 | +- attach: Attach to a catalog |
| 6 | +- detach: Detach from a catalog |
| 7 | +- create: Create a new catalog |
| 8 | +- drop: Drop a catalog |
| 9 | +- version: Get catalog version |
| 10 | +
|
| 11 | +""" |
| 12 | + |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +import click |
| 16 | + |
| 17 | +from vgi.catalog import OnConflict |
| 18 | +from vgi.client.client import Client |
| 19 | +from vgi.client.cli_utils import ( |
| 20 | + catalog_attach_result_to_dict, |
| 21 | + hex_to_attach_id, |
| 22 | + hex_to_transaction_id, |
| 23 | + output_json, |
| 24 | + parse_json_option, |
| 25 | +) |
| 26 | + |
| 27 | + |
| 28 | +@click.group() |
| 29 | +def catalog() -> None: |
| 30 | + """Manage catalogs.""" |
| 31 | + |
| 32 | + |
| 33 | +@catalog.command("list") |
| 34 | +@click.option("--server", required=True, help="VGI worker command") |
| 35 | +def catalog_list(server: str) -> None: |
| 36 | + """List available catalogs.""" |
| 37 | + client = Client(server) |
| 38 | + catalogs = client.catalogs() |
| 39 | + output_json(catalogs) |
| 40 | + |
| 41 | + |
| 42 | +@catalog.command("attach") |
| 43 | +@click.argument("name") |
| 44 | +@click.option("--server", required=True, help="VGI worker command") |
| 45 | +@click.option("--options", default="{}", help="Catalog options as JSON object") |
| 46 | +def catalog_attach(name: str, server: str, options: str) -> None: |
| 47 | + """Attach to a catalog and return attach_id. |
| 48 | +
|
| 49 | + NAME is the catalog name to attach to. |
| 50 | +
|
| 51 | + """ |
| 52 | + opts = parse_json_option(options, "--options") |
| 53 | + client = Client(server) |
| 54 | + result = client.catalog_attach(name=name, options=opts) |
| 55 | + output_json(catalog_attach_result_to_dict(result)) |
| 56 | + |
| 57 | + |
| 58 | +@catalog.command("detach") |
| 59 | +@click.argument("attach_id") |
| 60 | +@click.option("--server", required=True, help="VGI worker command") |
| 61 | +def catalog_detach(attach_id: str, server: str) -> None: |
| 62 | + """Detach from a catalog. |
| 63 | +
|
| 64 | + ATTACH_ID is the hex-encoded attach ID from catalog attach. |
| 65 | +
|
| 66 | + """ |
| 67 | + client = Client(server) |
| 68 | + client.catalog_detach(attach_id=hex_to_attach_id(attach_id)) |
| 69 | + output_json({"status": "detached"}) |
| 70 | + |
| 71 | + |
| 72 | +@catalog.command("create") |
| 73 | +@click.argument("name") |
| 74 | +@click.option("--server", required=True, help="VGI worker command") |
| 75 | +@click.option( |
| 76 | + "--on-conflict", |
| 77 | + type=click.Choice(["error", "ignore", "replace"]), |
| 78 | + default="error", |
| 79 | + help="Behavior if catalog already exists", |
| 80 | +) |
| 81 | +@click.option("--options", default="{}", help="Catalog options as JSON object") |
| 82 | +def catalog_create(name: str, server: str, on_conflict: str, options: str) -> None: |
| 83 | + """Create a new catalog. |
| 84 | +
|
| 85 | + NAME is the name for the new catalog. |
| 86 | +
|
| 87 | + """ |
| 88 | + opts = parse_json_option(options, "--options") |
| 89 | + client = Client(server) |
| 90 | + client.catalog_create( |
| 91 | + name=name, |
| 92 | + on_conflict=OnConflict(on_conflict), |
| 93 | + options=opts, |
| 94 | + ) |
| 95 | + output_json({"status": "created", "name": name}) |
| 96 | + |
| 97 | + |
| 98 | +@catalog.command("drop") |
| 99 | +@click.argument("name") |
| 100 | +@click.option("--server", required=True, help="VGI worker command") |
| 101 | +def catalog_drop(name: str, server: str) -> None: |
| 102 | + """Drop a catalog. |
| 103 | +
|
| 104 | + NAME is the name of the catalog to drop. |
| 105 | +
|
| 106 | + """ |
| 107 | + client = Client(server) |
| 108 | + client.catalog_drop(name=name) |
| 109 | + output_json({"status": "dropped", "name": name}) |
| 110 | + |
| 111 | + |
| 112 | +@catalog.command("version") |
| 113 | +@click.argument("attach_id") |
| 114 | +@click.option("--server", required=True, help="VGI worker command") |
| 115 | +@click.option("--transaction-id", help="Transaction ID (hex) for transactional read") |
| 116 | +def catalog_version(attach_id: str, server: str, transaction_id: str | None) -> None: |
| 117 | + """Get the current catalog version. |
| 118 | +
|
| 119 | + ATTACH_ID is the hex-encoded attach ID from catalog attach. |
| 120 | +
|
| 121 | + """ |
| 122 | + client = Client(server) |
| 123 | + version = client.catalog_version( |
| 124 | + attach_id=hex_to_attach_id(attach_id), |
| 125 | + transaction_id=( |
| 126 | + hex_to_transaction_id(transaction_id) if transaction_id else None |
| 127 | + ), |
| 128 | + ) |
| 129 | + output_json({"version": version, "attach_id": attach_id}) |
0 commit comments