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