Skip to content

Commit 9a31c48

Browse files
committed
cli: add default argument to command_opt
Implement a way to set default value for a specific leapp cli option and use it for --report-schema.
1 parent 66d6b86 commit 9a31c48

2 files changed

Lines changed: 11 additions & 15 deletions

File tree

leapp/cli/upgrade/__init__.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,6 @@ def process_whitelist_experimental(repositories, workflow, configuration, logger
183183
raise CommandError(msg)
184184

185185

186-
def process_report_schema(args, cfg):
187-
default_report_schema = cfg.get('report', 'schema')
188-
if args.report_schema and args.report_schema > default_report_schema:
189-
raise CommandError('--report-schema version can not be greater that the '
190-
'actual {} one.'.format(default_report_schema))
191-
return args.report_schema or default_report_schema
192-
193-
194186
@command('upgrade', help='Upgrade the current system to the next available major version.')
195187
@command_opt('resume', is_flag=True, help='Continue the last execution after it was stopped (e.g. after reboot)')
196188
@command_opt('reboot', is_flag=True, help='Automatically performs reboot when requested.')
@@ -201,7 +193,8 @@ def process_report_schema(args, cfg):
201193
' with Red Hat Subscription Manager')
202194
@command_opt('enablerepo', action='append', metavar='<repoid>',
203195
help='Enable specified repository. Can be used multiple times.')
204-
@command_opt('report-schema', help='Specify report schema version for leapp-report.json', choices=['1.0.0', '1.1.0'])
196+
@command_opt('report-schema', help='Specify report schema version for leapp-report.json', choices=['1.0.0', '1.1.0'],
197+
default=get_config().get('report', 'schema'))
205198
def upgrade(args):
206199
skip_phases_until = None
207200
context = str(uuid.uuid4())
@@ -210,7 +203,6 @@ def upgrade(args):
210203
configuration = prepare_configuration(args)
211204
answerfile_path = cfg.get('report', 'answerfile')
212205
userchoices_path = cfg.get('report', 'userchoices')
213-
report_schema = process_report_schema(args, cfg)
214206
if os.getuid():
215207
raise CommandError('This command has to be run under the root user.')
216208

@@ -253,7 +245,7 @@ def upgrade(args):
253245
workflow.save_answers(answerfile_path, userchoices_path)
254246
report_errors(workflow.errors)
255247
report_inhibitors(context)
256-
generate_report_files(context, report_schema)
248+
generate_report_files(context, args.report_schema)
257249
report_files = get_cfg_files('report', cfg)
258250
log_files = get_cfg_files('logs', cfg)
259251
report_info(report_files, log_files, answerfile_path, fail=workflow.failure)
@@ -270,7 +262,8 @@ def upgrade(args):
270262
' with Red Hat Subscription Manager')
271263
@command_opt('enablerepo', action='append', metavar='<repoid>',
272264
help='Enable specified repository. Can be used multiple times.')
273-
@command_opt('report-schema', help='Specify report schema version for leapp-report.json', choices=['1.0.0', '1.1.0'])
265+
@command_opt('report-schema', help='Specify report schema version for leapp-report.json', choices=['1.0.0', '1.1.0'],
266+
default=get_config().get('report', 'schema'))
274267
def preupgrade(args):
275268
context = str(uuid.uuid4())
276269
cfg = get_config()
@@ -279,7 +272,6 @@ def preupgrade(args):
279272
answerfile_path = cfg.get('report', 'answerfile')
280273
userchoices_path = cfg.get('report', 'userchoices')
281274

282-
report_schema = process_report_schema(args, cfg)
283275
if os.getuid():
284276
raise CommandError('This command has to be run under the root user.')
285277
e = Execution(context=context, kind='preupgrade', configuration=configuration)
@@ -303,7 +295,7 @@ def preupgrade(args):
303295

304296
logger.info("Answerfile will be created at %s", answerfile_path)
305297
workflow.save_answers(answerfile_path, userchoices_path)
306-
generate_report_files(context, report_schema)
298+
generate_report_files(context, args.report_schema)
307299
report_errors(workflow.errors)
308300
report_inhibitors(context)
309301
report_files = get_cfg_files('report', cfg)

leapp/utils/clicmd.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def _add_opt(self, *args, **kwargs):
193193

194194
def add_option(self, name, short_name='', help='', # noqa; pylint: disable=redefined-builtin
195195
is_flag=False, inherit=False, value_type=str, wrapped=None, action=None, metavar=None,
196-
choices=None):
196+
choices=None, default=None):
197197
"""
198198
Add an option
199199
@@ -217,6 +217,8 @@ def add_option(self, name, short_name='', help='', # noqa; pylint: disable=rede
217217
:type metavar: str
218218
:param choices: range of values that the argument is allowed to take
219219
:type choices: list
220+
:param choices: default value of the argument if nothing is specified
221+
:type choices: str
220222
:return: self
221223
"""
222224
name = name.lstrip('-')
@@ -237,6 +239,8 @@ def add_option(self, name, short_name='', help='', # noqa; pylint: disable=rede
237239
kwargs['metavar'] = metavar
238240
if choices:
239241
kwargs['choices'] = choices
242+
if default:
243+
kwargs['default'] = default
240244
self._add_opt(*names, help=help, # noqa; pylint: disable=redefined-builtin
241245
action=action, internal={'wrapped': wrapped, 'inherit': inherit}, **kwargs)
242246
return self

0 commit comments

Comments
 (0)