Skip to content

Commit ad3ead1

Browse files
committed
DRYer
1 parent 37fb1e4 commit ad3ead1

File tree

1 file changed

+14
-24
lines changed

1 file changed

+14
-24
lines changed

kittens/remote_file/main.py

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,13 @@ def help_text(x: str) -> str:
9797
return {'e': 'edit', 'o': 'open', 's': 'save'}.get(response, 'cancel')
9898

9999

100-
def simple_copy_command(conn_data: SSHConnectionData, path: str) -> List[str]:
101-
cmd = [conn_data.binary]
102-
if conn_data.port:
103-
cmd += ['-p', str(conn_data.port)]
104-
cmd += [conn_data.hostname, 'cat', path]
105-
return cmd
106-
107-
108100
class ControlMaster:
109101

110-
def __init__(self, conn_data: SSHConnectionData, remote_path: str):
102+
def __init__(self, conn_data: SSHConnectionData, remote_path: str, dest: str = ''):
111103
self.conn_data = conn_data
112104
self.remote_path = remote_path
105+
self.dest = dest
106+
self.tdir = ''
113107
self.cmd_prefix = cmd = [
114108
conn_data.binary, '-o', f'ControlPath=~/.ssh/kitty-master-{os.getpid()}-%r@%h:%p',
115109
'-o', 'TCPKeepAlive=yes', '-o', 'ControlPersist=yes'
@@ -123,16 +117,18 @@ def __enter__(self) -> 'ControlMaster':
123117
self.cmd_prefix + ['-o', 'ControlMaster=auto', '-fN', self.conn_data.hostname])
124118
subprocess.check_call(
125119
self.batch_cmd_prefix + ['-O', 'check', self.conn_data.hostname])
126-
self.tdir = tempfile.mkdtemp()
127-
self.dest = os.path.join(self.tdir, os.path.basename(self.remote_path))
120+
if not self.dest:
121+
self.tdir = tempfile.mkdtemp()
122+
self.dest = os.path.join(self.tdir, os.path.basename(self.remote_path))
128123
return self
129124

130125
def __exit__(self, *a: Any) -> bool:
131126
subprocess.Popen(
132127
self.batch_cmd_prefix + ['-O', 'exit', self.conn_data.hostname],
133128
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, stdin=subprocess.DEVNULL
134129
).wait()
135-
shutil.rmtree(self.tdir)
130+
if self.tdir:
131+
shutil.rmtree(self.tdir)
136132
return True
137133

138134
@property
@@ -159,12 +155,6 @@ def upload(self, suppress_output: bool = True) -> bool:
159155
return subprocess.run(cmd, stdout=redirect, stderr=redirect, stdin=f).returncode == 0
160156

161157

162-
def save_output(cmd: List[str], dest_path: str) -> bool:
163-
with open(dest_path, 'wb') as f:
164-
cp = subprocess.run(cmd, stdout=f)
165-
return cp.returncode == 0
166-
167-
168158
Result = Optional[str]
169159

170160

@@ -241,20 +231,20 @@ def save_as(conn_data: SSHConnectionData, remote_path: str) -> None:
241231
dest = q
242232
if os.path.dirname(dest):
243233
os.makedirs(os.path.dirname(dest), exist_ok=True)
244-
cmd = simple_copy_command(conn_data, remote_path)
245-
if not save_output(cmd, dest):
246-
show_error('Failed to copy file from remote machine')
234+
with ControlMaster(conn_data, remote_path, dest=dest) as master:
235+
if not master.download():
236+
show_error('Failed to copy file from remote machine')
247237

248238

249239
def handle_action(action: str, cli_opts: RemoteFileCLIOptions) -> Result:
250240
conn_data = SSHConnectionData(*json.loads(cli_opts.ssh_connection_data or ''))
251241
remote_path = cli_opts.path or ''
252242
if action == 'open':
253243
print('Opening', cli_opts.path, 'from', cli_opts.hostname)
254-
cmd = simple_copy_command(conn_data, remote_path)
255244
dest = os.path.join(tempfile.mkdtemp(), os.path.basename(remote_path))
256-
if save_output(cmd, dest):
257-
return dest
245+
with ControlMaster(conn_data, remote_path, dest=dest) as master:
246+
if master.download():
247+
return dest
258248
show_error('Failed to copy file from remote machine')
259249
elif action == 'edit':
260250
print('Editing', cli_opts.path, 'from', cli_opts.hostname)

0 commit comments

Comments
 (0)