Skip to content

Commit

Permalink
Merge pull request #328 from doronz88/feature/reuse-client
Browse files Browse the repository at this point in the history
shell: add an optional flag to reuse existing client
  • Loading branch information
doronz88 authored Dec 3, 2023
2 parents b41f026 + 1ac8dbe commit 5de7a1b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/rpcclient/rpcclient/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ def close(self):
with self._protocol_lock:
self._close()

def shell(self):
def shell(self, reuse_client: bool = False):
self._logger.disabled = True
self._ipython_run_cell_hook_enabled = False

Expand All @@ -517,6 +517,9 @@ def shell(self):
if home_rc.exists():
args.append(str(home_rc.expanduser().absolute()))
args.append(str((Path(rpcclient.__file__).parent / 'xonshrc.py').absolute()))

if reuse_client:
XSH.ctx['_client_to_reuse'] = self
XSH.ctx['_create_socket_cb'] = self._create_socket_cb

try:
Expand Down
15 changes: 9 additions & 6 deletions src/rpcclient/rpcclient/xonshrc.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@
from xonsh.events import events
from xonsh.tools import print_color

import rpcclient
from rpcclient.client import Client
from rpcclient.client_factory import create_client
from rpcclient.exceptions import RpcClientException
from rpcclient.ios.client import IosClient
from rpcclient.linux.client import LinuxClient
from rpcclient.macos.client import MacosClient
from rpcclient.structs.consts import SIGTERM


Expand Down Expand Up @@ -184,13 +187,13 @@ def print(self, *objects, sep=' ', end='\n', file=sys.stdout, flush=False):

class XonshRc:
def __init__(self):
self.client: Union[None, rpcclient.client.Client, rpcclient.darwin.client.DarwinClient] = None
self.client: Union[None, Client, IosClient, MacosClient, LinuxClient] = XSH.ctx.get('_client_to_reuse')
self._commands = {}
self._orig_aliases = {}
self._orig_prompt = XSH.env['PROMPT']
self._register_rpc_command('rpc-connect', self._rpc_connect)
self._register_rpc_command('rpc-connect', lambda: self._rpc_connect(self.client))
self._register_rpc_command('rpc-list-commands', self._rpc_list_commands)
self._rpc_connect()
self._rpc_connect(self.client)

print_color('''
{BOLD_WHITE}Welcome to xonsh-rpc shell! 👋{RESET}
Expand Down Expand Up @@ -223,11 +226,11 @@ def _rpc_disconnect(self, args, stdin, stdout, stderr):
for k, v in self._orig_aliases.items():
XSH.aliases[k] = v

def _rpc_connect(self):
def _rpc_connect(self, client_to_reuse: Union[None, Client, IosClient, MacosClient, LinuxClient]):
"""
connect to remote rpcserver
"""
self.client = create_client(XSH.ctx['_create_socket_cb'])
self.client = client_to_reuse or create_client(XSH.ctx['_create_socket_cb'])

# clear all host commands except for some useful ones
XSH.env['PATH'].clear()
Expand Down

0 comments on commit 5de7a1b

Please sign in to comment.