Skip to content

Commit 979f8e3

Browse files
maurizio-lombardiigaw
authored andcommitted
nvmetcli: allow running arbitrary commands non-interactively
This patch refactors the command-line argument handling to allow for the execution of any valid shell command directly from the system's command line, without entering the interactive mode. Previously, only a limited, hardcoded set of commands (`save`, `restore`, `clear`, `ls`) could be run non-interactively. This change removes the special case for the `ls` command and introduces a generic mechanism that concatenates all command-line arguments and executes them as a single command within the `nvmetcli` shell. This enhancement improves the scriptability of the tool. For example, users can now run complex commands like: $ nvmetcli /subsystems create nqn.2024-07.org.example:sub1 $ nvmetcli /ports ls The usage information has been updated to reflect this new functionality. Signed-off-by: Maurizio Lombardi <mlombard@redhat.com> Reviewed-by: Nilay Shroff <nilay@linux.ibm.com> Signed-off-by: Ales Novak <alnovak@suse.com> Signed-off-by: Daniel Wagner <wagi@kernel.org>
1 parent 711330b commit 979f8e3

1 file changed

Lines changed: 17 additions & 16 deletions

File tree

nvmetcli

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,8 @@ def usage():
764764
print("syntax: %s save [file_to_save_to]" % sys.argv[0])
765765
print(" %s restore [file_to_restore_from]" % sys.argv[0])
766766
print(" %s clear" % sys.argv[0])
767-
print(" %s ls" % sys.argv[0])
767+
print(" %s --help (Print this information)" % sys.argv[0])
768+
print(" %s <CMD> (Run shell command and exit)" % sys.argv[0])
768769
sys.exit(-1)
769770

770771

@@ -801,38 +802,38 @@ def clear(unused):
801802
nvme.Root().clear_existing()
802803

803804

804-
def ls(unused):
805+
def execute_cmd(cmd):
805806
shell = configshell.ConfigShell('~/.nvmetcli')
806807
UIRootNode(shell)
807-
shell.run_cmdline("ls")
808+
shell.run_cmdline(cmd)
808809
sys.exit(0)
809810

810811

811-
funcs = dict(save=save, restore=restore, clear=clear, ls=ls)
812+
funcs = dict(save=save, restore=restore, clear=clear)
812813

813814

814815
def main():
815816
if os.geteuid() != 0:
816817
print("%s: must run as root." % sys.argv[0], file=sys.stderr)
817818
sys.exit(-1)
818819

819-
if len(sys.argv) > 3:
820-
usage()
821-
822-
if len(sys.argv) == 2 or len(sys.argv) == 3:
820+
if len(sys.argv) > 1:
823821
if sys.argv[1] == "--help":
824822
usage()
825823

826-
if sys.argv[1] not in funcs.keys():
827-
usage()
824+
if sys.argv[1] in funcs.keys():
825+
if len(sys.argv) > 3:
826+
usage()
827+
if len(sys.argv) == 3:
828+
savefile = sys.argv[2]
829+
else:
830+
savefile = None
828831

829-
if len(sys.argv) == 3:
830-
savefile = sys.argv[2]
832+
funcs[sys.argv[1]](savefile)
833+
return
831834
else:
832-
savefile = None
833-
834-
funcs[sys.argv[1]](savefile)
835-
return
835+
concat_args = ' '.join(sys.argv[1:])
836+
execute_cmd(concat_args)
836837

837838
try:
838839
shell = configshell.ConfigShell('~/.nvmetcli')

0 commit comments

Comments
 (0)