Skip to content

Commit 601dc54

Browse files
authored
Override images replicate output (#653)
1 parent 3f996eb commit 601dc54

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

linodecli/overrides.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
large changes to the OpenAPI spec.
55
"""
66

7-
from typing import Dict
7+
from typing import Dict, List
88

99
from rich import box
1010
from rich import print as rprint
@@ -57,6 +57,15 @@ def handle_types_region_prices_list(
5757
return linode_types_with_region_prices(operation, output_handler, json_data)
5858

5959

60+
@output_override("images", "replicate", OutputMode.table)
61+
def handle_image_replicate(operation, output_handler, json_data) -> bool:
62+
# pylint: disable=unused-argument
63+
"""
64+
Override the output of 'linode-cli images replicate'.
65+
"""
66+
return image_replicate_output(json_data)
67+
68+
6069
def linode_types_with_region_prices(
6170
operation, output_handler, json_data
6271
) -> bool:
@@ -137,3 +146,45 @@ def format_region_prices(data: Dict[str, any]) -> any:
137146
sub_table.add_row(*region_price_row)
138147

139148
return sub_table
149+
150+
151+
def build_replicas_output(replicas: List) -> Table:
152+
"""
153+
Format nested replicas list to a sub-table.
154+
"""
155+
replicas_output = Table(show_header=False, box=None)
156+
replicas_headers = replicas[0].keys()
157+
for replica in replicas:
158+
row = []
159+
for h in replicas_headers:
160+
row.append(Align(str(replica[h]), align="left"))
161+
replicas_output.add_row(*row)
162+
163+
return replicas_output
164+
165+
166+
def image_replicate_output(json_data) -> bool:
167+
"""
168+
Parse and format the image replicate output table.
169+
"""
170+
output = Table(
171+
header_style="bold",
172+
show_lines=True,
173+
)
174+
175+
row = []
176+
for header in json_data.keys():
177+
if header == "regions" and len(json_data[header]) > 0:
178+
# leverage `replicas` in output for readability
179+
output.add_column("replicas", justify="center")
180+
row.append(build_replicas_output(json_data[header]))
181+
elif json_data[header] is not None:
182+
output.add_column(header, justify="center")
183+
row.append(Align(str(json_data[header]), align="left"))
184+
185+
output.add_row(*row)
186+
187+
console = Console()
188+
console.print(output)
189+
190+
return False

0 commit comments

Comments
 (0)