]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virsh: Add virshDomainShutdownModeCompleter
authorMichal Privoznik <mprivozn@redhat.com>
Sun, 30 Dec 2018 04:28:04 +0000 (05:28 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Thu, 11 Apr 2019 06:54:34 +0000 (08:54 +0200)
This completer is used to offer shutdown/reboot modes.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Cole Robinson <crobinso@redhat.com>
tools/virsh-completer.c
tools/virsh-completer.h
tools/virsh-domain.c

index aac4579d1fc96b375afdbcf21e5c3484357f1362..918e4098900813aa8ada4473f2af189eecde1b34 100644 (file)
@@ -936,3 +936,59 @@ virshDomainDeviceAliasCompleter(vshControl *ctl,
     VIR_STEAL_PTR(ret, tmp);
     return ret;
 }
+
+
+char **
+virshDomainShutdownModeCompleter(vshControl *ctl,
+                                 const vshCmd *cmd,
+                                 unsigned int flags)
+{
+    const char *modes[] = {"acpi", "agent", "initctl", "signal", "paravirt"};
+    size_t i;
+    char **ret = NULL;
+    size_t ntmp = 0;
+    VIR_AUTOSTRINGLIST tmp = NULL;
+    const char *modeConst = NULL;
+    VIR_AUTOFREE(char *) mode = NULL;
+    VIR_AUTOSTRINGLIST modesSpecified = NULL;
+
+    virCheckFlags(0, NULL);
+
+    if (vshCommandOptStringQuiet(ctl, cmd, "mode", &modeConst) < 0)
+        return NULL;
+
+    if (STREQ_NULLABLE(modeConst, " "))
+        modeConst = NULL;
+
+    if (modeConst) {
+        char *modeTmp = NULL;
+
+        if (VIR_STRDUP(mode, modeConst) < 0)
+            return NULL;
+
+        if ((modeTmp = strrchr(mode, ',')))
+            *modeTmp = '\0';
+        else
+            VIR_FREE(mode);
+    }
+
+    if (mode && !(modesSpecified = virStringSplit(mode, ",", 0)))
+        return NULL;
+
+    if (VIR_ALLOC_N(tmp, ARRAY_CARDINALITY(modes) + 1) < 0)
+        return NULL;
+
+    for (i = 0; i < ARRAY_CARDINALITY(modes); i++) {
+        if (virStringListHasString((const char **)modesSpecified, modes[i]))
+            continue;
+
+        if ((mode && virAsprintf(&tmp[ntmp], "%s,%s", mode, modes[i]) < 0) ||
+            (!mode && VIR_STRDUP(tmp[ntmp], modes[i]) < 0))
+            return NULL;
+
+        ntmp++;
+    }
+
+    VIR_STEAL_PTR(ret, tmp);
+    return ret;
+}
index 2e2e1edafb58691c6f9c8ba711dad925eb4b61e5..ed37a26cc9f462e7ae54d4c0ee19903bef35bff2 100644 (file)
@@ -110,4 +110,8 @@ char ** virshDomainDeviceAliasCompleter(vshControl *ctl,
 char ** virshCellnoCompleter(vshControl *ctl,
                              const vshCmd *cmd,
                              unsigned int flags);
+
+char ** virshDomainShutdownModeCompleter(vshControl *ctl,
+                                         const vshCmd *cmd,
+                                         unsigned int flags);
 #endif /* LIBVIRT_VIRSH_COMPLETER_H */
index da087b74ae92c4ab77c8fdff7d180a8bddb2fa57..b5d44d6909bc31804c5a79370c1eb3d1d1a4bc9f 100644 (file)
@@ -5830,6 +5830,7 @@ static const vshCmdOptDef opts_shutdown[] = {
     VIRSH_COMMON_OPT_DOMAIN_FULL(VIR_CONNECT_LIST_DOMAINS_ACTIVE),
     {.name = "mode",
      .type = VSH_OT_STRING,
+     .completer = virshDomainShutdownModeCompleter,
      .help = N_("shutdown mode: acpi|agent|initctl|signal|paravirt")
     },
     {.name = NULL}
@@ -5914,6 +5915,7 @@ static const vshCmdOptDef opts_reboot[] = {
     VIRSH_COMMON_OPT_DOMAIN_FULL(VIR_CONNECT_LIST_DOMAINS_ACTIVE),
     {.name = "mode",
      .type = VSH_OT_STRING,
+     .completer = virshDomainShutdownModeCompleter,
      .help = N_("shutdown mode: acpi|agent|initctl|signal|paravirt")
     },
     {.name = NULL}