b38368
From d22e7953295f878950ca5be976d89bf9af8d36b1 Mon Sep 17 00:00:00 2001
b38368
From: Martin Babinsky <mbabinsk@redhat.com>
b38368
Date: Thu, 22 Jun 2017 15:02:25 +0200
b38368
Subject: [PATCH] delegate formatting of compound Bash statements to dedicated
b38368
 classes
b38368
b38368
this simplifies handling compound statements using _AdviceOutput class.
b38368
The necessary statements are exposed as context managers and API for
b38368
most common constructs is provided.
b38368
b38368
https://pagure.io/freeipa/issue/7036
b38368
b38368
Reviewed-By: Florence Blanc-Renaud <frenaud@redhat.com>
b38368
Reviewed-By: Alexander Bokovoy <abokovoy@redhat.com>
b38368
---
b38368
 ipaserver/advise/base.py | 48 ++++++++++++++++++++++++++++++++++--------------
b38368
 1 file changed, 34 insertions(+), 14 deletions(-)
b38368
b38368
diff --git a/ipaserver/advise/base.py b/ipaserver/advise/base.py
b38368
index 940d87ed4c1804326a46e2866381364e6f4f3f3e..581478fb75bc4f50b6bffe2e4cf9b51de46fa095 100644
b38368
--- a/ipaserver/advise/base.py
b38368
+++ b/ipaserver/advise/base.py
b38368
@@ -286,33 +286,53 @@ class _AdviceOutput(object):
b38368
         )
b38368
 
b38368
     def exit_on_predicate(self, predicate, error_message_lines):
b38368
-        commands_to_run = [
b38368
-            self._format_error(error_message_line)
b38368
-            for error_message_line in error_message_lines]
b38368
+        with self.unbranched_if(predicate):
b38368
+            for error_message_line in error_message_lines:
b38368
+                self.command(self._format_error(error_message_line))
b38368
 
b38368
-        commands_to_run.append('exit 1')
b38368
-        self.commands_on_predicate(
b38368
-            predicate,
b38368
-            commands_to_run)
b38368
+            self.command('exit 1')
b38368
+
b38368
+    @contextmanager
b38368
+    def unbranched_if(self, predicate):
b38368
+        with self._compound_statement(UnbranchedIfStatement, predicate):
b38368
+            yield
b38368
+
b38368
+    @contextmanager
b38368
+    def _compound_statement(self, statement_cls, *args):
b38368
+        with statement_cls(self, *args):
b38368
+            yield
b38368
 
b38368
     def commands_on_predicate(self, predicate, commands_to_run_when_true,
b38368
                               commands_to_run_when_false=None):
b38368
-        if_command = 'if {}'.format(predicate)
b38368
-        self.command(if_command)
b38368
-        self.command('then')
b38368
+        if commands_to_run_when_false is not None:
b38368
+            if_statement = self.if_branch
b38368
+        else:
b38368
+            if_statement = self.unbranched_if
b38368
 
b38368
-        with self.indented_block():
b38368
+        with if_statement(predicate):
b38368
             for command_to_run_when_true in commands_to_run_when_true:
b38368
                 self.command(
b38368
                     command_to_run_when_true)
b38368
 
b38368
         if commands_to_run_when_false is not None:
b38368
-            self.command("else")
b38368
-            with self.indented_block():
b38368
+            with self.else_branch():
b38368
                 for command_to_run_when_false in commands_to_run_when_false:
b38368
                     self.command(command_to_run_when_false)
b38368
 
b38368
-        self.command('fi')
b38368
+    @contextmanager
b38368
+    def if_branch(self, predicate):
b38368
+        with self._compound_statement(IfBranch, predicate):
b38368
+            yield
b38368
+
b38368
+    @contextmanager
b38368
+    def else_branch(self):
b38368
+        with self._compound_statement(ElseBranch):
b38368
+            yield
b38368
+
b38368
+    @contextmanager
b38368
+    def else_if_branch(self, predicate):
b38368
+        with self._compound_statement(ElseIfBranch, predicate):
b38368
+            yield
b38368
 
b38368
 
b38368
 class Advice(Plugin):
b38368
-- 
b38368
2.9.4
b38368