Skip to content

Commit 3cdd4ec

Browse files
author
Prasanna Kumar Kalever
committed
daemonized-mode: add interactive shell support
set interactive mode as default to make it appear similar to cli approach, $ targetcli targetcli shell version 2.1.51 Entering targetcli interactive mode for daemonized approach. Type 'exit' to quit. /> pwd / /> cd /iscsi /> pwd /iscsi /> exit Here we introduce a new global option daemon_use_batch_mode for switching between batch and interactive modes. $ targetcli set global daemon_use_batch_mode=true Parameter daemon_use_batch_mode is now 'true'. $ targetcli targetcli shell version 2.1.51 Entering targetcli batch mode for daemonized approach. Enter multiple commands separated by newline and type 'exit' to run them all in one go. /> pwd /> cd /iscsi /> pwd / /iscsi Fixes: #160 Signed-off-by: Prasanna Kumar Kalever <[email protected]>
1 parent e09fa8c commit 3cdd4ec

File tree

3 files changed

+69
-22
lines changed

3 files changed

+69
-22
lines changed

scripts/targetcli

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class TargetCLI(ConfigShell):
6464
'max_backup_files': '10',
6565
'auto_add_default_portal': True,
6666
'auto_use_daemon': False,
67+
'daemon_use_batch_mode': False,
6768
}
6869

6970
def usage():
@@ -160,9 +161,8 @@ def call_daemon(shell, req):
160161

161162
sock.send(b'-END@OF@DATA-')
162163
sock.close()
163-
sys.exit(0)
164164

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

@@ -173,27 +173,40 @@ def get_arguments(shell):
173173

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

196-
return command
209+
sys.exit(0)
197210

198211
def main():
199212
'''
@@ -225,8 +238,12 @@ def main():
225238
if sys.argv[1] in ("disable-daemon", "--disable-daemon"):
226239
disable_daemon=True
227240

241+
interactive_mode = True
242+
if shell.prefs['daemon_use_batch_mode']:
243+
interactive_mode = False
244+
228245
if use_daemon and not disable_daemon:
229-
call_daemon(shell, get_arguments(shell).encode())
246+
switch_to_daemon(shell, interactive_mode)
230247
# does not return
231248

232249
try:

targetcli/ui_node.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ def __init__(self, name, parent=None, shell=None):
5252
self.define_config_group_param(
5353
'global', 'auto_use_daemon', 'bool',
5454
'If true, commands will be sent to targetclid.')
55+
self.define_config_group_param(
56+
'global', 'daemon_use_batch_mode', 'bool',
57+
'If true, use batch mode for daemonized approach.')
5558

5659
def assert_root(self):
5760
'''

targetclid.8

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,38 @@ $ targetcli set global auto_use_daemon=true
3030
.br
3131
$ targetcli ls
3232
.TP
33-
You can use batch mode for sending multiple commands in one go,
33+
You can use interactive mode,
3434
.br
3535
$ targetcli <hit-enter>
3636
.br
37-
targetcli shell version 2.1.50
37+
targetcli shell version 2.1.51
38+
.br
39+
Entering targetcli interactive mode for daemonized approach.
40+
.br
41+
Type 'exit' to quit.
42+
.br
43+
/> pwd
44+
.br
45+
/
46+
.br
47+
/> cd /iscsi
48+
.br
49+
/> pwd
50+
.br
51+
/iscsi
52+
.br
53+
/> exit
54+
.br
55+
.TP
56+
You can also use batch mode for sending multiple commands in one go,
57+
.br
58+
$ targetcli set global daemon_use_batch_mode=true
59+
.br
60+
Parameter daemon_use_batch_mode is now 'true'.
61+
.br
62+
$ targetcli <hit-enter>
63+
.br
64+
targetcli shell version 2.1.51
3865
.br
3966
Entering targetcli batch mode for daemonized approach.
4067
.br

0 commit comments

Comments
 (0)