Skip to content

Commit 2315147

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 varibale use_batch_mode for switching between batch and interactive modes. $ targetcli set global use_batch_mode=true Parameter 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 18ce78b commit 2315147

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+
'use_batch_mode': False,
6768
}
6869

6970
def usage():
@@ -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

@@ -172,27 +172,40 @@ def get_arguments(shell):
172172

173173
if len(sys.argv) > 1:
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+
real_exit=False
192+
while True:
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+
if real_exit:
200+
command = '%'.join(inputs) # delimit multiple commands with '%'
201+
call_daemon(shell, command.encode())
202+
break
203+
else:
204+
if real_exit:
205+
break
206+
call_daemon(shell, command.encode())
194207

195-
return command
208+
sys.exit(0)
196209

197210
def main():
198211
'''
@@ -224,8 +237,12 @@ def main():
224237
if sys.argv[1] in ("disable-daemon", "--disable-daemon"):
225238
disable_daemon=True
226239

240+
interactive_mode = True
241+
if shell.prefs['use_batch_mode']:
242+
interactive_mode = False
243+
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:

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', '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 use_batch_mode=true
59+
.br
60+
Parameter 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)