Blob Blame History Raw
From 15abdd481c5babd4128f3f0a9f0ac2f983bf1c89 Mon Sep 17 00:00:00 2001
From: Prasanna Kumar Kalever <prasanna.kalever@redhat.com>
Date: Fri, 3 Apr 2020 13:01:06 +0530
Subject: [PATCH 1/3] 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 <prasanna.kalever@redhat.com>
---
 scripts/targetcli    | 57 ++++++++++++++++++++++++++++----------------
 targetcli/ui_node.py |  3 +++
 targetclid.8         | 31 ++++++++++++++++++++++--
 3 files changed, 69 insertions(+), 22 deletions(-)

diff --git a/scripts/targetcli b/scripts/targetcli
index f11ece4..3496126 100755
--- a/scripts/targetcli
+++ b/scripts/targetcli
@@ -64,6 +64,7 @@ class TargetCLI(ConfigShell):
                      'max_backup_files': '10',
                      'auto_add_default_portal': True,
                      'auto_use_daemon': False,
+                     'daemon_use_batch_mode': False,
                     }
 
 def usage():
@@ -160,9 +161,8 @@ def call_daemon(shell, req):
 
     sock.send(b'-END@OF@DATA-')
     sock.close()
-    sys.exit(0)
 
-def get_arguments(shell):
+def switch_to_daemon(shell, interactive):
     readline.set_completer(completer)
     readline.set_completer_delims('')
 
@@ -173,27 +173,40 @@ def get_arguments(shell):
 
     if len(sys.argv) > 1:
         command = " ".join(sys.argv[1:])
+        call_daemon(shell, command.encode())
+        sys.exit(0)
+
+    if interactive:
+        shell.con.display("targetcli shell version %s\n"
+                          "Entering targetcli interactive mode for daemonized approach.\n"
+                          "Type 'exit' to quit.\n"
+                          % targetcli_version)
     else:
-        inputs = []
         shell.con.display("targetcli shell version %s\n"
-                        "Entering targetcli batch mode for daemonized approach.\n"
-                        "Enter multiple commands separated by newline and "
-                        "type 'exit' to run them all in one go.\n"
-                        % targetcli_version)
-        while True:
-            shell.con.raw_write("/> ")
-            command = six.moves.input()
-            if command.lower() == "exit":
-                break
+                          "Entering targetcli batch mode for daemonized approach.\n"
+                          "Enter multiple commands separated by newline and "
+                          "type 'exit' to run them all in one go.\n"
+                          % targetcli_version)
+
+    inputs = []
+    real_exit=False
+    while True:
+        shell.con.raw_write("/> ")
+        command = six.moves.input()
+        if command.lower() == "exit":
+            real_exit=True
+        if not interactive:
             inputs.append(command)
-        command = '%'.join(inputs)  # delimit multiple commands with '%'
-
-    if not command:
-        sys.exit(1)
-
-    usage_version(command);
+            if real_exit:
+                command = '%'.join(inputs)  # delimit multiple commands with '%'
+                call_daemon(shell, command.encode())
+                break
+        else:
+            if real_exit:
+                break
+            call_daemon(shell, command.encode())
 
-    return command
+    sys.exit(0)
 
 def main():
     '''
@@ -225,8 +238,12 @@ def main():
         if sys.argv[1] in ("disable-daemon", "--disable-daemon"):
             disable_daemon=True
 
+    interactive_mode = True
+    if shell.prefs['daemon_use_batch_mode']:
+        interactive_mode = False
+
     if use_daemon and not disable_daemon:
-        call_daemon(shell, get_arguments(shell).encode())
+        switch_to_daemon(shell, interactive_mode)
         # does not return
 
     try:
diff --git a/targetcli/ui_node.py b/targetcli/ui_node.py
index 58a70c6..0485950 100644
--- a/targetcli/ui_node.py
+++ b/targetcli/ui_node.py
@@ -52,6 +52,9 @@ class UINode(ConfigNode):
         self.define_config_group_param(
             'global', 'auto_use_daemon', 'bool',
             'If true, commands will be sent to targetclid.')
+        self.define_config_group_param(
+            'global', 'daemon_use_batch_mode', 'bool',
+            'If true, use batch mode for daemonized approach.')
 
     def assert_root(self):
         '''
diff --git a/targetclid.8 b/targetclid.8
index a783091..5760168 100644
--- a/targetclid.8
+++ b/targetclid.8
@@ -30,11 +30,38 @@ $ targetcli set global auto_use_daemon=true
 .br
 $ targetcli ls
 .TP
-You can use batch mode for sending multiple commands in one go,
+You can use interactive mode,
 .br
 $ targetcli <hit-enter>
 .br
-targetcli shell version 2.1.50
+targetcli shell version 2.1.51
+.br
+Entering targetcli interactive mode for daemonized approach.
+.br
+Type 'exit' to quit.
+.br
+/> pwd
+.br
+/
+.br
+/> cd /iscsi
+.br
+/> pwd
+.br
+/iscsi
+.br
+/> exit
+.br
+.TP
+You can also use batch mode for sending multiple commands in one go,
+.br
+$ targetcli set global daemon_use_batch_mode=true
+.br
+Parameter daemon_use_batch_mode is now 'true'.
+.br
+$ targetcli <hit-enter>
+.br
+targetcli shell version 2.1.51
 .br
 Entering targetcli batch mode for daemonized approach.
 .br
-- 
2.21.0