Add a '--split' switch for the 'virsh echo' command and add few test
cases to the virshtest.
Signed-off-by: Peter Krempa <pkrempa(a)redhat.com>
---
tests/virshtest.c | 11 +++++++++++
tools/vsh.c | 15 +++++++++++++++
2 files changed, 26 insertions(+)
diff --git a/tests/virshtest.c b/tests/virshtest.c
index 07c27428ae..751e8ffc49 100644
--- a/tests/virshtest.c
+++ b/tests/virshtest.c
@@ -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);
diff --git a/tools/vsh.c b/tools/vsh.c
index 2456267426..cdcf67684e 100644
--- a/tools/vsh.c
+++ b/tools/vsh.c
@@ -3118,6 +3118,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"),
@@ -3156,11 +3160,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;
@@ -3169,6 +3176,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);
}
--
2.31.1