diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2021-02-08 10:31:05 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2021-02-08 10:31:05 +0000 |
commit | 6884720fae8a2622b14e93d9e35ca5fcc2283b40 (patch) | |
tree | df6f736bb623cdd7932bbe2256101a6ac4ef7f35 /pgcli/packages/prompt_utils.py | |
parent | Initial commit. (diff) | |
download | pgcli-6884720fae8a2622b14e93d9e35ca5fcc2283b40.tar.xz pgcli-6884720fae8a2622b14e93d9e35ca5fcc2283b40.zip |
Adding upstream version 3.1.0.upstream/3.1.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'pgcli/packages/prompt_utils.py')
-rw-r--r-- | pgcli/packages/prompt_utils.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/pgcli/packages/prompt_utils.py b/pgcli/packages/prompt_utils.py new file mode 100644 index 0000000..3c58490 --- /dev/null +++ b/pgcli/packages/prompt_utils.py @@ -0,0 +1,35 @@ +import sys +import click +from .parseutils import is_destructive + + +def confirm_destructive_query(queries): + """Check if the query is destructive and prompts the user to confirm. + + Returns: + * None if the query is non-destructive or we can't prompt the user. + * True if the query is destructive and the user wants to proceed. + * False if the query is destructive and the user doesn't want to proceed. + + """ + prompt_text = ( + "You're about to run a destructive command.\n" "Do you want to proceed? (y/n)" + ) + if is_destructive(queries) and sys.stdin.isatty(): + return prompt(prompt_text, type=bool) + + +def confirm(*args, **kwargs): + """Prompt for confirmation (yes/no) and handle any abort exceptions.""" + try: + return click.confirm(*args, **kwargs) + except click.Abort: + return False + + +def prompt(*args, **kwargs): + """Prompt the user for input and handle any abort exceptions.""" + try: + return click.prompt(*args, **kwargs) + except click.Abort: + return False |