virsh: Introduce virshNetworkNameCompleter

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:35:40 +01:00
parent 4f42e2c4d3
commit 7bed1a5b61
3 changed files with 65 additions and 11 deletions

View File

@ -294,3 +294,51 @@ virshInterfaceNameCompleter(vshControl *ctl,
VIR_FREE(ret);
return NULL;
}
char **
virshNetworkNameCompleter(vshControl *ctl,
const vshCmd *cmd ATTRIBUTE_UNUSED,
unsigned int flags)
{
virshControlPtr priv = ctl->privData;
virNetworkPtr *nets = NULL;
int nnets = 0;
size_t i = 0;
char **ret = NULL;
virCheckFlags(VIR_CONNECT_LIST_NETWORKS_INACTIVE |
VIR_CONNECT_LIST_NETWORKS_ACTIVE |
VIR_CONNECT_LIST_NETWORKS_PERSISTENT,
NULL);
if (!priv->conn || virConnectIsAlive(priv->conn) <= 0)
return NULL;
if ((nnets = virConnectListAllNetworks(priv->conn, &nets, flags)) < 0)
return NULL;
if (VIR_ALLOC_N(ret, nnets + 1) < 0)
goto error;
for (i = 0; i < nnets; i++) {
const char *name = virNetworkGetName(nets[i]);
if (VIR_STRDUP(ret[i], name) < 0)
goto error;
virNetworkFree(nets[i]);
}
VIR_FREE(nets);
return ret;
error:
for (; i < nnets; i++)
virNetworkFree(nets[i]);
VIR_FREE(nets);
for (i = 0; i < nnets; i++)
VIR_FREE(ret[i]);
VIR_FREE(ret);
return NULL;
}

View File

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

View File

@ -34,11 +34,13 @@
#include "virtime.h"
#include "conf/network_conf.h"
#define VIRSH_COMMON_OPT_NETWORK \
#define VIRSH_COMMON_OPT_NETWORK(cflags) \
{.name = "network", \
.type = VSH_OT_DATA, \
.flags = VSH_OFLAG_REQ, \
.help = N_("network name or uuid") \
.help = N_("network name or uuid"), \
.completer = virshNetworkNameCompleter, \
.completer_flags = cflags, \
}
virNetworkPtr
@ -93,7 +95,7 @@ static const vshCmdInfo info_network_autostart[] = {
};
static const vshCmdOptDef opts_network_autostart[] = {
VIRSH_COMMON_OPT_NETWORK,
VIRSH_COMMON_OPT_NETWORK(VIR_CONNECT_LIST_NETWORKS_PERSISTENT),
{.name = "disable",
.type = VSH_OT_BOOL,
.help = N_("disable autostarting")
@ -240,7 +242,7 @@ static const vshCmdInfo info_network_destroy[] = {
};
static const vshCmdOptDef opts_network_destroy[] = {
VIRSH_COMMON_OPT_NETWORK,
VIRSH_COMMON_OPT_NETWORK(VIR_CONNECT_LIST_NETWORKS_ACTIVE),
{.name = NULL}
};
@ -279,7 +281,7 @@ static const vshCmdInfo info_network_dumpxml[] = {
};
static const vshCmdOptDef opts_network_dumpxml[] = {
VIRSH_COMMON_OPT_NETWORK,
VIRSH_COMMON_OPT_NETWORK(0),
{.name = "inactive",
.type = VSH_OT_BOOL,
.help = N_("show inactive defined XML")
@ -330,7 +332,7 @@ static const vshCmdInfo info_network_info[] = {
};
static const vshCmdOptDef opts_network_info[] = {
VIRSH_COMMON_OPT_NETWORK,
VIRSH_COMMON_OPT_NETWORK(0),
{.name = NULL}
};
@ -779,7 +781,7 @@ static const vshCmdInfo info_network_start[] = {
};
static const vshCmdOptDef opts_network_start[] = {
VIRSH_COMMON_OPT_NETWORK,
VIRSH_COMMON_OPT_NETWORK(VIR_CONNECT_LIST_NETWORKS_INACTIVE),
{.name = NULL}
};
@ -817,7 +819,7 @@ static const vshCmdInfo info_network_undefine[] = {
};
static const vshCmdOptDef opts_network_undefine[] = {
VIRSH_COMMON_OPT_NETWORK,
VIRSH_COMMON_OPT_NETWORK(VIR_CONNECT_LIST_NETWORKS_PERSISTENT),
{.name = NULL}
};
@ -856,7 +858,7 @@ static const vshCmdInfo info_network_update[] = {
};
static const vshCmdOptDef opts_network_update[] = {
VIRSH_COMMON_OPT_NETWORK,
VIRSH_COMMON_OPT_NETWORK(0),
{.name = "command",
.type = VSH_OT_DATA,
.flags = VSH_OFLAG_REQ,
@ -1057,7 +1059,7 @@ static const vshCmdInfo info_network_edit[] = {
};
static const vshCmdOptDef opts_network_edit[] = {
VIRSH_COMMON_OPT_NETWORK,
VIRSH_COMMON_OPT_NETWORK(0),
{.name = NULL}
};
@ -1304,7 +1306,7 @@ static const vshCmdInfo info_network_dhcp_leases[] = {
};
static const vshCmdOptDef opts_network_dhcp_leases[] = {
VIRSH_COMMON_OPT_NETWORK,
VIRSH_COMMON_OPT_NETWORK(0),
{.name = "mac",
.type = VSH_OT_STRING,
.flags = VSH_OFLAG_NONE,