|
1 | 1 | import os |
| 2 | +import subprocess |
2 | 3 | from typing import Dict, List, Optional |
3 | 4 |
|
4 | 5 | from InquirerPy import inquirer |
5 | 6 |
|
6 | 7 | from .ui import console |
7 | | -from .utils import ConfigPathUtility |
| 8 | +from .utils import get_config_file_host_data |
8 | 9 |
|
9 | 10 |
|
10 | 11 | def ask_host_prompt(): |
11 | | - HOST: List[str] = ConfigPathUtility.get_config_file_host_data() |
12 | | - questions: List[List] = inquirer.fuzzy( |
| 12 | + HOST: List[str] = get_config_file_host_data() |
| 13 | + questions = inquirer.fuzzy( |
13 | 14 | message="Select the Host Given in the Above List: ", |
14 | 15 | choices=HOST, |
15 | 16 | ) |
16 | 17 | try: |
17 | | - answers: Optional[Dict[str, str]] = questions.execute() |
| 18 | + answers = questions.execute() |
18 | 19 | if answers is None: |
19 | 20 | return |
20 | 21 |
|
21 | 22 | cmd: str = answers |
22 | | - cmd = cmd[7::] |
23 | | - cmd = f"ssh {cmd}" |
24 | | - os.system("cls" if os.name == "nt" else "clear") |
| 23 | + try: |
| 24 | + _cmd_exec_data = cmd.split("-> ")[1] # clean the data from answers |
| 25 | + except IndexError: |
| 26 | + raise ValueError("Invalid format: expected '-> ' delimiter in the answer.") |
| 27 | + if os.name == "nt": # Windows |
| 28 | + subprocess.run(["cls"], shell=True, check=True) |
| 29 | + else: # Unix-like systems |
| 30 | + subprocess.run(["clear"], check=True) |
25 | 31 | console.print( |
26 | 32 | "Please Wait While Your System is Connecting to the Remote Server 🖥️", |
27 | 33 | style="green", |
28 | 34 | ) |
29 | | - os.system(cmd) |
| 35 | + subprocess.run(["ssh", _cmd_exec_data], check=True) |
| 36 | + except subprocess.CalledProcessError as e: |
| 37 | + print(f"Error: {e.stdout}") |
30 | 38 | except Exception as Error: |
31 | 39 | print(f"\nInterrupted by {Error}") |
0 commit comments