added input prompt functionality to bacloud client

This commit is contained in:
Eric Froemling 2020-03-09 20:38:37 -07:00
parent 5a389efb65
commit 84338018b9

View File

@ -92,6 +92,10 @@ class Response:
dir_prune_empty: If present, all empty dirs under this one should be dir_prune_empty: If present, all empty dirs under this one should be
removed. removed.
open_url: If present, url to display to the user. open_url: If present, url to display to the user.
input_prompt: If present, a line of input is read and placed into
endcommand args as 'input'. The first value is the prompt printed
before reading and the second is whether it should be read as a
password (without echoing to the terminal).
end_message: If present, a message that should be printed after all other end_message: If present, a message that should be printed after all other
response processing is done. response processing is done.
end_message_end: end arg for end_message print() call. end_message_end: end arg for end_message print() call.
@ -110,6 +114,7 @@ class Response:
deletes: Optional[List[str]] = None deletes: Optional[List[str]] = None
dir_prune_empty: Optional[str] = None dir_prune_empty: Optional[str] = None
open_url: Optional[str] = None open_url: Optional[str] = None
input_prompt: Optional[Tuple[str, bool]] = None
end_message: Optional[str] = None end_message: Optional[str] = None
end_message_end: str = '\n' end_message_end: str = '\n'
end_command: Optional[Tuple[str, Dict]] = None end_command: Optional[Tuple[str, Dict]] = None
@ -263,6 +268,7 @@ class App:
't': json.dumps(self._state.login_token), 't': json.dumps(self._state.login_token),
'd': json.dumps(data), 'd': json.dumps(data),
'z': get_tz_offset_seconds(), 'z': get_tz_offset_seconds(),
'y': int(sys.stdout.isatty()),
}, },
files=files) files=files)
response_raw_2.raise_for_status() # Except if anything went wrong. response_raw_2.raise_for_status() # Except if anything went wrong.
@ -383,6 +389,15 @@ class App:
import webbrowser import webbrowser
webbrowser.open(url) webbrowser.open(url)
def _handle_input_prompt(self, prompt: str, as_password: bool) -> None:
if as_password:
from getpass import getpass
self._end_command_args['input'] = getpass(prompt=prompt)
else:
if prompt:
print(prompt, end='', flush=True)
self._end_command_args['input'] = input()
def run_user_command(self, args: List[str]) -> None: def run_user_command(self, args: List[str]) -> None:
"""Run a single user command to completion.""" """Run a single user command to completion."""
# pylint: disable=too-many-branches # pylint: disable=too-many-branches
@ -412,6 +427,9 @@ class App:
self._handle_dir_prune_empty(response.dir_prune_empty) self._handle_dir_prune_empty(response.dir_prune_empty)
if response.open_url is not None: if response.open_url is not None:
self._handle_open_url(response.open_url) self._handle_open_url(response.open_url)
if response.input_prompt is not None:
self._handle_input_prompt(prompt=response.input_prompt[0],
as_password=response.input_prompt[1])
if response.end_message is not None: if response.end_message is not None:
print(response.end_message, print(response.end_message,
end=response.end_message_end, end=response.end_message_end,