]> xenbits.xensource.com Git - libvirt.git/commitdiff
virsh: domain: Split out code to lookup domain from string
authorPeter Krempa <pkrempa@redhat.com>
Tue, 26 Aug 2014 12:16:01 +0000 (14:16 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Tue, 26 Aug 2014 20:48:05 +0000 (22:48 +0200)
Split out guts of the function to reuse it to get domain objects from
string.

tools/virsh-domain.c
tools/virsh-domain.h

index fcfbf7430d63bd74a1f30c4c8a7195f61e76b650..befc87a5d72839a9cb33a41ef65f542dfd1770ac 100644 (file)
 # define SA_SIGINFO 0
 #endif
 
-virDomainPtr
-vshCommandOptDomainBy(vshControl *ctl, const vshCmd *cmd,
-                      const char **name, unsigned int flags)
+
+static virDomainPtr
+vshLookupDomainInternal(vshControl *ctl,
+                        const char *cmdname,
+                        const char *name,
+                        unsigned int flags)
 {
     virDomainPtr dom = NULL;
-    const char *n = NULL;
     int id;
-    const char *optname = "domain";
     virCheckFlags(VSH_BYID | VSH_BYUUID | VSH_BYNAME, NULL);
 
-    if (!vshCmdHasOption(ctl, cmd, optname))
-        return NULL;
-
-    if (vshCommandOptStringReq(ctl, cmd, optname, &n) < 0)
-        return NULL;
-
-    vshDebug(ctl, VSH_ERR_INFO, "%s: found option <%s>: %s\n",
-             cmd->def->name, optname, n);
-
-    if (name)
-        *name = n;
-
     /* try it by ID */
     if (flags & VSH_BYID) {
-        if (virStrToLong_i(n, NULL, 10, &id) == 0 && id >= 0) {
-            vshDebug(ctl, VSH_ERR_DEBUG,
-                     "%s: <%s> seems like domain ID\n",
-                     cmd->def->name, optname);
+        if (virStrToLong_i(name, NULL, 10, &id) == 0 && id >= 0) {
+            vshDebug(ctl, VSH_ERR_DEBUG, "%s: <domain> looks like ID\n",
+                     cmdname);
             dom = virDomainLookupByID(ctl->conn, id);
         }
     }
+
     /* try it by UUID */
     if (!dom && (flags & VSH_BYUUID) &&
-        strlen(n) == VIR_UUID_STRING_BUFLEN-1) {
-        vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as domain UUID\n",
-                 cmd->def->name, optname);
-        dom = virDomainLookupByUUIDString(ctl->conn, n);
+        strlen(name) == VIR_UUID_STRING_BUFLEN-1) {
+        vshDebug(ctl, VSH_ERR_DEBUG, "%s: <domain> trying as domain UUID\n",
+                 cmdname);
+        dom = virDomainLookupByUUIDString(ctl->conn, name);
     }
+
     /* try it by NAME */
     if (!dom && (flags & VSH_BYNAME)) {
-        vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as domain NAME\n",
-                 cmd->def->name, optname);
-        dom = virDomainLookupByName(ctl->conn, n);
+        vshDebug(ctl, VSH_ERR_DEBUG, "%s: <domain> trying as domain NAME\n",
+                 cmdname);
+        dom = virDomainLookupByName(ctl->conn, name);
     }
 
     if (!dom)
-        vshError(ctl, _("failed to get domain '%s'"), n);
+        vshError(ctl, _("failed to get domain '%s'"), name);
 
     return dom;
 }
 
+
+virDomainPtr
+vshLookupDomainBy(vshControl *ctl,
+                  const char *name,
+                  unsigned int flags)
+{
+    return vshLookupDomainInternal(ctl, "unknown", name, flags);
+}
+
+
+virDomainPtr
+vshCommandOptDomainBy(vshControl *ctl, const vshCmd *cmd,
+                      const char **name, unsigned int flags)
+{
+    const char *n = NULL;
+    const char *optname = "domain";
+
+    if (!vshCmdHasOption(ctl, cmd, optname))
+        return NULL;
+
+    if (vshCommandOptStringReq(ctl, cmd, optname, &n) < 0)
+        return NULL;
+
+    vshDebug(ctl, VSH_ERR_INFO, "%s: found option <%s>: %s\n",
+             cmd->def->name, optname, n);
+
+    if (name)
+        *name = n;
+
+    return vshLookupDomainInternal(ctl, cmd->def->name, n, flags);
+}
+
 VIR_ENUM_DECL(vshDomainVcpuState)
 VIR_ENUM_IMPL(vshDomainVcpuState,
               VIR_VCPU_LAST,
index f03a0bb291a0b64b247acfadc0311467ae0d1e39..f46538fc69a762cdc45c1f75f6929a2065914c3e 100644 (file)
 
 # include "virsh.h"
 
+virDomainPtr vshLookupDomainBy(vshControl *ctl,
+                               const char *name,
+                               unsigned int flags);
+
 virDomainPtr vshCommandOptDomainBy(vshControl *ctl, const vshCmd *cmd,
                                    const char **name, unsigned int flags);