From 6053d9631faa8b5fdbf09fbc3188a1a5da2e2abf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Wed, 14 May 2025 10:26:19 +0200 Subject: [PATCH] prompt: handle KeyboardInterrupt more gracefully --- pkgs/clan-cli/clan_cli/vars/prompt.py | 30 +++++++++++++++++---------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/vars/prompt.py b/pkgs/clan-cli/clan_cli/vars/prompt.py index f1a3aa0da..5d4f1bad5 100644 --- a/pkgs/clan-cli/clan_cli/vars/prompt.py +++ b/pkgs/clan-cli/clan_cli/vars/prompt.py @@ -7,6 +7,8 @@ from dataclasses import dataclass from getpass import getpass from typing import Any +from clan_cli.errors import ClanError + log = logging.getLogger(__name__) # This is for simulating user input in tests. @@ -107,17 +109,23 @@ def ask( log.info(f"Prompting value for {ident}") if MOCK_PROMPT_RESPONSE: return next(MOCK_PROMPT_RESPONSE) - match input_type: - case PromptType.LINE: - result = input(f"{text}: ") - case PromptType.MULTILINE: - print(f"{text} (Finish with Ctrl-D): ") - result = sys.stdin.read() - case PromptType.MULTILINE_HIDDEN: - print("Enter multiple lines (press Ctrl-D to finish or Ctrl-C to cancel):") - result = get_multiline_hidden_input() - case PromptType.HIDDEN: - result = getpass(f"{text} (hidden): ") + try: + match input_type: + case PromptType.LINE: + result = input(f"{text}: ") + case PromptType.MULTILINE: + print(f"{text} (Finish with Ctrl-D): ") + result = sys.stdin.read() + case PromptType.MULTILINE_HIDDEN: + print( + "Enter multiple lines (press Ctrl-D to finish or Ctrl-C to cancel):" + ) + result = get_multiline_hidden_input() + case PromptType.HIDDEN: + result = getpass(f"{text} (hidden): ") + except KeyboardInterrupt as e: + msg = "User cancelled the input." + raise ClanError(msg) from e log.info("Input received. Processing...") return result