Ravi Nori has uploaded a new change for review.

Change subject: cli: add the option to retrieve partial history using 
CLI(#957499)
......................................................................

cli: add the option to retrieve partial history using CLI(#957499)

add the option to retrieve partial history
( e.g history --last 3 ) using CLI instead
of retrieving all history .

this patch adds
history --last n
and
history --first n
to retrive the last or first n history elements

Change-Id: I2321ccc7e3cce2521e0b86f67de2797a8a2b4276
Bug-Url: https://bugzilla.redhat.com/957499
Signed-off-by: Ravi Nori <[email protected]>
---
M src/ovirtcli/command/history.py
1 file changed, 61 insertions(+), 16 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine-cli refs/changes/72/20272/1

diff --git a/src/ovirtcli/command/history.py b/src/ovirtcli/command/history.py
index 7e3c51b..a75904b 100644
--- a/src/ovirtcli/command/history.py
+++ b/src/ovirtcli/command/history.py
@@ -48,26 +48,71 @@
 
     def execute(self):
         args = self.arguments
-        context = self.context
-        hformat = '[%d] %s'
+        options = self.options
 
         if len(args) > 0:
-            indx = args[0]
+            self.printHistoryAtIndex(args)
+        else:
+            if len(options) > 0:
+               self.printPartialHistory(options)
+            else:
+               self.printAllHistory()
+
+    def printAllHistory(self):
+        context = self.context
+        i = 0
+        history = context.history.list()
+        if history:
+            self.write('')
+            for item in history:
+                self.printHistoryItemAtIndex(i + 1, item)
+                i += 1
+            self.write('')
+
+    def printHistoryAtIndex(self, args):
+        context = self.context
+        indx = args[0]
+        history = context.history.list()
+        if history:
             try:
                 slide = int(indx)
                 h_item = context.history.get(slide)
-                if h_item:
-                    self.write('')
-                    self.write(hformat % (slide , str(h_item)))
-                    self.write('')
+                self.printHistoryItemAtIndex(slide, h_item)
             except Exception, e:
                 self.error(str(e))
-        else:
-            i = 0
-            history = context.history.list()
-            if history:
-                self.write('')
-                for item in history:
-                    self.write(hformat % (i + 1 , str(item)))
-                    i += 1
-                self.write('')
+
+    def printHistoryItemAtIndex(self, slide, h_item):
+        hformat = '[%d] %s'
+        if h_item:
+            self.write(hformat % (slide , str(h_item)))
+
+    def printPartialHistory(self, options):
+        context = self.context
+
+        prop = ''
+        val = '';
+        for key in options.keys():
+            prop = key.replace('--', '')
+            val = options[key]
+
+        history = context.history.list()
+        if history:
+            history_len = len(history)
+            slide_len = int(val)
+            if prop == 'last':
+                self.printHistoryBetweenIndexes(max(0, history_len - 
slide_len), history_len)
+            elif prop == 'first':
+                self.printHistoryBetweenIndexes(0, min(slide_len, history_len))
+            else:
+                self.error('Unknown property ' + prop)
+
+    def printHistoryBetweenIndexes(self, from_index, to_index):
+        context = self.context
+        try:
+            self.write('')
+            for slide in range(from_index, to_index):
+                h_item = context.history.get(slide+1)
+                self.printHistoryItemAtIndex(slide+1, h_item)
+            self.write('')
+        except Exception, e:
+            self.error(str(e))


-- 
To view, visit http://gerrit.ovirt.org/20272
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2321ccc7e3cce2521e0b86f67de2797a8a2b4276
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine-cli
Gerrit-Branch: master
Gerrit-Owner: Ravi Nori <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to