|
4 | 4 | import functools |
5 | 5 | import importlib |
6 | 6 | import logging |
| 7 | +import platform |
7 | 8 | import sys |
8 | 9 | from pathlib import Path |
9 | 10 | from typing import TYPE_CHECKING, Any, Callable |
|
12 | 13 | import typer |
13 | 14 | import ujson |
14 | 15 | from rich.console import Console |
| 16 | +from rich.layout import Layout |
15 | 17 | from rich.logging import RichHandler |
| 18 | +from rich.panel import Panel |
| 19 | +from rich.pretty import Pretty |
| 20 | +from rich.table import Table |
16 | 21 | from rich.traceback import Traceback |
17 | 22 |
|
18 | 23 | from .. import __version__ as sdk_version |
@@ -392,11 +397,106 @@ def protocols( |
392 | 397 |
|
393 | 398 | @app.command(name="version") |
394 | 399 | @catch_exception(console=console) |
395 | | -def version(_: str = CONFIG_PARAM) -> None: |
396 | | - """Display the version of Infrahub and the version of the Python SDK in use.""" |
| 400 | +def version() -> None: |
| 401 | + """Display the version of Python and the version of the Python SDK in use.""" |
397 | 402 |
|
398 | | - client = initialize_client_sync() |
399 | | - response = client.execute_graphql(query="query { InfrahubInfo { version }}") |
| 403 | + console.print(f"Python: {platform.python_version()}\nPython SDK: v{sdk_version}") |
400 | 404 |
|
401 | | - infrahub_version = response["InfrahubInfo"]["version"] |
402 | | - console.print(f"Infrahub: v{infrahub_version}\nPython SDK: v{sdk_version}") |
| 405 | + |
| 406 | +@app.command(name="info") |
| 407 | +@catch_exception(console=console) |
| 408 | +def info(detail: bool = typer.Option(False, help="Display detailed information."), _: str = CONFIG_PARAM) -> None: # noqa: PLR0915 |
| 409 | + """Display the status of the Python SDK.""" |
| 410 | + |
| 411 | + info: dict[str, Any] = { |
| 412 | + "error": None, |
| 413 | + "status": ":x:", |
| 414 | + "infrahub_version": "N/A", |
| 415 | + "user_info": {}, |
| 416 | + "groups": {}, |
| 417 | + } |
| 418 | + try: |
| 419 | + client = initialize_client_sync() |
| 420 | + info["infrahub_version"] = client.get_version() |
| 421 | + info["user_info"] = client.get_user() |
| 422 | + info["status"] = ":white_heavy_check_mark:" |
| 423 | + info["groups"] = client.get_user_permissions() |
| 424 | + except Exception as e: |
| 425 | + info["error"] = f"{e!s} ({e.__class__.__name__})" |
| 426 | + |
| 427 | + if detail: |
| 428 | + layout = Layout() |
| 429 | + |
| 430 | + # Layout structure |
| 431 | + new_console = Console(height=45) |
| 432 | + layout = Layout() |
| 433 | + layout.split_column( |
| 434 | + Layout(name="body", ratio=1), |
| 435 | + ) |
| 436 | + layout["body"].split_row( |
| 437 | + Layout(name="left"), |
| 438 | + Layout(name="right"), |
| 439 | + ) |
| 440 | + |
| 441 | + layout["left"].split_column( |
| 442 | + Layout(name="connection_status", size=7), |
| 443 | + Layout(name="client_info", ratio=1), |
| 444 | + ) |
| 445 | + |
| 446 | + layout["right"].split_column( |
| 447 | + Layout(name="version_info", size=7), |
| 448 | + Layout(name="infrahub_info", ratio=1), |
| 449 | + ) |
| 450 | + |
| 451 | + # Connection status panel |
| 452 | + connection_status = Table(show_header=False, box=None) |
| 453 | + connection_status.add_row("Server Address:", client.config.address) |
| 454 | + connection_status.add_row("Status:", info["status"]) |
| 455 | + if info["error"]: |
| 456 | + connection_status.add_row("Error Reason:", info["error"]) |
| 457 | + layout["connection_status"].update(Panel(connection_status, title="Connection Status")) |
| 458 | + |
| 459 | + # Version information panel |
| 460 | + version_info = Table(show_header=False, box=None) |
| 461 | + version_info.add_row("Python Version:", platform.python_version()) |
| 462 | + version_info.add_row("Infrahub Version", info["infrahub_version"]) |
| 463 | + version_info.add_row("Infrahub SDK:", sdk_version) |
| 464 | + layout["version_info"].update(Panel(version_info, title="Version Information")) |
| 465 | + |
| 466 | + # SDK client configuration panel |
| 467 | + pretty_model = Pretty(client.config.model_dump(), expand_all=True) |
| 468 | + layout["client_info"].update(Panel(pretty_model, title="Client Info")) |
| 469 | + |
| 470 | + # Infrahub information planel |
| 471 | + infrahub_info = Table(show_header=False, box=None) |
| 472 | + if info["user_info"]: |
| 473 | + infrahub_info.add_row("User:", info["user_info"]["AccountProfile"]["display_label"]) |
| 474 | + infrahub_info.add_row("Description:", info["user_info"]["AccountProfile"]["description"]["value"]) |
| 475 | + infrahub_info.add_row("Status:", info["user_info"]["AccountProfile"]["status"]["label"]) |
| 476 | + infrahub_info.add_row( |
| 477 | + "Number of Groups:", str(info["user_info"]["AccountProfile"]["member_of_groups"]["count"]) |
| 478 | + ) |
| 479 | + |
| 480 | + if groups := info["groups"]: |
| 481 | + infrahub_info.add_row("Groups:", "") |
| 482 | + for group, roles in groups.items(): |
| 483 | + infrahub_info.add_row("", group, ", ".join(roles)) |
| 484 | + |
| 485 | + layout["infrahub_info"].update(Panel(infrahub_info, title="Infrahub Info")) |
| 486 | + |
| 487 | + new_console.print(layout) |
| 488 | + else: |
| 489 | + # Simple output |
| 490 | + table = Table(show_header=False, box=None) |
| 491 | + table.add_row("Address:", client.config.address) |
| 492 | + table.add_row("Connection Status:", info["status"]) |
| 493 | + if info["error"]: |
| 494 | + table.add_row("Connection Error:", info["error"]) |
| 495 | + |
| 496 | + table.add_row("Python Version:", platform.python_version()) |
| 497 | + table.add_row("SDK Version:", sdk_version) |
| 498 | + table.add_row("Infrahub Version:", info["infrahub_version"]) |
| 499 | + if account := info["user_info"].get("AccountProfile"): |
| 500 | + table.add_row("User:", account["display_label"]) |
| 501 | + |
| 502 | + console.print(table) |
0 commit comments