diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2020-03-21 10:28:17 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2021-02-07 11:16:53 +0000 |
commit | 06cba6ccd165ca8b224797e37fccb9e63f026d77 (patch) | |
tree | e82f1bc439997ae296f2e74f8a64d84c5d95f140 /iredis/warning.py | |
parent | Initial commit. (diff) | |
download | iredis-06cba6ccd165ca8b224797e37fccb9e63f026d77.tar.xz iredis-06cba6ccd165ca8b224797e37fccb9e63f026d77.zip |
Adding upstream version 1.9.1.upstream/1.9.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'iredis/warning.py')
-rw-r--r-- | iredis/warning.py | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/iredis/warning.py b/iredis/warning.py new file mode 100644 index 0000000..bb8231e --- /dev/null +++ b/iredis/warning.py @@ -0,0 +1,59 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + + +import sys +import click +from .commands import dangerous_commands + + +class ConfirmBoolParamType(click.ParamType): + name = "confirmation" + + def convert(self, value, param, ctx): + if isinstance(value, bool): + return bool(value) + value = value.lower() + if value in ("yes", "y"): + return True + elif value in ("no", "n"): + return False + self.fail("%s is not a valid boolean" % value, param, ctx) + + def __repr__(self): + return "BOOL" + + +BOOLEAN_TYPE = ConfirmBoolParamType() + + +def is_dangerous(command): + """ + :return : return True, reason str if command is dangerous; + return False, None otherwise. + """ + reason = dangerous_commands.get(command) + return reason is not None, reason + + +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 + + +def confirm_dangerous_command(upper_command): + """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. + + """ + dangerous, reason = is_dangerous(upper_command) + prompt_text = f"{reason}.\n" "Do you want to proceed? (y/n)" + if dangerous and sys.stdin.isatty(): + return prompt(prompt_text, type=BOOLEAN_TYPE) |