Skip to content

Commit 12415bc

Browse files
author
Prasanna Kumar Kalever
committed
daemonized-mode: add interactive shell support
$ targetcli --help Usage: /usr/bin/targetcli [--version|--help|CMD|--disable-daemon|--interactive] [...] -i, --interactive Enter interactive shell in daemonized mode See man page for more information. $ targetcli -i targetcli shell version 2.1.51 Entering targetcli interactive mode for daemonized approach. Type 'exit' to quit. /> pwd / /> cd /iscsi /> pwd /iscsi /> exit Fixes: #160 Signed-off-by: Prasanna Kumar Kalever <[email protected]>
1 parent 0157f74 commit 12415bc

File tree

2 files changed

+62
-22
lines changed

2 files changed

+62
-22
lines changed

scripts/targetcli

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,13 @@ class TargetCLI(ConfigShell):
6767
}
6868

6969
def usage():
70-
print("Usage: %s [--version|--help|CMD|--disable-daemon]" % sys.argv[0], file=err)
70+
print("Usage: %s [--version|--help|CMD|--disable-daemon|--interactive]" % sys.argv[0], file=err)
7171
print(" --version\t\tPrint version", file=err)
7272
print(" --help\t\tPrint this information", file=err)
7373
print(" CMD\t\t\tRun targetcli shell command and exit", file=err)
7474
print(" <nothing>\t\tEnter configuration shell", file=err)
7575
print(" --disable-daemon\tTurn-off the global auto use daemon flag", file=err)
76+
print(" -i, --interactive\tEnter interactive shell in daemonized mode", file=err)
7677
print("See man page for more information.", file=err)
7778
sys.exit(-1)
7879

@@ -159,9 +160,8 @@ def call_daemon(shell, req):
159160

160161
sock.send(b'-END@OF@DATA-')
161162
sock.close()
162-
sys.exit(0)
163163

164-
def get_arguments(shell):
164+
def switch_to_daemon(shell, interactive):
165165
readline.set_completer(completer)
166166
readline.set_completer_delims('')
167167

@@ -170,29 +170,43 @@ def get_arguments(shell):
170170
else:
171171
readline.parse_and_bind("tab: complete")
172172

173-
if len(sys.argv) > 1:
173+
if len(sys.argv) > 1 and not interactive:
174174
command = " ".join(sys.argv[1:])
175+
call_daemon(shell, command.encode())
176+
sys.exit(0)
177+
178+
if interactive:
179+
shell.con.display("targetcli shell version %s\n"
180+
"Entering targetcli interactive mode for daemonized approach.\n"
181+
"Type 'exit' to quit.\n"
182+
% targetcli_version)
175183
else:
176-
inputs = []
177184
shell.con.display("targetcli shell version %s\n"
178-
"Entering targetcli batch mode for daemonized approach.\n"
179-
"Enter multiple commands separated by newline and "
180-
"type 'exit' to run them all in one go.\n"
181-
% targetcli_version)
182-
while True:
183-
shell.con.raw_write("/> ")
184-
command = six.moves.input()
185-
if command.lower() == "exit":
186-
break
185+
"Entering targetcli batch mode for daemonized approach.\n"
186+
"Enter multiple commands separated by newline and "
187+
"type 'exit' to run them all in one go.\n"
188+
% targetcli_version)
189+
190+
inputs = []
191+
while True:
192+
real_exit=False
193+
shell.con.raw_write("/> ")
194+
command = six.moves.input()
195+
if command.lower() == "exit":
196+
real_exit=True
197+
if not interactive:
187198
inputs.append(command)
188-
command = '%'.join(inputs) # delimit multiple commands with '%'
189-
190-
if not command:
191-
sys.exit(1)
192-
193-
usage_version(command);
199+
command = '%'.join(inputs) # delimit multiple commands with '%'
200+
if real_exit:
201+
if interactive:
202+
break
203+
else:
204+
call_daemon(shell, command.encode())
205+
sys.exit(0)
206+
if interactive:
207+
call_daemon(shell, command.encode())
194208

195-
return command
209+
sys.exit(0)
196210

197211
def main():
198212
'''
@@ -219,13 +233,16 @@ def main():
219233
use_daemon = True
220234

221235
disable_daemon=False
236+
interactive_mode=False
222237
if len(sys.argv) > 1:
223238
usage_version(sys.argv[1])
224239
if sys.argv[1] in ("disable-daemon", "--disable-daemon"):
225240
disable_daemon=True
241+
elif sys.argv[1] in ("interactive", "--interactive", "-i"):
242+
interactive_mode=True
226243

227244
if use_daemon and not disable_daemon:
228-
call_daemon(shell, get_arguments(shell).encode())
245+
switch_to_daemon(shell, interactive_mode)
229246
# does not return
230247

231248
try:

targetclid.8

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,29 @@ Enter multiple commands separated by newline and type 'exit' to run them all in
4949
/> exit
5050
.br
5151
.TP
52+
You can also use interactive mode,
53+
.br
54+
$ targetcli -i
55+
.br
56+
targetcli shell version 2.1.51
57+
.br
58+
Entering targetcli interactive mode for daemonized approach.
59+
.br
60+
Type 'exit' to quit.
61+
.br
62+
/> pwd
63+
.br
64+
/
65+
.br
66+
/> cd /iscsi
67+
.br
68+
/> pwd
69+
.br
70+
/iscsi
71+
.br
72+
/> exit
73+
.br
74+
.TP
5275
You can set preference to stop using daemonized mode even when the daemon is not running,
5376
.br
5477
$ targetcli --disable-daemon

0 commit comments

Comments
 (0)