Handle error messages form terminal during bootstrap

This commit is contained in:
Kovid Goyal 2022-02-26 14:57:01 +05:30
parent 53c8485a7a
commit 37c185462a
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
2 changed files with 17 additions and 6 deletions

View file

@ -11,7 +11,9 @@
import tempfile
import time
from contextlib import suppress
from typing import Dict, Iterator, List, NoReturn, Optional, Set, Tuple, Union
from typing import (
Any, Dict, Iterator, List, NoReturn, Optional, Set, Tuple, Union
)
from kitty.constants import cache_dir, shell_integration_dir, terminfo_dir
from kitty.shell_integration import get_effective_ksi_env_var
@ -55,26 +57,30 @@ def filter_files(tarinfo: tarfile.TarInfo) -> Optional[tarfile.TarInfo]:
def get_ssh_data(msg: str, shell_integration_dest: str = DEFAULT_SHELL_INTEGRATION_DEST) -> Iterator[bytes]:
def fmt_prefix(msg: Any) -> bytes:
return f'\036{msg}:'.encode('ascii')
try:
hostname, pwfilename, pw = msg.split(':', 2)
except Exception:
yield b' invalid ssh data request message\n'
yield fmt_prefix('!invalid ssh data request message')
try:
with open(os.path.join(cache_dir(), pwfilename)) as f:
os.unlink(f.name)
if pw != f.read():
raise ValueError('Incorrect password')
except Exception:
yield b' incorrect ssh data password\n'
yield fmt_prefix('!incorrect ssh data password')
else:
try:
data = make_tarfile(hostname, shell_integration_dest)
except Exception:
yield b' error while gathering ssh data\n'
yield fmt_prefix('!error while gathering ssh data')
else:
from base64 import standard_b64encode
encoded_data = standard_b64encode(data)
yield f"\036{len(encoded_data)}:".encode('ascii')
yield fmt_prefix(len(encoded_data))
yield encoded_data

View file

@ -7,7 +7,7 @@ cleanup_on_bootstrap_exit() {
[ ! -z "$saved_tty_settings" ] && command stty "$saved_tty_settings"
}
die() { echo "$*" > /dev/stderr; cleanup_on_bootstrap_exit; exit 1; }
die() { printf "\033[31m%s\033[m\n" "$*" > /dev/stderr; cleanup_on_bootstrap_exit; exit 1; }
dsc_to_kitty() { printf "\033P@kitty-$1|%s\033\\" "$(printf "%s" "$2" | base64)" > /dev/tty; }
debug() { dsc_to_kitty "print" "debug $1"; }
echo_via_kitty() { dsc_to_kitty "echo" "$1"; }
@ -67,6 +67,11 @@ get_data() {
fi
fi
done
case "$size" in
("!"*)
die "$size"
;;
esac
# using dd with bs=1 is very slow on Linux, so use head
command head -c "$size" | untar
# command dd bs=1 "count=$size" 2> /dev/null | untar