|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import asyncio |
4 | | -import json |
| 4 | +import json as json_lib |
5 | 5 | import logging |
6 | 6 | import shutil |
7 | 7 | from decimal import Decimal |
@@ -864,7 +864,25 @@ async def list_instances( |
864 | 864 | raise typer.Exit(code=1) |
865 | 865 |
|
866 | 866 | execution = await client.instance.get_instance_executions_info(allocations) |
867 | | - await show_instances(messages=instances, allocations=allocations, executions=execution) |
| 867 | + |
| 868 | + if not json: |
| 869 | + await show_instances(messages=instances, allocations=allocations, executions=execution) |
| 870 | + else: |
| 871 | + # Create a single JSON array with one object per instance, combining all related data |
| 872 | + instances_data = [] |
| 873 | + for message in instances: |
| 874 | + allocation = allocations.root.get(message.item_hash, None) |
| 875 | + execution_info = execution.root.get(message.item_hash, None) |
| 876 | + |
| 877 | + instance_entry = { |
| 878 | + "instance": message.model_dump(), |
| 879 | + "allocation": allocation.model_dump() if allocation else None, |
| 880 | + "execution": execution_info.model_dump() if execution_info else None, |
| 881 | + } |
| 882 | + instances_data.append(instance_entry) |
| 883 | + |
| 884 | + console = Console() |
| 885 | + console.print(json_lib.dumps(instances_data, indent=2, default=str)) |
868 | 886 |
|
869 | 887 |
|
870 | 888 | @app.command() |
@@ -956,7 +974,7 @@ async def logs( |
956 | 974 | async with VmClient(account, domain) as manager: |
957 | 975 | try: |
958 | 976 | async for log in manager.get_logs(vm_id=vm_id): |
959 | | - log_data = json.loads(log) |
| 977 | + log_data = json_lib.loads(log) |
960 | 978 | if "message" in log_data: |
961 | 979 | echo(log_data["message"]) |
962 | 980 | except aiohttp.ClientConnectorError as e: |
|
0 commit comments