virsh: Introduce virshInterfaceNameCompleter

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
This commit is contained in:
Michal Privoznik 2018-01-12 14:26:35 +01:00
parent f81f8b62bd
commit 4f42e2c4d3
3 changed files with 60 additions and 7 deletions

View File

@ -247,3 +247,50 @@ virshStorageVolNameCompleter(vshControl *ctl,
virStoragePoolFree(pool);
return NULL;
}
char **
virshInterfaceNameCompleter(vshControl *ctl,
const vshCmd *cmd ATTRIBUTE_UNUSED,
unsigned int flags)
{
virshControlPtr priv = ctl->privData;
virInterfacePtr *ifaces = NULL;
int nifaces = 0;
size_t i = 0;
char **ret = NULL;
virCheckFlags(VIR_CONNECT_LIST_INTERFACES_ACTIVE |
VIR_CONNECT_LIST_INTERFACES_INACTIVE,
NULL);
if (!priv->conn || virConnectIsAlive(priv->conn) <= 0)
return NULL;
if ((nifaces = virConnectListAllInterfaces(priv->conn, &ifaces, flags)) < 0)
return NULL;
if (VIR_ALLOC_N(ret, nifaces + 1) < 0)
goto error;
for (i = 0; i < nifaces; i++) {
const char *name = virInterfaceGetName(ifaces[i]);
if (VIR_STRDUP(ret[i], name) < 0)
goto error;
virInterfaceFree(ifaces[i]);
}
VIR_FREE(ifaces);
return ret;
error:
for (; i < nifaces; i++)
virInterfaceFree(ifaces[i]);
VIR_FREE(ifaces);
for (i = 0; i < nifaces; i++)
VIR_FREE(ret[i]);
VIR_FREE(ret);
return NULL;
}

View File

@ -46,4 +46,8 @@ char ** virshStorageVolNameCompleter(vshControl *ctl,
const vshCmd *cmd,
unsigned int flags);
char ** virshInterfaceNameCompleter(vshControl *ctl,
const vshCmd *cmd,
unsigned int flags);
#endif

View File

@ -23,11 +23,13 @@
*
*/
#define VIRSH_COMMON_OPT_INTERFACE \
#define VIRSH_COMMON_OPT_INTERFACE(cflags) \
{.name = "interface", \
.type = VSH_OT_DATA, \
.flags = VSH_OFLAG_REQ, \
.help = N_("interface name or MAC address") \
.help = N_("interface name or MAC address"), \
.completer = virshInterfaceNameCompleter, \
.completer_flags = cflags, \
}
#include <config.h>
@ -107,7 +109,7 @@ static const vshCmdInfo info_interface_edit[] = {
};
static const vshCmdOptDef opts_interface_edit[] = {
VIRSH_COMMON_OPT_INTERFACE,
VIRSH_COMMON_OPT_INTERFACE(0),
{.name = NULL}
};
@ -467,7 +469,7 @@ static const vshCmdInfo info_interface_dumpxml[] = {
};
static const vshCmdOptDef opts_interface_dumpxml[] = {
VIRSH_COMMON_OPT_INTERFACE,
VIRSH_COMMON_OPT_INTERFACE(0),
{.name = "inactive",
.type = VSH_OT_BOOL,
.help = N_("show inactive defined XML")
@ -564,7 +566,7 @@ static const vshCmdInfo info_interface_undefine[] = {
};
static const vshCmdOptDef opts_interface_undefine[] = {
VIRSH_COMMON_OPT_INTERFACE,
VIRSH_COMMON_OPT_INTERFACE(0),
{.name = NULL}
};
@ -603,7 +605,7 @@ static const vshCmdInfo info_interface_start[] = {
};
static const vshCmdOptDef opts_interface_start[] = {
VIRSH_COMMON_OPT_INTERFACE,
VIRSH_COMMON_OPT_INTERFACE(VIR_CONNECT_LIST_INTERFACES_INACTIVE),
{.name = NULL}
};
@ -642,7 +644,7 @@ static const vshCmdInfo info_interface_destroy[] = {
};
static const vshCmdOptDef opts_interface_destroy[] = {
VIRSH_COMMON_OPT_INTERFACE,
VIRSH_COMMON_OPT_INTERFACE(VIR_CONNECT_LIST_INTERFACES_ACTIVE),
{.name = NULL}
};