From fd6bd23457ca70a150b768d0f7d916639e92f50c Mon Sep 17 00:00:00 2001 From: Chris Dent Date: Fri, 25 Jan 2019 13:38:38 +0000 Subject: [PATCH] Separate opt registration from command running In some scenarios, it is useful for the caller of upgradecheck to be responsible for parsing the conf (see [1]). Since registering command line options must come before parsing, we must separate registation from running the commands. This change enables that by making `register_cli_options` public and extracting a public `run` method. `main` continues to work as before by calling these new methods. [1] I2eea00343d1bbdf486985daeaf6707358b6a2708 Change-Id: I3d7e28d2432f5b04d4154793025786dc289b0cb2 --- oslo_upgradecheck/upgradecheck.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/oslo_upgradecheck/upgradecheck.py b/oslo_upgradecheck/upgradecheck.py index 3dab00c..2a7b850 100644 --- a/oslo_upgradecheck/upgradecheck.py +++ b/oslo_upgradecheck/upgradecheck.py @@ -141,7 +141,7 @@ class UpgradeCommands(object): return return_code -def _register_cli_options(conf, upgrade_command): +def register_cli_options(conf, upgrade_command): """Set up the command line options. Adds a subcommand to support 'upgrade check' on the command line. @@ -156,6 +156,16 @@ def _register_cli_options(conf, upgrade_command): conf.register_cli_opt(opt) +def run(conf): + """Run the requested command.""" + try: + return conf.command.action_fn() + except Exception: + print(_('Error:\n%s') % traceback.format_exc()) + # This is 255 so it's not confused with the upgrade check exit codes. + return 255 + + def main(conf, project, upgrade_command, argv=sys.argv[1:], default_config_files=None): @@ -177,7 +187,7 @@ def main(conf, project, upgrade_command, the search behavior in oslo.config. """ - _register_cli_options(conf, upgrade_command) + register_cli_options(conf, upgrade_command) conf( args=argv, @@ -185,9 +195,4 @@ def main(conf, project, upgrade_command, default_config_files=default_config_files, ) - try: - return conf.command.action_fn() - except Exception: - print(_('Error:\n%s') % traceback.format_exc()) - # This is 255 so it's not confused with the upgrade check exit codes. - return 255 + return run(conf)