[libvirt] [PATCH v2 1/9] virsh: Introduce virshStoragePoolNameCompleter

Michal Privoznik posted 8 patches 7 years, 4 months ago
[libvirt] [PATCH v2 1/9] virsh: Introduce virshStoragePoolNameCompleter
Posted by Michal Privoznik 7 years, 3 months ago
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---

Diff to v1:
- Pruned the list of flags accepted in the callback
- Pools for vol_* are ACTIVE only
- Other small nits raised in the review

 tools/virsh-completer.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 tools/virsh-completer.h |  4 ++++
 tools/virsh-pool.c      | 28 ++++++++++++++--------------
 tools/virsh-volume.c    | 10 +++++++---
 tools/virsh.h           |  6 ++++--
 5 files changed, 77 insertions(+), 19 deletions(-)

diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c
index 48dd9fbc2..947c326fc 100644
--- a/tools/virsh-completer.c
+++ b/tools/virsh-completer.c
@@ -147,3 +147,51 @@ virshDomainInterfaceCompleter(vshControl *ctl,
     virStringListFree(ret);
     return NULL;
 }
+
+
+char **
+virshStoragePoolNameCompleter(vshControl *ctl,
+                              const vshCmd *cmd ATTRIBUTE_UNUSED,
+                              unsigned int flags)
+{
+    virshControlPtr priv = ctl->privData;
+    virStoragePoolPtr *pools = NULL;
+    int npools = 0;
+    size_t i = 0;
+    char **ret = NULL;
+
+    virCheckFlags(VIR_CONNECT_LIST_STORAGE_POOLS_INACTIVE |
+                  VIR_CONNECT_LIST_STORAGE_POOLS_ACTIVE |
+                  VIR_CONNECT_LIST_STORAGE_POOLS_PERSISTENT,
+                  NULL);
+
+    if (!priv->conn || virConnectIsAlive(priv->conn) <= 0)
+        return NULL;
+
+    if ((npools = virConnectListAllStoragePools(priv->conn, &pools, flags)) < 0)
+        return NULL;
+
+    if (VIR_ALLOC_N(ret, npools + 1) < 0)
+        goto error;
+
+    for (i = 0; i < npools; i++) {
+        const char *name = virStoragePoolGetName(pools[i]);
+
+        if (VIR_STRDUP(ret[i], name) < 0)
+            goto error;
+
+        virStoragePoolFree(pools[i]);
+    }
+    VIR_FREE(pools);
+
+    return ret;
+
+ error:
+    for (; i < npools; i++)
+        virStoragePoolFree(pools[i]);
+    VIR_FREE(pools);
+    for (i = 0; i < npools; i++)
+        VIR_FREE(ret[i]);
+    VIR_FREE(ret);
+    return NULL;
+}
diff --git a/tools/virsh-completer.h b/tools/virsh-completer.h
index 1a2dd685f..249e793b9 100644
--- a/tools/virsh-completer.h
+++ b/tools/virsh-completer.h
@@ -38,4 +38,8 @@ char ** virshDomainInterfaceCompleter(vshControl *ctl,
                                       const vshCmd *cmd,
                                       unsigned int flags);
 
+char ** virshStoragePoolNameCompleter(vshControl *ctl,
+                                      const vshCmd *cmd,
+                                      unsigned int flags);
+
 #endif
diff --git a/tools/virsh-pool.c b/tools/virsh-pool.c
index 094874b64..56b6cfc73 100644
--- a/tools/virsh-pool.c
+++ b/tools/virsh-pool.c
@@ -34,8 +34,8 @@
 #include "virstring.h"
 #include "virtime.h"
 
-#define VIRSH_COMMON_OPT_POOL_FULL \
-    VIRSH_COMMON_OPT_POOL(N_("pool name or uuid"))
+#define VIRSH_COMMON_OPT_POOL_FULL(cflags) \
+    VIRSH_COMMON_OPT_POOL(N_("pool name or uuid"), cflags)
 
 #define VIRSH_COMMON_OPT_POOL_BUILD \
     {.name = "build", \
@@ -182,7 +182,7 @@ static const vshCmdInfo info_pool_autostart[] = {
 };
 
 static const vshCmdOptDef opts_pool_autostart[] = {
-    VIRSH_COMMON_OPT_POOL_FULL,
+    VIRSH_COMMON_OPT_POOL_FULL(VIR_CONNECT_LIST_STORAGE_POOLS_PERSISTENT),
 
     {.name = "disable",
      .type = VSH_OT_BOOL,
@@ -575,7 +575,7 @@ static const vshCmdInfo info_pool_build[] = {
 };
 
 static const vshCmdOptDef opts_pool_build[] = {
-    VIRSH_COMMON_OPT_POOL_FULL,
+    VIRSH_COMMON_OPT_POOL_FULL(0),
     VIRSH_COMMON_OPT_POOL_NO_OVERWRITE,
     VIRSH_COMMON_OPT_POOL_OVERWRITE,
 
@@ -625,7 +625,7 @@ static const vshCmdInfo info_pool_destroy[] = {
 };
 
 static const vshCmdOptDef opts_pool_destroy[] = {
-    VIRSH_COMMON_OPT_POOL_FULL,
+    VIRSH_COMMON_OPT_POOL_FULL(VIR_CONNECT_LIST_STORAGE_POOLS_ACTIVE),
 
     {.name = NULL}
 };
@@ -665,7 +665,7 @@ static const vshCmdInfo info_pool_delete[] = {
 };
 
 static const vshCmdOptDef opts_pool_delete[] = {
-    VIRSH_COMMON_OPT_POOL_FULL,
+    VIRSH_COMMON_OPT_POOL_FULL(VIR_CONNECT_LIST_STORAGE_POOLS_INACTIVE),
 
     {.name = NULL}
 };
@@ -705,7 +705,7 @@ static const vshCmdInfo info_pool_refresh[] = {
 };
 
 static const vshCmdOptDef opts_pool_refresh[] = {
-    VIRSH_COMMON_OPT_POOL_FULL,
+    VIRSH_COMMON_OPT_POOL_FULL(0),
 
     {.name = NULL}
 };
@@ -745,7 +745,7 @@ static const vshCmdInfo info_pool_dumpxml[] = {
 };
 
 static const vshCmdOptDef opts_pool_dumpxml[] = {
-    VIRSH_COMMON_OPT_POOL_FULL,
+    VIRSH_COMMON_OPT_POOL_FULL(0),
 
     {.name = "inactive",
      .type = VSH_OT_BOOL,
@@ -1636,7 +1636,7 @@ static const vshCmdInfo info_pool_info[] = {
 };
 
 static const vshCmdOptDef opts_pool_info[] = {
-    VIRSH_COMMON_OPT_POOL_FULL,
+    VIRSH_COMMON_OPT_POOL_FULL(0),
 
     {.name = "bytes",
      .type = VSH_OT_BOOL,
@@ -1726,7 +1726,7 @@ static const vshCmdInfo info_pool_name[] = {
 };
 
 static const vshCmdOptDef opts_pool_name[] = {
-    VIRSH_COMMON_OPT_POOL_FULL,
+    VIRSH_COMMON_OPT_POOL_FULL(0),
 
     {.name = NULL}
 };
@@ -1758,7 +1758,7 @@ static const vshCmdInfo info_pool_start[] = {
 };
 
 static const vshCmdOptDef opts_pool_start[] = {
-    VIRSH_COMMON_OPT_POOL_FULL,
+    VIRSH_COMMON_OPT_POOL_FULL(VIR_CONNECT_LIST_STORAGE_POOLS_INACTIVE),
     VIRSH_COMMON_OPT_POOL_BUILD,
     VIRSH_COMMON_OPT_POOL_NO_OVERWRITE,
     VIRSH_COMMON_OPT_POOL_OVERWRITE,
@@ -1819,7 +1819,7 @@ static const vshCmdInfo info_pool_undefine[] = {
 };
 
 static const vshCmdOptDef opts_pool_undefine[] = {
-    VIRSH_COMMON_OPT_POOL_FULL,
+    VIRSH_COMMON_OPT_POOL_FULL(VIR_CONNECT_LIST_STORAGE_POOLS_PERSISTENT),
 
     {.name = NULL}
 };
@@ -1859,7 +1859,7 @@ static const vshCmdInfo info_pool_uuid[] = {
 };
 
 static const vshCmdOptDef opts_pool_uuid[] = {
-    VIRSH_COMMON_OPT_POOL_FULL,
+    VIRSH_COMMON_OPT_POOL_FULL(0),
 
     {.name = NULL}
 };
@@ -1896,7 +1896,7 @@ static const vshCmdInfo info_pool_edit[] = {
 };
 
 static const vshCmdOptDef opts_pool_edit[] = {
-    VIRSH_COMMON_OPT_POOL_FULL,
+    VIRSH_COMMON_OPT_POOL_FULL(0),
 
     {.name = NULL}
 };
diff --git a/tools/virsh-volume.c b/tools/virsh-volume.c
index 8265a3979..bfbb7c7d7 100644
--- a/tools/virsh-volume.c
+++ b/tools/virsh-volume.c
@@ -44,15 +44,19 @@
 #include "virstring.h"
 
 #define VIRSH_COMMON_OPT_POOL_FULL \
-    VIRSH_COMMON_OPT_POOL(N_("pool name or uuid"))
+    VIRSH_COMMON_OPT_POOL(N_("pool name or uuid"), \
+                          VIR_CONNECT_LIST_STORAGE_POOLS_ACTIVE)
 
 #define VIRSH_COMMON_OPT_POOL_NAME \
-    VIRSH_COMMON_OPT_POOL(N_("pool name"))
+    VIRSH_COMMON_OPT_POOL(N_("pool name"), \
+                          VIR_CONNECT_LIST_STORAGE_POOLS_ACTIVE)
 
 #define VIRSH_COMMON_OPT_POOL_OPTIONAL \
     {.name = "pool", \
      .type = VSH_OT_STRING, \
-     .help = N_("pool name or uuid") \
+     .help = N_("pool name or uuid"), \
+     .completer = virshStoragePoolNameCompleter, \
+     .completer_flags = VIR_CONNECT_LIST_STORAGE_POOLS_ACTIVE, \
     }
 
 #define VIRSH_COMMON_OPT_VOLUME_VOL \
diff --git a/tools/virsh.h b/tools/virsh.h
index 528e04558..f2213ebb5 100644
--- a/tools/virsh.h
+++ b/tools/virsh.h
@@ -64,11 +64,13 @@
 /*
  * Common command options
  */
-# define VIRSH_COMMON_OPT_POOL(_helpstr) \
+# define VIRSH_COMMON_OPT_POOL(_helpstr, cflags) \
     {.name = "pool", \
      .type = VSH_OT_DATA, \
      .flags = VSH_OFLAG_REQ, \
-     .help = _helpstr \
+     .help = _helpstr, \
+     .completer = virshStoragePoolNameCompleter, \
+     .completer_flags = cflags, \
     }
 
 # define VIRSH_COMMON_OPT_DOMAIN(_helpstr, cflags) \
-- 
2.13.6

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v2 1/9] virsh: Introduce virshStoragePoolNameCompleter
Posted by John Ferlan 7 years, 3 months ago

On 01/24/2018 03:50 AM, Michal Privoznik wrote:
> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
> ---
> 
> Diff to v1:
> - Pruned the list of flags accepted in the callback
> - Pools for vol_* are ACTIVE only
> - Other small nits raised in the review
> 
>  tools/virsh-completer.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
>  tools/virsh-completer.h |  4 ++++
>  tools/virsh-pool.c      | 28 ++++++++++++++--------------
>  tools/virsh-volume.c    | 10 +++++++---
>  tools/virsh.h           |  6 ++++--
>  5 files changed, 77 insertions(+), 19 deletions(-)
> 

Reviewed-by: John Ferlan <jferlan@redhat.com>

John

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
[libvirt] [PATCH v2 9/9] docs: Mention just implemented completers
Posted by Michal Privoznik 7 years, 3 months ago
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---

Diff to v1:
- Adapted to new release

 docs/news.xml | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/docs/news.xml b/docs/news.xml
index b4d980624..71305c203 100644
--- a/docs/news.xml
+++ b/docs/news.xml
@@ -37,6 +37,16 @@
     <section title="New features">
     </section>
     <section title="Improvements">
+      <change>
+        <summary>
+          virsh: Enhance bash completion
+        </summary>
+        <description>
+          Implement more bash completions so that basic libvirt
+          objects can be autocompleted (e.g. networks,
+          interfaces, NWFilters, and so on).
+        </description>
+      </change>
     </section>
     <section title="Bug fixes">
     </section>
-- 
2.13.6

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v2 9/9] docs: Mention just implemented completers
Posted by John Ferlan 7 years, 3 months ago

On 01/24/2018 03:50 AM, Michal Privoznik wrote:
> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
> ---
> 
> Diff to v1:
> - Adapted to new release
> 
>  docs/news.xml | 10 ++++++++++
>  1 file changed, 10 insertions(+)
> 

Would you say that the auto completion being added in this release adds
the lookup by name on various commands. Whether you want to be that
precise is up to you...

Reviewed-by: John Ferlan <jferlan@redhat.com>

John

> diff --git a/docs/news.xml b/docs/news.xml
> index b4d980624..71305c203 100644
> --- a/docs/news.xml
> +++ b/docs/news.xml
> @@ -37,6 +37,16 @@
>      <section title="New features">
>      </section>
>      <section title="Improvements">
> +      <change>
> +        <summary>
> +          virsh: Enhance bash completion
> +        </summary>
> +        <description>
> +          Implement more bash completions so that basic libvirt
> +          objects can be autocompleted (e.g. networks,

my email client spell checker says "auto completed" or "auto-completed"
(IDC, but figured I'd note it).

> +          interfaces, NWFilters, and so on).
> +        </description>
> +      </change>
>      </section>
>      <section title="Bug fixes">
>      </section>
> 

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list