Skip to content

Commit 853bbc4

Browse files
author
Prasanna Kumar Kalever
committed
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 <[email protected]>
1 parent 3cfd45b commit 853bbc4

File tree

1 file changed

+45
-7
lines changed

1 file changed

+45
-7
lines changed

scripts/targetcli

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def completer(text, state):
122122
except IndexError:
123123
return None
124124

125-
def call_daemon(shell, req):
125+
def call_daemon(shell, req, interactive):
126126
try:
127127
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
128128
except socket.error as err:
@@ -140,9 +140,23 @@ def call_daemon(shell, req):
140140
"then run '#targetcli --disable-daemon'", 'red'))
141141
sys.exit(1)
142142

143+
# Two cases where we want to get pwd:
144+
# 1. Before starting shell in interactive mode, needed for setting terminal
145+
# 2. And only in Interactive mode, having command 'cd'
146+
get_pwd = False
147+
if interactive:
148+
if not req:
149+
req = "pwd"
150+
get_pwd = True
151+
elif "cd " in req:
152+
req += "%pwd"
153+
get_pwd = True
154+
else:
155+
req = "cd /%" + req # Non-interactive modes always consider start at '/'
156+
143157
try:
144158
# send request
145-
sock.sendall(req)
159+
sock.sendall(req.encode())
146160
except socket.error as err:
147161
shell.con.display(shell.con.render_text(err, 'red'))
148162
sys.exit(1)
@@ -153,15 +167,30 @@ def call_daemon(shell, req):
153167
amount_received = 0
154168

155169
# get the actual data in chunks
170+
output = ""
171+
path = ""
156172
while amount_received < amount_expected:
157173
data = sock.recv(1024)
158174
data = data.decode()
159175
amount_received += len(data)
160-
print(data, end ="")
176+
output += data
177+
178+
if get_pwd:
179+
output_split = output.splitlines()
180+
lines = len(output_split)
181+
for i in range(0, lines):
182+
if i == lines-1:
183+
path = str(output_split[i])
184+
else:
185+
print(str(output_split[i]), end ="\n")
186+
else:
187+
print(output, end ="")
161188

162189
sock.send(b'-END@OF@DATA-')
163190
sock.close()
164191

192+
return path
193+
165194
def switch_to_daemon(shell, interactive):
166195
readline.set_completer(completer)
167196
readline.set_completer_delims('')
@@ -173,7 +202,7 @@ def switch_to_daemon(shell, interactive):
173202

174203
if len(sys.argv) > 1:
175204
command = " ".join(sys.argv[1:])
176-
call_daemon(shell, command.encode())
205+
call_daemon(shell, command, False)
177206
sys.exit(0)
178207

179208
if interactive:
@@ -188,10 +217,14 @@ def switch_to_daemon(shell, interactive):
188217
"type 'exit' to run them all in one go.\n"
189218
% targetcli_version)
190219

220+
prompt_path = "/"
221+
if interactive:
222+
prompt_path = call_daemon(shell, None, interactive) # get the initial path
223+
191224
inputs = []
192225
real_exit=False
193226
while True:
194-
shell.con.raw_write("/> ")
227+
shell.con.raw_write("%s> " %prompt_path)
195228
pos = shell.con.get_cursor_xy()
196229
shell.con.set_cursor_xy(pos[0], pos[1])
197230
command = six.moves.input()
@@ -203,12 +236,17 @@ def switch_to_daemon(shell, interactive):
203236
inputs.append(command)
204237
if real_exit:
205238
command = '%'.join(inputs) # delimit multiple commands with '%'
206-
call_daemon(shell, command.encode())
239+
call_daemon(shell, command, interactive)
207240
break
208241
else:
209242
if real_exit:
210243
break
211-
call_daemon(shell, command.encode())
244+
path = call_daemon(shell, command, interactive)
245+
if path:
246+
if path[0] == "/":
247+
prompt_path = path
248+
else:
249+
print(path) # Error No Path ...
212250

213251
sys.exit(0)
214252

0 commit comments

Comments
 (0)