Skip to content

Commit

Permalink
Add force flag to generate plan and upgrate non-empty hypervisors
Browse files Browse the repository at this point in the history
- introduction of "--force" that can be used on "plan" and "upgrade"
- usage of argparse.SUPPRESS to not overwrite the parent parser value
  [0]
- more unit tests changing the sequence of subcommands to show that
  overwriting it's not happening anymore.

[0] https://stackoverflow.com/questions/62904585/support-global-arguments-before-or-after-sub-command-in-argparse
  • Loading branch information
gabrielcocenza committed Jan 18, 2024
1 parent 8add88f commit db6c650
Show file tree
Hide file tree
Showing 2 changed files with 282 additions and 3 deletions.
23 changes: 20 additions & 3 deletions cou/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,22 @@ def add_argument(self, action: argparse.Action) -> None:
def get_subcommand_common_opts_parser() -> argparse.ArgumentParser:
"""Create a shared parser for options specific to subcommands.
Using SUPPRESS for the subparser default keeps it from overwriting the parent parser value.
A SUPPRESS default is not inserted into the namespace at the start of parsing. A value is
written only if the user used that argument.
Without SUPPRESS a command like: "cou upgrade --force data-plane" wouldn't force the data-plan
to upgrade non-empty hypervisors, because the "child" argument "data-plane" would overwrite
with False.
:return: a parser groups options commonly shared by subcommands
:rtype: argparse.ArgumentParser
"""
# Define common options for subcommands and their children commands
subcommand_common_opts_parser = argparse.ArgumentParser(add_help=False)
subcommand_common_opts_parser.add_argument(
"--model",
default=None,
default=argparse.SUPPRESS,
dest="model_name",
type=str,
help="Set the model to operate on.\nIf not set, the currently active Juju model will "
Expand All @@ -92,15 +100,22 @@ def get_subcommand_common_opts_parser() -> argparse.ArgumentParser:
help="Include database backup step before cloud upgrade.\n"
"Default to enabling database backup.",
action=argparse.BooleanOptionalAction,
default=True,
default=argparse.SUPPRESS,
)
subcommand_common_opts_parser.add_argument(
"--force",
action="store_true",
dest="force",
help="Force the plan/upgrade of non-empty hypervisors.",
default=argparse.SUPPRESS,
)

# quiet and verbose options are mutually exclusive
group = subcommand_common_opts_parser.add_mutually_exclusive_group()
group.add_argument(
"--verbose",
"-v",
default=0,
default=argparse.SUPPRESS,
action="count",
dest="verbosity",
help="Increase logging verbosity in STDOUT. Multiple 'v's yield progressively "
Expand All @@ -115,6 +130,7 @@ def get_subcommand_common_opts_parser() -> argparse.ArgumentParser:
action="store_true",
dest="quiet",
help="Disable output in STDOUT.",
default=argparse.SUPPRESS,
)

return subcommand_common_opts_parser
Expand Down Expand Up @@ -333,6 +349,7 @@ class CLIargs:
verbosity: int = 0
backup: bool = True
quiet: bool = False
force: bool = False
auto_approve: bool = False
model_name: Optional[str] = None
upgrade_group: Optional[str] = None
Expand Down
Loading

0 comments on commit db6c650

Please sign in to comment.