Skip to content

Commit f48cf51

Browse files
committed
Cleanup exit flow control for remote hostname not matching
1 parent b78bce7 commit f48cf51

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

kittens/remote_file/main.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def is_alive(self) -> bool:
147147
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, stdin=subprocess.DEVNULL
148148
).wait() == 0
149149

150-
def check_hostname_matches(self) -> None:
150+
def check_hostname_matches(self) -> bool:
151151
cp = subprocess.run(self.batch_cmd_prefix + [self.conn_data.hostname, 'hostname', '-f'], stdout=subprocess.PIPE,
152152
stderr=subprocess.DEVNULL, stdin=subprocess.DEVNULL)
153153
if cp.returncode == 0:
@@ -170,9 +170,9 @@ def check_hostname_matches(self) -> None:
170170
)
171171
sys.stdout.flush()
172172
response = get_key_press('yn', 'n')
173-
if response != 'y':
174-
raise SystemExit(1)
175173
print(reset_terminal(), end='')
174+
return response == 'y'
175+
return True
176176

177177
def download(self) -> bool:
178178
with open(self.dest, 'wb') as f:
@@ -268,9 +268,9 @@ def save_as(conn_data: SSHConnectionData, remote_path: str, cli_opts: RemoteFile
268268
if os.path.dirname(dest):
269269
os.makedirs(os.path.dirname(dest), exist_ok=True)
270270
with ControlMaster(conn_data, remote_path, cli_opts, dest=dest) as master:
271-
master.check_hostname_matches()
272-
if not master.download():
273-
show_error('Failed to copy file from remote machine')
271+
if master.check_hostname_matches():
272+
if not master.download():
273+
show_error('Failed to copy file from remote machine')
274274

275275

276276
def handle_action(action: str, cli_opts: RemoteFileCLIOptions) -> Result:
@@ -280,14 +280,15 @@ def handle_action(action: str, cli_opts: RemoteFileCLIOptions) -> Result:
280280
print('Opening', cli_opts.path, 'from', cli_opts.hostname)
281281
dest = os.path.join(tempfile.mkdtemp(), os.path.basename(remote_path))
282282
with ControlMaster(conn_data, remote_path, cli_opts, dest=dest) as master:
283-
master.check_hostname_matches()
284-
if master.download():
285-
return dest
286-
show_error('Failed to copy file from remote machine')
283+
if master.check_hostname_matches():
284+
if master.download():
285+
return dest
286+
show_error('Failed to copy file from remote machine')
287287
elif action == 'edit':
288288
print('Editing', cli_opts.path, 'from', cli_opts.hostname)
289289
with ControlMaster(conn_data, remote_path, cli_opts) as master:
290-
master.check_hostname_matches()
290+
if not master.check_hostname_matches():
291+
return None
291292
if not master.download():
292293
show_error(f'Failed to download {remote_path}')
293294
return None

0 commit comments

Comments
 (0)