summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPrasanna Kumar Kalever <prasanna.kalever@redhat.com>2020-04-06 19:33:20 +0530
committerPrasanna Kumar Kalever <prasanna.kalever@redhat.com>2020-04-06 23:22:59 +0530
commitd768df6942385bb1b3447b9950531b40f35ccfed (patch)
tree23a6ebc609687762dbf93a39f9401b1397b9e8a8
parentb60c74594abf6cf681ff8eaa348059c5348399e9 (diff)
downloadtargetcli-d768df6942385bb1b3447b9950531b40f35ccfed.tar.gz
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>
-rwxr-xr-xscripts/targetcli52
1 files changed, 45 insertions, 7 deletions
diff --git a/scripts/targetcli b/scripts/targetcli
index a0eaf7a..1c2046f 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)
command = six.moves.input()
if command.lower() == "exit":
real_exit=True
@@ -201,12 +234,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)