[libvirt] [PATCH 4/8] virsh: Introduce virshNetworkNameCompleter

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


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

diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c
index f5b1e4261..2c0d4f640 100644
--- a/tools/virsh-completer.c
+++ b/tools/virsh-completer.c
@@ -297,3 +297,54 @@ virshInterfaceNameCompleter(vshControl *ctl,
     VIR_FREE(ret);
     return NULL;
 }
+
+
+char **
+virshNetworkNameCompleter(vshControl *ctl,
+                          const vshCmd *cmd ATTRIBUTE_UNUSED,
+                          unsigned int flags)
+{
+    virshControlPtr priv = ctl->privData;
+    virNetworkPtr *nets = NULL;
+    int nnets = 0;
+    size_t i = 0;
+    char **ret = NULL;
+
+    virCheckFlags(VIR_CONNECT_LIST_NETWORKS_INACTIVE |
+                  VIR_CONNECT_LIST_NETWORKS_ACTIVE |
+                  VIR_CONNECT_LIST_NETWORKS_PERSISTENT |
+                  VIR_CONNECT_LIST_NETWORKS_TRANSIENT |
+                  VIR_CONNECT_LIST_NETWORKS_AUTOSTART |
+                  VIR_CONNECT_LIST_NETWORKS_NO_AUTOSTART,
+                  NULL);
+
+    if (!priv->conn || virConnectIsAlive(priv->conn) <= 0)
+        return NULL;
+
+    if ((nnets = virConnectListAllNetworks(priv->conn, &nets, flags)) < 0)
+        return NULL;
+
+    if (VIR_ALLOC_N(ret, nnets + 1) < 0)
+        goto error;
+
+    for (i = 0; i < nnets; i++) {
+        const char *name = virNetworkGetName(nets[i]);
+
+        if (VIR_STRDUP(ret[i], name) < 0)
+            goto error;
+
+        virNetworkFree(nets[i]);
+    }
+    VIR_FREE(nets);
+
+    return ret;
+
+ error:
+    for (; i < nnets; i++)
+        virNetworkFree(nets[i]);
+    VIR_FREE(nets);
+    for (i = 0; i < nnets; i++)
+        VIR_FREE(ret[i]);
+    VIR_FREE(ret);
+    return NULL;
+}
diff --git a/tools/virsh-completer.h b/tools/virsh-completer.h
index 2323aaba3..20ba4cb55 100644
--- a/tools/virsh-completer.h
+++ b/tools/virsh-completer.h
@@ -50,4 +50,8 @@ char ** virshInterfaceNameCompleter(vshControl *ctl,
                                     const vshCmd *cmd,
                                     unsigned int flags);
 
+char ** virshNetworkNameCompleter(vshControl *ctl,
+                                  const vshCmd *cmd,
+                                  unsigned int flags);
+
 #endif
diff --git a/tools/virsh-network.c b/tools/virsh-network.c
index cd55e384f..3b472ea67 100644
--- a/tools/virsh-network.c
+++ b/tools/virsh-network.c
@@ -34,11 +34,13 @@
 #include "virtime.h"
 #include "conf/network_conf.h"
 
-#define VIRSH_COMMON_OPT_NETWORK \
+#define VIRSH_COMMON_OPT_NETWORK(cflags) \
     {.name = "network", \
      .type = VSH_OT_DATA, \
      .flags = VSH_OFLAG_REQ, \
-     .help = N_("network name or uuid") \
+     .help = N_("network name or uuid"), \
+     .completer = virshNetworkNameCompleter, \
+     .completer_flags = cflags, \
     }
 
 virNetworkPtr
@@ -93,7 +95,7 @@ static const vshCmdInfo info_network_autostart[] = {
 };
 
 static const vshCmdOptDef opts_network_autostart[] = {
-    VIRSH_COMMON_OPT_NETWORK,
+    VIRSH_COMMON_OPT_NETWORK(0),
     {.name = "disable",
      .type = VSH_OT_BOOL,
      .help = N_("disable autostarting")
@@ -240,7 +242,7 @@ static const vshCmdInfo info_network_destroy[] = {
 };
 
 static const vshCmdOptDef opts_network_destroy[] = {
-    VIRSH_COMMON_OPT_NETWORK,
+    VIRSH_COMMON_OPT_NETWORK(VIR_CONNECT_LIST_NETWORKS_ACTIVE),
     {.name = NULL}
 };
 
@@ -279,7 +281,7 @@ static const vshCmdInfo info_network_dumpxml[] = {
 };
 
 static const vshCmdOptDef opts_network_dumpxml[] = {
-    VIRSH_COMMON_OPT_NETWORK,
+    VIRSH_COMMON_OPT_NETWORK(0),
     {.name = "inactive",
      .type = VSH_OT_BOOL,
      .help = N_("show inactive defined XML")
@@ -330,7 +332,7 @@ static const vshCmdInfo info_network_info[] = {
 };
 
 static const vshCmdOptDef opts_network_info[] = {
-    VIRSH_COMMON_OPT_NETWORK,
+    VIRSH_COMMON_OPT_NETWORK(0),
     {.name = NULL}
 };
 
@@ -779,7 +781,7 @@ static const vshCmdInfo info_network_start[] = {
 };
 
 static const vshCmdOptDef opts_network_start[] = {
-    VIRSH_COMMON_OPT_NETWORK,
+    VIRSH_COMMON_OPT_NETWORK(VIR_CONNECT_LIST_NETWORKS_INACTIVE),
     {.name = NULL}
 };
 
@@ -817,7 +819,7 @@ static const vshCmdInfo info_network_undefine[] = {
 };
 
 static const vshCmdOptDef opts_network_undefine[] = {
-    VIRSH_COMMON_OPT_NETWORK,
+    VIRSH_COMMON_OPT_NETWORK(0),
     {.name = NULL}
 };
 
@@ -856,7 +858,7 @@ static const vshCmdInfo info_network_update[] = {
 };
 
 static const vshCmdOptDef opts_network_update[] = {
-    VIRSH_COMMON_OPT_NETWORK,
+    VIRSH_COMMON_OPT_NETWORK(0),
     {.name = "command",
      .type = VSH_OT_DATA,
      .flags = VSH_OFLAG_REQ,
@@ -1057,7 +1059,7 @@ static const vshCmdInfo info_network_edit[] = {
 };
 
 static const vshCmdOptDef opts_network_edit[] = {
-    VIRSH_COMMON_OPT_NETWORK,
+    VIRSH_COMMON_OPT_NETWORK(0),
     {.name = NULL}
 };
 
@@ -1304,7 +1306,7 @@ static const vshCmdInfo info_network_dhcp_leases[] = {
 };
 
 static const vshCmdOptDef opts_network_dhcp_leases[] = {
-    VIRSH_COMMON_OPT_NETWORK,
+    VIRSH_COMMON_OPT_NETWORK(0),
     {.name = "mac",
      .type = VSH_OT_STRING,
      .flags = VSH_OFLAG_NONE,
-- 
2.13.6




More information about the libvir-list mailing list