[libvirt] [PATCH 08/19] virsh: Add interface mac completion to iface-name command

Lin Ma posted 19 patches 5 years, 3 months ago
There is a newer version of this series
[libvirt] [PATCH 08/19] virsh: Add interface mac completion to iface-name command
Posted by Lin Ma 5 years, 3 months ago
Signed-off-by: Lin Ma <lma@suse.com>
---
 tools/virsh-completer-interface.c | 13 +++++++++----
 tools/virsh-completer-interface.h |  4 ++++
 tools/virsh-interface.c           |  8 +++-----
 3 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/tools/virsh-completer-interface.c b/tools/virsh-completer-interface.c
index 777bb22b0b..c24a2cea6c 100644
--- a/tools/virsh-completer-interface.c
+++ b/tools/virsh-completer-interface.c
@@ -38,7 +38,8 @@ virshInterfaceCompleter(vshControl *ctl,
     VIR_AUTOSTRINGLIST tmp = NULL;
 
     virCheckFlags(VIR_CONNECT_LIST_INTERFACES_ACTIVE |
-                  VIR_CONNECT_LIST_INTERFACES_INACTIVE,
+                  VIR_CONNECT_LIST_INTERFACES_INACTIVE |
+                  VIRSH_INTERFACE_COMPLETER_MAC,
                   NULL);
 
     if (!priv->conn || virConnectIsAlive(priv->conn) <= 0)
@@ -50,9 +51,13 @@ virshInterfaceCompleter(vshControl *ctl,
     tmp = g_new0(char *, nifaces + 1);
 
     for (i = 0; i < nifaces; i++) {
-        const char *name = virInterfaceGetName(ifaces[i]);
-
-        tmp[i] = g_strdup(name);
+        if (!(flags & VIRSH_INTERFACE_COMPLETER_MAC)) {
+            const char *name = virInterfaceGetName(ifaces[i]);
+            tmp[i] = g_strdup(name);
+        } else {
+            const char *mac = virInterfaceGetMACString(ifaces[i]);
+            tmp[i] = g_strdup(mac);
+        }
     }
 
     ret = g_steal_pointer(&tmp);
diff --git a/tools/virsh-completer-interface.h b/tools/virsh-completer-interface.h
index 2b382222d7..71bc44c4b9 100644
--- a/tools/virsh-completer-interface.h
+++ b/tools/virsh-completer-interface.h
@@ -22,6 +22,10 @@
 
 #include "vsh.h"
 
+enum {
+    VIRSH_INTERFACE_COMPLETER_MAC = 1 << 0,
+};
+
 char ** virshInterfaceCompleter(vshControl *ctl,
                                 const vshCmd *cmd,
                                 unsigned int flags);
diff --git a/tools/virsh-interface.c b/tools/virsh-interface.c
index df97b74c4c..1dabc6e406 100644
--- a/tools/virsh-interface.c
+++ b/tools/virsh-interface.c
@@ -404,11 +404,9 @@ static const vshCmdInfo info_interface_name[] = {
 };
 
 static const vshCmdOptDef opts_interface_name[] = {
-    {.name = "interface",
-     .type = VSH_OT_DATA,
-     .flags = VSH_OFLAG_REQ,
-     .help = N_("interface mac")
-    },
+    VIRSH_COMMON_OPT_INTERFACE(N_("interface mac"),
+                               VIR_CONNECT_LIST_INTERFACES_ACTIVE |
+                               VIRSH_INTERFACE_COMPLETER_MAC),
     {.name = NULL}
 };
 
-- 
2.26.0


Re: [libvirt] [PATCH 08/19] virsh: Add interface mac completion to iface-name command
Posted by Michal Privoznik 5 years, 3 months ago
On 11/2/20 9:26 AM, Lin Ma wrote:
> Signed-off-by: Lin Ma <lma@suse.com>
> ---
>   tools/virsh-completer-interface.c | 13 +++++++++----
>   tools/virsh-completer-interface.h |  4 ++++
>   tools/virsh-interface.c           |  8 +++-----
>   3 files changed, 16 insertions(+), 9 deletions(-)
> 
> diff --git a/tools/virsh-completer-interface.c b/tools/virsh-completer-interface.c
> index 777bb22b0b..c24a2cea6c 100644
> --- a/tools/virsh-completer-interface.c
> +++ b/tools/virsh-completer-interface.c
> @@ -38,7 +38,8 @@ virshInterfaceCompleter(vshControl *ctl,
>       VIR_AUTOSTRINGLIST tmp = NULL;
>   
>       virCheckFlags(VIR_CONNECT_LIST_INTERFACES_ACTIVE |
> -                  VIR_CONNECT_LIST_INTERFACES_INACTIVE,
> +                  VIR_CONNECT_LIST_INTERFACES_INACTIVE |
> +                  VIRSH_INTERFACE_COMPLETER_MAC,
>                     NULL);
>   
>       if (!priv->conn || virConnectIsAlive(priv->conn) <= 0)
> @@ -50,9 +51,13 @@ virshInterfaceCompleter(vshControl *ctl,
>       tmp = g_new0(char *, nifaces + 1);
>   
>       for (i = 0; i < nifaces; i++) {
> -        const char *name = virInterfaceGetName(ifaces[i]);
> -
> -        tmp[i] = g_strdup(name);
> +        if (!(flags & VIRSH_INTERFACE_COMPLETER_MAC)) {
> +            const char *name = virInterfaceGetName(ifaces[i]);
> +            tmp[i] = g_strdup(name);
> +        } else {
> +            const char *mac = virInterfaceGetMACString(ifaces[i]);
> +            tmp[i] = g_strdup(mac);
> +        }
>       }
>   
>       ret = g_steal_pointer(&tmp);
> diff --git a/tools/virsh-completer-interface.h b/tools/virsh-completer-interface.h
> index 2b382222d7..71bc44c4b9 100644
> --- a/tools/virsh-completer-interface.h
> +++ b/tools/virsh-completer-interface.h
> @@ -22,6 +22,10 @@
>   
>   #include "vsh.h"
>   
> +enum {
> +    VIRSH_INTERFACE_COMPLETER_MAC = 1 << 0,
> +};

This is not correct. VIRSH_INTERFACE_COMPLETER_MAC has value of 1 after 
this. But so does VIR_CONNECT_LIST_INTERFACES_INACTIVE and therefore 
there is no way to differentiate the two. After this commit 'virsh 
iface-start --interface' starts printing MAC addresses instead of names.

If you accept my advice in 04/19 you can avoid this new flag completely.

Michal