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

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