mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-10-03 04:45:46 +00:00
virsh: Add testing for vshStringToArray
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>
This commit is contained in:
parent
31fa241b46
commit
4db8ffeb2e
@ -487,6 +487,17 @@ mymain(void)
|
|||||||
DO_TEST(46, "a\n", "#unbalanced; 'quotes\"\necho a # b");
|
DO_TEST(46, "a\n", "#unbalanced; 'quotes\"\necho a # b");
|
||||||
DO_TEST(47, "a\n", "\\# ignored;echo a\n'#also' ignored");
|
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
|
# undef DO_TEST
|
||||||
|
|
||||||
VIR_FREE(custom_uri);
|
VIR_FREE(custom_uri);
|
||||||
|
15
tools/vsh.c
15
tools/vsh.c
@ -3115,6 +3115,10 @@ const vshCmdOptDef opts_echo[] = {
|
|||||||
.type = VSH_OT_BOOL,
|
.type = VSH_OT_BOOL,
|
||||||
.help = N_("escape for XML use")
|
.help = N_("escape for XML use")
|
||||||
},
|
},
|
||||||
|
{.name = "split",
|
||||||
|
.type = VSH_OT_BOOL,
|
||||||
|
.help = N_("split each argument on ','; ',,' is an escape sequence")
|
||||||
|
},
|
||||||
{.name = "err",
|
{.name = "err",
|
||||||
.type = VSH_OT_BOOL,
|
.type = VSH_OT_BOOL,
|
||||||
.help = N_("output to stderr"),
|
.help = N_("output to stderr"),
|
||||||
@ -3153,11 +3157,14 @@ cmdEcho(vshControl *ctl, const vshCmd *cmd)
|
|||||||
bool shell = vshCommandOptBool(cmd, "shell");
|
bool shell = vshCommandOptBool(cmd, "shell");
|
||||||
bool xml = vshCommandOptBool(cmd, "xml");
|
bool xml = vshCommandOptBool(cmd, "xml");
|
||||||
bool err = vshCommandOptBool(cmd, "err");
|
bool err = vshCommandOptBool(cmd, "err");
|
||||||
|
bool split = vshCommandOptBool(cmd, "split");
|
||||||
const vshCmdOpt *opt = NULL;
|
const vshCmdOpt *opt = NULL;
|
||||||
g_autofree char *arg = NULL;
|
g_autofree char *arg = NULL;
|
||||||
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
|
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
|
||||||
|
|
||||||
VSH_EXCLUSIVE_OPTIONS_VAR(shell, xml);
|
VSH_EXCLUSIVE_OPTIONS_VAR(shell, xml);
|
||||||
|
VSH_EXCLUSIVE_OPTIONS_VAR(shell, split);
|
||||||
|
VSH_EXCLUSIVE_OPTIONS_VAR(xml, split);
|
||||||
|
|
||||||
while ((opt = vshCommandOptArgv(ctl, cmd, opt))) {
|
while ((opt = vshCommandOptArgv(ctl, cmd, opt))) {
|
||||||
const char *curr = opt->data;
|
const char *curr = opt->data;
|
||||||
@ -3166,6 +3173,14 @@ cmdEcho(vshControl *ctl, const vshCmd *cmd)
|
|||||||
virBufferEscapeString(&buf, "%s", curr);
|
virBufferEscapeString(&buf, "%s", curr);
|
||||||
} else if (shell) {
|
} else if (shell) {
|
||||||
virBufferEscapeShell(&buf, curr);
|
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 {
|
} else {
|
||||||
virBufferAdd(&buf, curr, -1);
|
virBufferAdd(&buf, curr, -1);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user