Extend the advice printing code by some useful abstractions

The advise printing code was augmented by methods that simplify
generating bash snippets that report errors or failed commands.

https://pagure.io/freeipa/issue/6982

Reviewed-By: Alexander Bokovoy <abokovoy@redhat.com>
Reviewed-By: Florence Blanc-Renaud <frenaud@redhat.com>
This commit is contained in:
Martin Babinsky
2017-06-05 16:59:25 +02:00
parent d665224a85
commit 0569c02f17

View File

@@ -94,8 +94,67 @@ class _AdviceOutput(object):
if self.options.verbose: if self.options.verbose:
self.comment('DEBUG: ' + line) self.comment('DEBUG: ' + line)
def command(self, line): def command(self, line, indent_spaces=0):
self.content.append(line) self.content.append(
'{}{}'.format(self._format_indent(indent_spaces), line))
def _format_indent(self, num_spaces):
return ' ' * num_spaces
def echo_error(self, error_message, indent_spaces=0):
self.command(
self._format_error(error_message), indent_spaces=indent_spaces)
def _format_error(self, error_message):
return 'echo "{}" >&2'.format(error_message)
def exit_on_failed_command(self, command_to_run,
error_message_lines, indent_spaces=0):
self.command(command_to_run, indent_spaces=indent_spaces)
self.exit_on_predicate(
'[ "$?" -ne "0" ]',
error_message_lines,
indent_spaces=indent_spaces)
def exit_on_nonroot_euid(self):
self.exit_on_predicate(
'[ "$(id -u)" -ne "0" ]',
["This script has to be run as root user"]
)
def exit_on_predicate(self, predicate, error_message_lines,
indent_spaces=0):
commands_to_run = [
self._format_error(error_message_line)
for error_message_line in error_message_lines]
commands_to_run.append('exit 1')
self.commands_on_predicate(
predicate,
commands_to_run,
indent_spaces=indent_spaces)
def commands_on_predicate(self, predicate, commands_to_run_when_true,
commands_to_run_when_false=None,
indent_spaces=0):
if_command = 'if {}'.format(predicate)
self.command(if_command, indent_spaces=indent_spaces)
self.command('then', indent_spaces=indent_spaces)
indented_block_spaces = indent_spaces + 2
for command_to_run_when_true in commands_to_run_when_true:
self.command(
command_to_run_when_true, indent_spaces=indented_block_spaces)
if commands_to_run_when_false is not None:
self.command("else", indent_spaces=indent_spaces)
for command_to_run_when_false in commands_to_run_when_false:
self.command(
command_to_run_when_false,
indent_spaces=indented_block_spaces)
self.command('fi', indent_spaces=indent_spaces)
class Advice(Plugin): class Advice(Plugin):