(
ists: list[Instance],
format: str = "table",
numbered: bool = False,
show_launched_by: bool = False,
)
| 153 | |
| 154 | |
| 155 | def print_instances( |
| 156 | ists: list[Instance], |
| 157 | format: str = "table", |
| 158 | numbered: bool = False, |
| 159 | show_launched_by: bool = False, |
| 160 | ) -> None: |
| 161 | if format == "csv": |
| 162 | # CSV is machine-readable: emit the full set of columns, unabridged. |
| 163 | field_names = [ |
| 164 | "Name", |
| 165 | "Instance ID", |
| 166 | "Public IP Address", |
| 167 | "Private IP Address", |
| 168 | "Launched By", |
| 169 | "Delete After", |
| 170 | "State", |
| 171 | ] |
| 172 | if numbered: |
| 173 | field_names = ["#"] + field_names |
| 174 | w = csv.writer(sys.stdout) |
| 175 | w.writerow(field_names) |
| 176 | for idx, i in enumerate(ists, 1): |
| 177 | t = tags(i) |
| 178 | row = [ |
| 179 | name(t), |
| 180 | i.instance_id, |
| 181 | i.public_ip_address, |
| 182 | i.private_ip_address, |
| 183 | launched_by(t), |
| 184 | delete_after(t), |
| 185 | i.state["Name"], |
| 186 | ] |
| 187 | if numbered: |
| 188 | row = [idx] + row |
| 189 | w.writerow(row) |
| 190 | return |
| 191 | |
| 192 | if format != "table": |
| 193 | raise RuntimeError("Unknown format passed to print_instances") |
| 194 | |
| 195 | # Table is for humans: a compact subset that fits a default terminal. |
| 196 | # Private IP is omitted (mssh connects by instance id, not IP) and the |
| 197 | # owner only shown when listing instances beyond your own. |
| 198 | headers = ["INSTANCE ID", "NAME", "STATE", "EXPIRES", "PUBLIC IP"] |
| 199 | if show_launched_by: |
| 200 | headers.append("LAUNCHED BY") |
| 201 | if numbered: |
| 202 | headers = ["#"] + headers |
| 203 | rows = [] |
| 204 | for idx, i in enumerate(ists, 1): |
| 205 | t = tags(i) |
| 206 | state = i.state["Name"] |
| 207 | # A dead instance has no meaningful expiry; don't show a future time. |
| 208 | expires = ( |
| 209 | "-" |
| 210 | if state in ("terminated", "shutting-down") |
| 211 | else _format_expires(delete_after(t)) |
| 212 | ) |
no test coverage detected