Coverage for src/commands/catalog_selection.py: 88%
25 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-06-05 22:56 -0700
« prev ^ index » next coverage.py v7.8.0, created at 2025-06-05 22:56 -0700
1"""
2Command handler for catalog selection.
4This module contains the handler for setting the active catalog
5for database operations.
6"""
8import logging
9from typing import Optional
11from src.clients.databricks import DatabricksAPIClient
12from src.command_registry import CommandDefinition
13from src.config import set_active_catalog
14from .base import CommandResult
17def handle_command(client: Optional[DatabricksAPIClient], **kwargs) -> CommandResult:
18 """
19 Set the active catalog.
21 Args:
22 client: API client instance
23 **kwargs: catalog_name (str)
24 """
25 catalog_name: str = kwargs.get("catalog_name")
26 if not catalog_name:
27 return CommandResult(False, message="catalog_name parameter is required.")
29 try:
30 catalog_type = "Unknown"
31 try:
32 from src.catalogs import get_catalog
34 catalog_info = get_catalog(client, catalog_name)
35 catalog_type = catalog_info.get("type", "Unknown").lower()
36 except Exception:
37 set_active_catalog(catalog_name) # Set anyway if verification fails
38 return CommandResult(
39 True,
40 message=f"Warning: Could not verify catalog '{catalog_name}'. Setting anyway.",
41 data={"catalog_name": catalog_name, "catalog_type": catalog_type},
42 )
44 set_active_catalog(catalog_name)
45 return CommandResult(
46 True,
47 message=f"Active catalog is now set to '{catalog_name}' (Type: {catalog_type}).",
48 data={"catalog_name": catalog_name, "catalog_type": catalog_type},
49 )
50 except Exception as e:
51 logging.error(f"Failed to set catalog '{catalog_name}': {e}", exc_info=True)
52 return CommandResult(False, error=e, message=str(e))
55DEFINITION = CommandDefinition(
56 name="set-catalog",
57 description="Set the active catalog for database operations",
58 handler=handle_command,
59 parameters={
60 "catalog_name": {
61 "type": "string",
62 "description": "Name of the catalog to set as active",
63 }
64 },
65 required_params=["catalog_name"],
66 tui_aliases=["/select-catalog"],
67 visible_to_user=True,
68 visible_to_agent=True,
69 condensed_action="Setting catalog",
70)