Blame SOURCES/0009-daemon-interactive-show-path-on-prompt.patch

a135b2
From 853bbc4f85427206bb27e5d63727d8b9fbe10972 Mon Sep 17 00:00:00 2001
a135b2
From: Prasanna Kumar Kalever <prasanna.kalever@redhat.com>
a135b2
Date: Mon, 6 Apr 2020 19:33:20 +0530
a135b2
Subject: [PATCH 3/3] daemon-interactive: show path on prompt
a135b2
a135b2
$ targetcli
a135b2
targetcli shell version 2.1.51
a135b2
Entering targetcli interactive mode for daemonized approach.
a135b2
Type 'exit' to quit.
a135b2
a135b2
/> pwd
a135b2
/
a135b2
/> cd /iscsi
a135b2
/iscsi> ls
a135b2
o- iscsi .......................................... [Targets: 0]
a135b2
/iscsi> exit
a135b2
a135b2
Signed-off-by: Prasanna Kumar Kalever <prasanna.kalever@redhat.com>
a135b2
---
a135b2
 scripts/targetcli | 52 ++++++++++++++++++++++++++++++++++++++++-------
a135b2
 1 file changed, 45 insertions(+), 7 deletions(-)
a135b2
a135b2
diff --git a/scripts/targetcli b/scripts/targetcli
a135b2
index 97f41eb..e1ec178 100755
a135b2
--- a/scripts/targetcli
a135b2
+++ b/scripts/targetcli
a135b2
@@ -122,7 +122,7 @@ def completer(text, state):
a135b2
     except IndexError:
a135b2
         return None
a135b2
 
a135b2
-def call_daemon(shell, req):
a135b2
+def call_daemon(shell, req, interactive):
a135b2
     try:
a135b2
         sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
a135b2
     except socket.error as err:
a135b2
@@ -140,9 +140,23 @@ def call_daemon(shell, req):
a135b2
                 "then run '#targetcli --disable-daemon'", 'red'))
a135b2
         sys.exit(1)
a135b2
 
a135b2
+    # Two cases where we want to get pwd:
a135b2
+    # 1. Before starting shell in interactive mode, needed for setting terminal
a135b2
+    # 2. And only in Interactive mode, having command 'cd'
a135b2
+    get_pwd = False
a135b2
+    if interactive:
a135b2
+        if not req:
a135b2
+            req = "pwd"
a135b2
+            get_pwd = True
a135b2
+        elif "cd " in req:
a135b2
+            req += "%pwd"
a135b2
+            get_pwd = True
a135b2
+    else:
a135b2
+        req = "cd /%" + req # Non-interactive modes always consider start at '/'
a135b2
+
a135b2
     try:
a135b2
         # send request
a135b2
-        sock.sendall(req)
a135b2
+        sock.sendall(req.encode())
a135b2
     except socket.error as err:
a135b2
         shell.con.display(shell.con.render_text(err, 'red'))
a135b2
         sys.exit(1)
a135b2
@@ -153,15 +167,30 @@ def call_daemon(shell, req):
a135b2
     amount_received = 0
a135b2
 
a135b2
     # get the actual data in chunks
a135b2
+    output = ""
a135b2
+    path = ""
a135b2
     while amount_received < amount_expected:
a135b2
         data = sock.recv(1024)
a135b2
         data = data.decode()
a135b2
         amount_received += len(data)
a135b2
-        print(data, end ="")
a135b2
+        output += data
a135b2
+
a135b2
+    if get_pwd:
a135b2
+        output_split = output.splitlines()
a135b2
+        lines = len(output_split)
a135b2
+        for i in range(0, lines):
a135b2
+            if i == lines-1:
a135b2
+                path = str(output_split[i])
a135b2
+            else:
a135b2
+                print(str(output_split[i]), end ="\n")
a135b2
+    else:
a135b2
+        print(output, end ="")
a135b2
 
a135b2
     sock.send(b'-END@OF@DATA-')
a135b2
     sock.close()
a135b2
 
a135b2
+    return path
a135b2
+
a135b2
 def switch_to_daemon(shell, interactive):
a135b2
     readline.set_completer(completer)
a135b2
     readline.set_completer_delims('')
a135b2
@@ -173,7 +202,7 @@ def switch_to_daemon(shell, interactive):
a135b2
 
a135b2
     if len(sys.argv) > 1:
a135b2
         command = " ".join(sys.argv[1:])
a135b2
-        call_daemon(shell, command.encode())
a135b2
+        call_daemon(shell, command, False)
a135b2
         sys.exit(0)
a135b2
 
a135b2
     if interactive:
a135b2
@@ -188,10 +217,14 @@ def switch_to_daemon(shell, interactive):
a135b2
                           "type 'exit' to run them all in one go.\n"
a135b2
                           % targetcli_version)
a135b2
 
a135b2
+    prompt_path = "/"
a135b2
+    if interactive:
a135b2
+        prompt_path = call_daemon(shell, None, interactive) # get the initial path
a135b2
+
a135b2
     inputs = []
a135b2
     real_exit=False
a135b2
     while True:
a135b2
-        shell.con.raw_write("/> ")
a135b2
+        shell.con.raw_write("%s> " %prompt_path)
a135b2
         pos = shell.con.get_cursor_xy()
a135b2
         shell.con.set_cursor_xy(pos[0], pos[1])
a135b2
         command = six.moves.input()
a135b2
@@ -203,12 +236,17 @@ def switch_to_daemon(shell, interactive):
a135b2
             inputs.append(command)
a135b2
             if real_exit:
a135b2
                 command = '%'.join(inputs)  # delimit multiple commands with '%'
a135b2
-                call_daemon(shell, command.encode())
a135b2
+                call_daemon(shell, command, interactive)
a135b2
                 break
a135b2
         else:
a135b2
             if real_exit:
a135b2
                 break
a135b2
-            call_daemon(shell, command.encode())
a135b2
+            path = call_daemon(shell, command, interactive)
a135b2
+            if path:
a135b2
+                if path[0] == "/":
a135b2
+                    prompt_path = path
a135b2
+                else:
a135b2
+                    print(path)  # Error No Path ...
a135b2
 
a135b2
     sys.exit(0)
a135b2
 
a135b2
-- 
a135b2
2.21.0
a135b2