@@ -34,21 +34,29 @@ def get_command_history() -> list:
3434 return list (command_history )
3535
3636
37- async def __execute_command (command : str , username : str ) -> Tuple [bool , str ]:
37+ async def __execute_command (
38+ command : str , username : str , use_log : bool = True , dry_run : bool = False
39+ ) -> Tuple [bool , str ]:
3840 """
3941 Execute a command on the remote server via SSH.
4042
4143 Args:
4244 command (str): The command to execute.
4345 username (str): The SSH username.
46+ use_log (bool): If True, save the command at a command history (in-memory).
47+ dry_run (bool): If True, the command is not actually executed.
4448 Returns:
4549 Tuple[bool, str]: A tuple containing a boolean indicating success
4650 or failure and the output or error message.
4751 """
4852 if username == "root" :
4953 command = f"dokku { command } "
5054
51- _log_command (command )
55+ if use_log :
56+ _log_command (command )
57+
58+ if dry_run :
59+ return True , ""
5260
5361 try :
5462 async with asyncssh .connect (
@@ -80,17 +88,23 @@ async def __execute_command(command: str, username: str) -> Tuple[bool, str]:
8088 return False , str (error )
8189
8290
83- async def run_command (command : str ) -> Tuple [bool , str ]:
91+ async def run_command (
92+ command : str , use_log : bool = True , dry_run : bool = False
93+ ) -> Tuple [bool , str ]:
8494 """
8595 Run a command on the remote server via SSH.
8696
8797 Args:
8898 command (str): The command to execute.
99+ use_log (bool): If True, save the command at a command history (in-memory).
100+ dry_run (bool): If True, the command is not actually executed.
89101 Returns:
90102 Tuple[bool, str]: A tuple containing a boolean indicating success
91103 or failure and the output or error message.
92104 """
93- success , message = await __execute_command (command , "dokku" )
105+ success , message = await __execute_command (
106+ command , "dokku" , use_log = use_log , dry_run = dry_run
107+ )
94108
95109 if success :
96110 logging .info (f"Command executed successfully: { command } " )
@@ -99,17 +113,23 @@ async def run_command(command: str) -> Tuple[bool, str]:
99113 return success , message
100114
101115
102- async def run_command_as_root (command : str ) -> Tuple [bool , str ]:
116+ async def run_command_as_root (
117+ command : str , use_log : bool = True , dry_run : bool = False
118+ ) -> Tuple [bool , str ]:
103119 """
104120 Run a command on the remote server via SSH as root.
105121
106122 Args:
107123 command (str): The command to execute.
124+ use_log (bool): If True, save the command at a command history (in-memory).
125+ dry_run (bool): If True, the command is not actually executed.
108126 Returns:
109127 Tuple[bool, str]: A tuple containing a boolean indicating success
110128 or failure and the output or error message.
111129 """
112- success , message = await __execute_command (command , "root" )
130+ success , message = await __execute_command (
131+ command , "root" , use_log = use_log , dry_run = dry_run
132+ )
113133
114134 if success :
115135 logging .info (f"Command executed successfully: { command } " )
0 commit comments