Re: [PATCH v2 11/12] virsh: add helper for returning event name string

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 05/15/2018 11:32 AM, Lin Ma wrote:
> 
> 

>>> (btw, there are other 'vshEventCallbacks' array which defining in
>>> virsh-secret.c,
>>> virsh-pool.c and virsh-nodedev.c.we only move the vshEventCallbacks and
>>> related
>>> data structures from virsh-domain.c?)
>> We can have virshSecretEventToString(), virshPoolEventToString(), ...
>> exposed and used in completers.
> The function names about *EventToString are already occupied, 

Oh, we can't just export them. Not because of they already exist but
because they don't really do what we expect them to do. At least
virshSecretEventToString() doesn't. Anyway, I'm including a patch that
does what I'm trying to say.

Also, virshSecretEventToString() should be renamed to
virshSecretLifecycleEventToString().

Michal
diff --git i/tools/virsh-completer.c w/tools/virsh-completer.c
index e3b8234b41..875dfe71df 100644
--- i/tools/virsh-completer.c
+++ w/tools/virsh-completer.c
@@ -26,6 +26,7 @@
 #include "virsh.h"
 #include "virsh-pool.h"
 #include "virsh-util.h"
+#include "virsh-secret.h"
 #include "internal.h"
 #include "viralloc.h"
 #include "virstring.h"
@@ -522,3 +523,29 @@ virshSnapshotNameCompleter(vshControl *ctl,
     virshDomainFree(dom);
     return NULL;
 }
+
+
+char **
+virshSecretEventNameCompleter(vshControl *ctl ATTRIBUTE_UNUSED,
+                              const vshCmd *cmd ATTRIBUTE_UNUSED,
+                              unsigned int flags)
+{
+    size_t i;
+    char **ret = NULL;
+
+    virCheckFlags(0, NULL);
+
+    if (VIR_ALLOC_N(ret, VIR_SECRET_EVENT_ID_LAST) < 0)
+        goto error;
+
+    for (i = 0; i < VIR_SECRET_EVENT_ID_LAST; i++) {
+        if (VIR_STRDUP(ret[i], vshSecretEventCallbacks[i].name) < 0)
+            goto error;
+    }
+
+    return ret;
+
+ error:
+    virStringListFree(ret);
+    return NULL;
+}
diff --git i/tools/virsh-completer.h w/tools/virsh-completer.h
index fa443d3ad7..b4c5d7aeab 100644
--- i/tools/virsh-completer.h
+++ w/tools/virsh-completer.h
@@ -70,4 +70,7 @@ char ** virshSnapshotNameCompleter(vshControl *ctl,
                                    const vshCmd *cmd,
                                    unsigned int flags);
 
+char ** virshSecretEventNameCompleter(vshControl *ctl,
+                                      const vshCmd *cmd,
+                                      unsigned int flags);
 #endif
diff --git i/tools/virsh-secret.c w/tools/virsh-secret.c
index 9e4ec61a88..6ec2beca23 100644
--- i/tools/virsh-secret.c
+++ w/tools/virsh-secret.c
@@ -571,18 +571,12 @@ virshSecretEventToString(int event)
     return str ? _(str) : _("unknown");
 }
 
-struct vshEventCallback {
-    const char *name;
-    virConnectSecretEventGenericCallback cb;
-};
-typedef struct vshEventCallback vshEventCallback;
-
 struct virshSecretEventData {
     vshControl *ctl;
     bool loop;
     bool timestamp;
     int count;
-    vshEventCallback *cb;
+    vshSecretEventCallback *cb;
 };
 typedef struct virshSecretEventData virshSecretEventData;
 
@@ -652,7 +646,7 @@ vshEventGenericPrint(virConnectPtr conn ATTRIBUTE_UNUSED,
         vshEventDone(data->ctl);
 }
 
-static vshEventCallback vshEventCallbacks[] = {
+vshSecretEventCallback vshSecretEventCallbacks[] = {
     { "lifecycle",
       VIR_SECRET_EVENT_CALLBACK(vshEventLifecyclePrint), },
     { "value-changed", vshEventGenericPrint, },
@@ -676,6 +670,7 @@ static const vshCmdOptDef opts_secret_event[] = {
     },
     {.name = "event",
      .type = VSH_OT_STRING,
+     .completer = virshSecretEventNameCompleter,
      .help = N_("which event type to wait for")
     },
     {.name = "loop",
@@ -713,7 +708,7 @@ cmdSecretEvent(vshControl *ctl, const vshCmd *cmd)
         size_t i;
 
         for (i = 0; i < VIR_SECRET_EVENT_ID_LAST; i++)
-            vshPrint(ctl, "%s\n", vshEventCallbacks[i].name);
+            vshPrint(ctl, "%s\n", vshSecretEventCallbacks[i].name);
         return true;
     }
 
@@ -724,7 +719,7 @@ cmdSecretEvent(vshControl *ctl, const vshCmd *cmd)
         return false;
     }
     for (event = 0; event < VIR_SECRET_EVENT_ID_LAST; event++)
-        if (STREQ(eventName, vshEventCallbacks[event].name))
+        if (STREQ(eventName, vshSecretEventCallbacks[event].name))
             break;
     if (event == VIR_SECRET_EVENT_ID_LAST) {
         vshError(ctl, _("unknown event type %s"), eventName);
@@ -735,7 +730,7 @@ cmdSecretEvent(vshControl *ctl, const vshCmd *cmd)
     data.loop = vshCommandOptBool(cmd, "loop");
     data.timestamp = vshCommandOptBool(cmd, "timestamp");
     data.count = 0;
-    data.cb = &vshEventCallbacks[event];
+    data.cb = &vshSecretEventCallbacks[event];
     if (vshCommandOptTimeoutToMs(ctl, cmd, &timeout) < 0)
         return false;
 
diff --git i/tools/virsh-secret.h w/tools/virsh-secret.h
index dda22b021e..c70a2b5c75 100644
--- i/tools/virsh-secret.h
+++ w/tools/virsh-secret.h
@@ -28,6 +28,14 @@
 
 # include "virsh.h"
 
+struct vshSecretEventCallback {
+    const char *name;
+    virConnectSecretEventGenericCallback cb;
+};
+typedef struct vshSecretEventCallback vshSecretEventCallback;
+
+extern vshSecretEventCallback vshSecretEventCallbacks[];
+
 extern const vshCmdDef secretCmds[];
 
 #endif /* VIRSH_SECRET_H */
--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux