fix(template): replace click.confirm with input() to avoid ^M on Linux terminals

click.confirm() doesn't strip \r — "y\r" doesn't match "y" and the prompt
loops or fails. Replace with raw input().strip().rstrip('\r').lower() which
handles both \n and \r\n line endings.

Also add --recreate-snapshot flag to skip the interactive prompt entirely
(useful for scripted re-provisioning).

Update snapshot_exists_skip/recreate tests to monkeypatch builtins.input
instead of using CliRunner input=.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-24 02:02:22 +02:00
parent 483dd51a3b
commit 258ef3a7e7
2 changed files with 43 additions and 4 deletions
+9 -1
View File
@@ -366,6 +366,8 @@ def _run_with_heartbeat(
help="Store build-user credentials in the system keyring after provisioning.")
@click.option("--credential-target", default="BuildVMGuest", show_default=True,
help="Keyring target name for the stored credential.")
@click.option("--recreate-snapshot", is_flag=True,
help="Delete and recreate the snapshot if it already exists (no prompt).")
def template_prepare_win(
vmx: str,
guest_ip: str,
@@ -382,6 +384,7 @@ def template_prepare_win(
auth: str,
store_credential: bool,
credential_target: str,
recreate_snapshot: bool,
) -> None:
"""Provision a Windows template VM via pypsrp (Linux-host compatible).
@@ -665,7 +668,12 @@ Register-ScheduledTask -TaskName 'CI-StaticIp' `
existing = backend.list_snapshots(handle)
if snapshot in existing:
click.echo(f"[prepare-win] Snapshot '{snapshot}' already exists.")
if not click.confirm(f"Delete and recreate '{snapshot}'?", default=False):
if recreate_snapshot:
do_recreate = True
else:
raw = input(f"[prepare-win] Delete and recreate '{snapshot}'? [y/N] ")
do_recreate = raw.strip().rstrip("\r").lower() in ("y", "yes")
if not do_recreate:
click.echo("[prepare-win] Keeping existing snapshot. Provisioning complete.")
return
click.echo(f"[prepare-win] Deleting snapshot '{snapshot}' ...")