|
4 | 4 | large changes to the OpenAPI spec. |
5 | 5 | """ |
6 | 6 |
|
7 | | -from typing import Dict |
| 7 | +from typing import Dict, List |
8 | 8 |
|
9 | 9 | from rich import box |
10 | 10 | from rich import print as rprint |
@@ -57,6 +57,15 @@ def handle_types_region_prices_list( |
57 | 57 | return linode_types_with_region_prices(operation, output_handler, json_data) |
58 | 58 |
|
59 | 59 |
|
| 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 | + |
60 | 69 | def linode_types_with_region_prices( |
61 | 70 | operation, output_handler, json_data |
62 | 71 | ) -> bool: |
@@ -137,3 +146,45 @@ def format_region_prices(data: Dict[str, any]) -> any: |
137 | 146 | sub_table.add_row(*region_price_row) |
138 | 147 |
|
139 | 148 | 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