"""Integration management CLI commands.""" from __future__ import annotations import click from platform.analytics.cli import ( capture_integration_removed, capture_integration_setup_completed, capture_integration_setup_started, capture_integration_verified, capture_integrations_listed, ) from surfaces.cli.constants import ( MANAGED_INTEGRATION_SERVICES, SETUP_SERVICES, VERIFY_SERVICES, ) class IntegrationServiceChoice(click.Choice): """``click.Choice`` that resolves integration-management service aliases. Without this, passing an intuitive alias such as ``posthog`` (whose only management flow is ``posthog_mcp``) fails the enum check with exit code 2 before ``cmd_setup``/``cmd_verify`` ever run. Resolving the alias here keeps Click's friendly ``[[a|b|c]]`` usage/error display and shell completion while accepting the canonical service name the handlers expect. """ def convert( self, value: object, param: click.Parameter | None, ctx: click.Context | None, ) -> object: if isinstance(value, str): from integrations.registry import resolve_management_service value = resolve_management_service(value) return super().convert(value, param, ctx) @click.group(name="integrations") def integrations() -> None: """Manage local integration credentials.""" @integrations.command(name="setup") @click.argument( "service", required=False, default=None, type=IntegrationServiceChoice(SETUP_SERVICES) ) def setup_integration(service: str | None) -> None: """Set up credentials for a service.""" from integrations.cli import cmd_setup, cmd_verify normalized_service = service or "prompt" capture_integration_setup_started(normalized_service) resolved_service = cmd_setup(service) capture_integration_setup_completed(resolved_service) if resolved_service in VERIFY_SERVICES: click.echo(f" Verifying {resolved_service}...\n") exit_code = cmd_verify(resolved_service) if exit_code == 0: capture_integration_verified(resolved_service) raise SystemExit(exit_code) @integrations.command(name="list") def list_integrations() -> None: """List all configured integrations.""" from integrations.cli import cmd_list capture_integrations_listed() cmd_list() @integrations.command(name="show") @click.argument("service", type=IntegrationServiceChoice(MANAGED_INTEGRATION_SERVICES)) def show_integration(service: str) -> None: """Show details for a configured integration.""" from integrations.cli import cmd_show cmd_show(service) @integrations.command(name="remove") @click.argument("service", type=IntegrationServiceChoice(MANAGED_INTEGRATION_SERVICES)) def remove_integration(service: str) -> None: """Remove a configured integration.""" from integrations.cli import cmd_remove cmd_remove(service) capture_integration_removed(service) @integrations.command(name="verify") @click.argument( "service", required=False, default=None, type=IntegrationServiceChoice(VERIFY_SERVICES) ) @click.option( "--send-slack-test", is_flag=True, help="Send a test message to the configured Slack webhook." ) def verify_integration( service: str | None, send_slack_test: bool, ) -> None: """Verify integration connectivity (all services, or a specific one).""" from integrations.cli import cmd_verify exit_code = cmd_verify( service, send_slack_test=send_slack_test, ) if exit_code == 0: capture_integration_verified(service or "all") raise SystemExit(exit_code)