[libvirt] [PATCH 3/8] virsh: Introduce virshInterfaceNameCompleter

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


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

diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c
index d6ac0ccb8..f5b1e4261 100644
--- a/tools/virsh-completer.c
+++ b/tools/virsh-completer.c
@@ -250,3 +250,50 @@ virshStorageVolNameCompleter(vshControl *ctl,
     virStoragePoolFree(pool);
     return NULL;
 }
+
+
+char **
+virshInterfaceNameCompleter(vshControl *ctl,
+                            const vshCmd *cmd ATTRIBUTE_UNUSED,
+                            unsigned int flags)
+{
+    virshControlPtr priv = ctl->privData;
+    virInterfacePtr *ifaces = NULL;
+    int nifaces = 0;
+    size_t i = 0;
+    char **ret = NULL;
+
+    virCheckFlags(VIR_CONNECT_LIST_INTERFACES_ACTIVE |
+                  VIR_CONNECT_LIST_INTERFACES_INACTIVE,
+                  NULL);
+
+    if (!priv->conn || virConnectIsAlive(priv->conn) <= 0)
+        return NULL;
+
+    if ((nifaces = virConnectListAllInterfaces(priv->conn, &ifaces, flags)) < 0)
+        return NULL;
+
+    if (VIR_ALLOC_N(ret, nifaces + 1) < 0)
+        goto error;
+
+    for (i = 0; i < nifaces; i++) {
+        const char *name = virInterfaceGetName(ifaces[i]);
+
+        if (VIR_STRDUP(ret[i], name) < 0)
+            goto error;
+
+        virInterfaceFree(ifaces[i]);
+    }
+    VIR_FREE(ifaces);
+
+    return ret;
+
+ error:
+    for (; i < nifaces; i++)
+        virInterfaceFree(ifaces[i]);
+    VIR_FREE(ifaces);
+    for (i = 0; i < nifaces; i++)
+        VIR_FREE(ret[i]);
+    VIR_FREE(ret);
+    return NULL;
+}
diff --git a/tools/virsh-completer.h b/tools/virsh-completer.h
index 1e4ce5932..2323aaba3 100644
--- a/tools/virsh-completer.h
+++ b/tools/virsh-completer.h
@@ -46,4 +46,8 @@ char ** virshStorageVolNameCompleter(vshControl *ctl,
                                      const vshCmd *cmd,
                                      unsigned int flags);
 
+char ** virshInterfaceNameCompleter(vshControl *ctl,
+                                    const vshCmd *cmd,
+                                    unsigned int flags);
+
 #endif
diff --git a/tools/virsh-interface.c b/tools/virsh-interface.c
index c1a2b21de..50518c667 100644
--- a/tools/virsh-interface.c
+++ b/tools/virsh-interface.c
@@ -23,11 +23,13 @@
  *
  */
 
-#define VIRSH_COMMON_OPT_INTERFACE \
+#define VIRSH_COMMON_OPT_INTERFACE(cflags) \
     {.name = "interface", \
      .type = VSH_OT_DATA, \
      .flags = VSH_OFLAG_REQ, \
-     .help = N_("interface name or MAC address") \
+     .help = N_("interface name or MAC address"), \
+     .completer = virshInterfaceNameCompleter, \
+     .completer_flags = cflags, \
     }
 
 #include <config.h>
@@ -107,7 +109,7 @@ static const vshCmdInfo info_interface_edit[] = {
 };
 
 static const vshCmdOptDef opts_interface_edit[] = {
-    VIRSH_COMMON_OPT_INTERFACE,
+    VIRSH_COMMON_OPT_INTERFACE(0),
     {.name = NULL}
 };
 
@@ -467,7 +469,7 @@ static const vshCmdInfo info_interface_dumpxml[] = {
 };
 
 static const vshCmdOptDef opts_interface_dumpxml[] = {
-    VIRSH_COMMON_OPT_INTERFACE,
+    VIRSH_COMMON_OPT_INTERFACE(0),
     {.name = "inactive",
      .type = VSH_OT_BOOL,
      .help = N_("show inactive defined XML")
@@ -564,7 +566,7 @@ static const vshCmdInfo info_interface_undefine[] = {
 };
 
 static const vshCmdOptDef opts_interface_undefine[] = {
-    VIRSH_COMMON_OPT_INTERFACE,
+    VIRSH_COMMON_OPT_INTERFACE(0),
     {.name = NULL}
 };
 
@@ -603,7 +605,7 @@ static const vshCmdInfo info_interface_start[] = {
 };
 
 static const vshCmdOptDef opts_interface_start[] = {
-    VIRSH_COMMON_OPT_INTERFACE,
+    VIRSH_COMMON_OPT_INTERFACE(VIR_CONNECT_LIST_INTERFACES_INACTIVE),
     {.name = NULL}
 };
 
@@ -642,7 +644,7 @@ static const vshCmdInfo info_interface_destroy[] = {
 };
 
 static const vshCmdOptDef opts_interface_destroy[] = {
-    VIRSH_COMMON_OPT_INTERFACE,
+    VIRSH_COMMON_OPT_INTERFACE(VIR_CONNECT_LIST_INTERFACES_ACTIVE),
     {.name = NULL}
 };
 
-- 
2.13.6




More information about the libvir-list mailing list