]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virsh: Add testing for vshStringToArray
authorPeter Krempa <pkrempa@redhat.com>
Wed, 11 Aug 2021 13:22:59 +0000 (15:22 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Wed, 18 Aug 2021 09:07:25 +0000 (11:07 +0200)
Add a '--split' switch for the 'virsh echo' command and add few test
cases to the virshtest.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
tests/virshtest.c
tools/vsh.c

index 07c27428aefb6af2b6a02c7b3ec80234f2ae53a6..751e8ffc493948414ee5cff2dbb025262e5d34b5 100644 (file)
@@ -487,6 +487,17 @@ mymain(void)
     DO_TEST(46, "a\n", "#unbalanced; 'quotes\"\necho a # b");
     DO_TEST(47, "a\n", "\\# ignored;echo a\n'#also' ignored");
 
+    /* test of splitting in vshStringToArray */
+    DO_TEST(48, "a\nb,c,\nd,,e,,\nf,,,e\n",
+            "-q", "echo", "--split", "a,b,,c,,,d,,,,e,,,,,f,,,,,,e");
+    DO_TEST(49, "\na\nb,c,\nd,,e,,\nf,,,e\n\n",
+            "-q", "echo", "--split", ",a,b,,c,,,d,,,,e,,,,,f,,,,,,e,");
+    DO_TEST(50, ",a\nb,c,\nd,,e,,\nf,,,e,\n",
+            "-q", "echo", "--split", ",,a,b,,c,,,d,,,,e,,,,,f,,,,,,e,,");
+    DO_TEST(51, ",\na\nb,c,\nd,,e,,\nf,,,e,\n\n",
+            "-q", "echo", "--split", ",,,a,b,,c,,,d,,,,e,,,,,f,,,,,,e,,,");
+    DO_TEST(52, ",,a\nb,c,\nd,,e,,\nf,,,e,,\n",
+            "-q", "echo", "--split", ",,,,a,b,,c,,,d,,,,e,,,,,f,,,,,,e,,,,");
 # undef DO_TEST
 
     VIR_FREE(custom_uri);
index cca2920711b1594ed38b6725da92b84be9978100..e67c0b35db0ab28a671f52bc1a1995127a69c32c 100644 (file)
@@ -3115,6 +3115,10 @@ const vshCmdOptDef opts_echo[] = {
      .type = VSH_OT_BOOL,
      .help = N_("escape for XML use")
     },
+    {.name = "split",
+     .type = VSH_OT_BOOL,
+     .help = N_("split each argument on ','; ',,' is an escape sequence")
+    },
     {.name = "err",
      .type = VSH_OT_BOOL,
      .help = N_("output to stderr"),
@@ -3153,11 +3157,14 @@ cmdEcho(vshControl *ctl, const vshCmd *cmd)
     bool shell = vshCommandOptBool(cmd, "shell");
     bool xml = vshCommandOptBool(cmd, "xml");
     bool err = vshCommandOptBool(cmd, "err");
+    bool split = vshCommandOptBool(cmd, "split");
     const vshCmdOpt *opt = NULL;
     g_autofree char *arg = NULL;
     g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
 
     VSH_EXCLUSIVE_OPTIONS_VAR(shell, xml);
+    VSH_EXCLUSIVE_OPTIONS_VAR(shell, split);
+    VSH_EXCLUSIVE_OPTIONS_VAR(xml, split);
 
     while ((opt = vshCommandOptArgv(ctl, cmd, opt))) {
         const char *curr = opt->data;
@@ -3166,6 +3173,14 @@ cmdEcho(vshControl *ctl, const vshCmd *cmd)
             virBufferEscapeString(&buf, "%s", curr);
         } else if (shell) {
             virBufferEscapeShell(&buf, curr);
+        } else if (split) {
+            g_auto(GStrv) spl = NULL;
+            GStrv n;
+
+            vshStringToArray(curr, &spl);
+
+            for (n = spl; *n; n++)
+                virBufferAsprintf(&buf, "%s\n", *n);
         } else {
             virBufferAdd(&buf, curr, -1);
         }