Show running instances and let the user pick one.
()
| 43 | |
| 44 | |
| 45 | def pick_instance() -> Instance: |
| 46 | """Show running instances and let the user pick one.""" |
| 47 | from materialize.scratch import print_instances |
| 48 | |
| 49 | instances = list_all_instances() |
| 50 | active = [ |
| 51 | i |
| 52 | for i in instances |
| 53 | if i.state and i.state["Name"] in ("running", "pending", "stopping", "stopped") |
| 54 | ] |
| 55 | |
| 56 | if not active: |
| 57 | raise RuntimeError("No active instances found.") |
| 58 | |
| 59 | if len(active) == 1: |
| 60 | return active[0] |
| 61 | |
| 62 | print_instances(active, numbered=True) |
| 63 | |
| 64 | while True: |
| 65 | choice = input("Select an instance [#]: ").strip() |
| 66 | try: |
| 67 | idx = int(choice) |
| 68 | if 1 <= idx <= len(active): |
| 69 | return active[idx - 1] |
| 70 | except ValueError: |
| 71 | pass |
| 72 | print(f"Invalid choice: {choice!r}. Enter a number 1-{len(active)}.") |
no test coverage detected