[libvirt] [PATCH 8/8] virsh: Introduce virshSnapshotNameCompleter

Michal Privoznik mprivozn at redhat.com
Fri Jan 12 14:37:42 UTC 2018


Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 tools/virsh-completer.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++
 tools/virsh-completer.h |  4 ++++
 tools/virsh-snapshot.c  | 21 +++++++++++++-------
 3 files changed, 69 insertions(+), 7 deletions(-)

diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c
index 7332fa97a..9db7c59d2 100644
--- a/tools/virsh-completer.c
+++ b/tools/virsh-completer.c
@@ -484,3 +484,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;
+}
diff --git a/tools/virsh-completer.h b/tools/virsh-completer.h
index 0e518873c..fa443d3ad 100644
--- a/tools/virsh-completer.h
+++ b/tools/virsh-completer.h
@@ -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
diff --git a/tools/virsh-snapshot.c b/tools/virsh-snapshot.c
index c44a36f98..e4908eea7 100644
--- a/tools/virsh-snapshot.c
+++ b/tools/virsh-snapshot.c
@@ -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",
-- 
2.13.6




More information about the libvir-list mailing list