From d11abc928fb7d083b812f1532f6d6494e08b7eee Mon Sep 17 00:00:00 2001 From: "Joshua M. Boniface" Date: Thu, 17 Aug 2023 22:06:27 -0400 Subject: [PATCH] Revamp behaviour of VM "--restart" options Previously, either "--restart" was specified or a prompt was given, with the prompt being ignored with "--unsafe" in favour of a reboot. This failed to provide an explicit way to prevent VM restarts with these commands, which might be desired in some non-interactive situations, and the interaction of "--unsafe" with this option was an undesired bug. This is now a complete binary flag with --restart and --no-restart versions, while still defaulting to a prompt if neither is specified. This allows full non-interactive control of this option. --- client-cli/pvc/cli/cli.py | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/client-cli/pvc/cli/cli.py b/client-cli/pvc/cli/cli.py index 47351d8d..8ae65712 100644 --- a/client-cli/pvc/cli/cli.py +++ b/client-cli/pvc/cli/cli.py @@ -166,32 +166,24 @@ def connection_req(function): def restart_opt(function): """ Click Option Decorator: - Wraps a Click command which requires confirm_flag or unsafe option or asks for VM restart confirmation + Wraps a Click command which requires a VM domain restart, to provide options for/against restart or prompt """ @click.option( - "-r", - "--restart", + "-r/-R", + "--restart/--no-restart", "restart_flag", is_flag=True, - default=False, - help="Immediately restart VM to apply changes.", + default=None, + show_default=False, + help="Immediately restart VM to apply changes or do not restart VM, or prompt if unspecified.", ) @wraps(function) def confirm_action(*args, **kwargs): - confirm_action = True - if "restart_flag" in kwargs: - if not kwargs.get("restart_flag", False): - if not CLI_CONFIG.get("unsafe", False): - confirm_action = True - else: - confirm_action = False - else: - confirm_action = False - else: - confirm_action = False + restart_state = kwargs.get("restart_flag", None) - if confirm_action: + if restart_state is None: + # Neither "--restart" or "--no-restart" was passed: prompt for restart or restart if "--unsafe" try: click.confirm( f"Restart VM {kwargs.get('domain')} to apply changes", @@ -202,6 +194,12 @@ def restart_opt(function): except Exception: echo(CLI_CONFIG, "Changes will be applied on next VM start/restart.") kwargs["restart_flag"] = False + elif restart_state is True: + # "--restart" was passed: allow restart without confirming + kwargs["restart_flag"] = True + elif restart_state is False: + # "--no-restart" was passed: skip confirming and skip restart + kwargs["restart_flag"] = False return function(*args, **kwargs) @@ -5550,7 +5548,7 @@ def cli_connection_detail( envvar="PVC_UNSAFE", is_flag=True, default=False, - help='Allow unsafe operations without confirmation/"--yes" argument.', + help='Perform unsafe operations without confirmation/"--yes" argument.', ) @click.option( "--colour",