summaryrefslogtreecommitdiffstats
path: root/pgcli/packages/prompt_utils.py
blob: 997b86e7fbf7c53a7365c8854f5f9321df8b7ecd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import sys
import click
from .parseutils import is_destructive


def confirm_destructive_query(queries, keywords, alias):
    """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.

    """
    info = "You're about to run a destructive command"
    if alias:
        info += f" in {click.style(alias, fg='red')}"

    prompt_text = f"{info}.\nDo you want to proceed?"
    if is_destructive(queries, keywords) and sys.stdin.isatty():
        return confirm(prompt_text)


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