]> xenbits.xensource.com Git - libvirt.git/commitdiff
virsh: Introduce virshSnapshotNameCompleter
authorMichal Privoznik <mprivozn@redhat.com>
Fri, 12 Jan 2018 14:28:30 +0000 (15:28 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Wed, 24 Jan 2018 12:51:23 +0000 (13:51 +0100)
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
tools/virsh-completer.c
tools/virsh-completer.h
tools/virsh-snapshot.c

index c06828df4ed93c53c3f49ab6b74c1d126365d3aa..e216d9076139ae80899c48c591d5686cba7541a9 100644 (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;
+}
index 0e518873c5e2a7ade0482255ebdacb5c5d51f406..fa443d3ad77c91cc884a54f68b4f0afc27620971 100644 (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
index c44a36f981c9e1d0b6c8c29218d3c99ba01b4313..e4908eea70b86d7e3528cf84e764de835d681a60 100644 (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",