virsh: Add network uuid completion to network-name command

Signed-off-by: Lin Ma <lma@suse.de>
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Lin Ma 2020-09-11 15:13:08 +08:00 committed by Michal Privoznik
parent d9a320bf97
commit f6e69f03e3
3 changed files with 47 additions and 0 deletions

View File

@ -137,3 +137,45 @@ virshNetworkPortUUIDCompleter(vshControl *ctl,
VIR_FREE(ret);
return NULL;
}
char **
virshNetworkUUIDCompleter(vshControl *ctl,
const vshCmd *cmd G_GNUC_UNUSED,
unsigned int flags)
{
virshControlPtr priv = ctl->privData;
virNetworkPtr *nets = NULL;
int nnets = 0;
size_t i = 0;
char **ret = NULL;
VIR_AUTOSTRINGLIST tmp = NULL;
virCheckFlags(0, NULL);
if (!priv->conn || virConnectIsAlive(priv->conn) <= 0)
return NULL;
if ((nnets = virConnectListAllNetworks(priv->conn, &nets, flags)) < 0)
return NULL;
if (VIR_ALLOC_N(tmp, nnets + 1) < 0)
goto cleanup;
for (i = 0; i < nnets; i++) {
char uuid[VIR_UUID_STRING_BUFLEN];
if (virNetworkGetUUIDString(nets[i], uuid) < 0)
goto cleanup;
tmp[i] = g_strdup(uuid);
}
ret = g_steal_pointer(&tmp);
cleanup:
for (i = 0; i < nnets; i++)
virNetworkFree(nets[i]);
VIR_FREE(nets);
return ret;
}

View File

@ -33,3 +33,7 @@ char ** virshNetworkEventNameCompleter(vshControl *ctl,
char ** virshNetworkPortUUIDCompleter(vshControl *ctl,
const vshCmd *cmd,
unsigned int flags);
char ** virshNetworkUUIDCompleter(vshControl *ctl,
const vshCmd *cmd,
unsigned int flags);

View File

@ -806,6 +806,7 @@ static const vshCmdOptDef opts_network_name[] = {
{.name = "network",
.type = VSH_OT_DATA,
.flags = VSH_OFLAG_REQ,
.completer = virshNetworkUUIDCompleter,
.help = N_("network uuid")
},
{.name = NULL}