virsh: Introduce virshSnapshotNameCompleter

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 15:28:30 +01:00
parent bab521d837
commit 4cb4b649c3
3 changed files with 69 additions and 7 deletions

View File

@ -478,3 +478,54 @@ virshSecretUUIDCompleter(vshControl *ctl,
VIR_FREE(ret);
return NULL;
}
char **
virshSnapshotNameCompleter(vshControl *ctl,
const vshCmd *cmd,
unsigned int flags)
{
virshControlPtr priv = ctl->privData;
virDomainPtr dom = NULL;
virDomainSnapshotPtr *snapshots = NULL;
int nsnapshots = 0;
size_t i = 0;
char **ret = NULL;
virCheckFlags(0, NULL);
if (!priv->conn || virConnectIsAlive(priv->conn) <= 0)
return NULL;
if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
return NULL;
if ((nsnapshots = virDomainListAllSnapshots(dom, &snapshots, flags)) < 0)
goto error;
if (VIR_ALLOC_N(ret, nsnapshots + 1) < 0)
goto error;
for (i = 0; i < nsnapshots; i++) {
const char *name = virDomainSnapshotGetName(snapshots[i]);
if (VIR_STRDUP(ret[i], name) < 0)
goto error;
virshDomainSnapshotFree(snapshots[i]);
}
VIR_FREE(snapshots);
virshDomainFree(dom);
return ret;
error:
for (; i < nsnapshots; i++)
virshDomainSnapshotFree(snapshots[i]);
VIR_FREE(snapshots);
for (i = 0; i < nsnapshots; i++)
VIR_FREE(ret[i]);
VIR_FREE(ret);
virshDomainFree(dom);
return NULL;
}

View File

@ -66,4 +66,8 @@ char ** virshSecretUUIDCompleter(vshControl *ctl,
const vshCmd *cmd,
unsigned int flags);
char ** virshSnapshotNameCompleter(vshControl *ctl,
const vshCmd *cmd,
unsigned int flags);
#endif

View File

@ -511,7 +511,8 @@ static const vshCmdOptDef opts_snapshot_edit[] = {
VIRSH_COMMON_OPT_DOMAIN_FULL(0),
{.name = "snapshotname",
.type = VSH_OT_STRING,
.help = N_("snapshot name")
.help = N_("snapshot name"),
.completer = virshSnapshotNameCompleter,
},
VIRSH_COMMON_OPT_CURRENT(N_("also set edited snapshot as current")),
{.name = "rename",
@ -631,7 +632,8 @@ static const vshCmdOptDef opts_snapshot_current[] = {
},
{.name = "snapshotname",
.type = VSH_OT_STRING,
.help = N_("name of existing snapshot to make current")
.help = N_("name of existing snapshot to make current"),
.completer = virshSnapshotNameCompleter,
},
{.name = NULL}
};
@ -854,7 +856,8 @@ static const vshCmdOptDef opts_snapshot_info[] = {
VIRSH_COMMON_OPT_DOMAIN_FULL(0),
{.name = "snapshotname",
.type = VSH_OT_STRING,
.help = N_("snapshot name")
.help = N_("snapshot name"),
.completer = virshSnapshotNameCompleter,
},
VIRSH_COMMON_OPT_CURRENT(N_("info on current snapshot")),
{.name = NULL}
@ -1661,7 +1664,8 @@ static const vshCmdOptDef opts_snapshot_dumpxml[] = {
{.name = "snapshotname",
.type = VSH_OT_DATA,
.flags = VSH_OFLAG_REQ,
.help = N_("snapshot name")
.help = N_("snapshot name"),
.completer = virshSnapshotNameCompleter,
},
{.name = "security-info",
.type = VSH_OT_BOOL,
@ -1723,7 +1727,8 @@ static const vshCmdOptDef opts_snapshot_parent[] = {
VIRSH_COMMON_OPT_DOMAIN_FULL(0),
{.name = "snapshotname",
.type = VSH_OT_STRING,
.help = N_("find parent of snapshot name")
.help = N_("find parent of snapshot name"),
.completer = virshSnapshotNameCompleter,
},
VIRSH_COMMON_OPT_CURRENT(N_("find parent of current snapshot")),
{.name = NULL}
@ -1782,7 +1787,8 @@ static const vshCmdOptDef opts_snapshot_revert[] = {
VIRSH_COMMON_OPT_DOMAIN_FULL(0),
{.name = "snapshotname",
.type = VSH_OT_STRING,
.help = N_("snapshot name")
.help = N_("snapshot name"),
.completer = virshSnapshotNameCompleter,
},
VIRSH_COMMON_OPT_CURRENT(N_("revert to current snapshot")),
{.name = "running",
@ -1866,7 +1872,8 @@ static const vshCmdOptDef opts_snapshot_delete[] = {
VIRSH_COMMON_OPT_DOMAIN_FULL(0),
{.name = "snapshotname",
.type = VSH_OT_STRING,
.help = N_("snapshot name")
.help = N_("snapshot name"),
.completer = virshSnapshotNameCompleter,
},
VIRSH_COMMON_OPT_CURRENT(N_("delete current snapshot")),
{.name = "children",