[libvirt] [PATCH] virsh: Improve editing

Michal Privoznik mprivozn at redhat.com
Fri May 13 10:34:25 UTC 2011


When users (pool-/net-)edit and they make a mistake, temporary file
and thus all changes are gone. Better way is to let them decide if
they want to get back to edit and correct what's wrong.
However, this is suitable only in interactive mode.
---
 tools/virsh.c |   42 ++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/tools/virsh.c b/tools/virsh.c
index fbeb7c8..0dccc17 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -338,6 +338,7 @@ static const char *vshDomainStateToString(int state);
 static const char *vshDomainVcpuStateToString(int state);
 static bool vshConnectionUsability(vshControl *ctl, virConnectPtr conn);
 
+static char * vshReadline (vshControl *ctl, const char *prompt);
 static char *editWriteToTempFile (vshControl *ctl, const char *doc);
 static int   editFile (vshControl *ctl, const char *filename);
 static char *editReadBackFile (vshControl *ctl, const char *filename);
@@ -10099,6 +10100,7 @@ cmdEdit (vshControl *ctl, const vshCmd *cmd)
     char *doc_edited = NULL;
     char *doc_reread = NULL;
     int flags = VIR_DOMAIN_XML_SECURE | VIR_DOMAIN_XML_INACTIVE;
+    char *response;
 
     if (!vshConnectionUsability(ctl, ctl->conn))
         goto cleanup;
@@ -10116,6 +10118,7 @@ cmdEdit (vshControl *ctl, const vshCmd *cmd)
     tmp = editWriteToTempFile (ctl, doc);
     if (!tmp) goto cleanup;
 
+again:
     /* Start the editor. */
     if (editFile (ctl, tmp) == -1) goto cleanup;
 
@@ -10148,8 +10151,43 @@ cmdEdit (vshControl *ctl, const vshCmd *cmd)
     /* Everything checks out, so redefine the domain. */
     virDomainFree (dom);
     dom = virDomainDefineXML (ctl->conn, doc_edited);
-    if (!dom)
-        goto cleanup;
+    if (!dom) {
+        if (ctl->imode) {
+            virshReportError(ctl);
+            response = vshReadline(ctl, "Do you want to edit again [Y/n] ");
+            while (true) {
+                char *c = response;
+                while (c && *c) {
+                    *c = tolower(*c);
+                    c++;
+                }
+
+                if (STREQ(response, "") || STREQ(response, "y") ||
+                    STREQ(response, "yes")) {
+                        dom = vshCommandOptDomain (ctl, cmd, NULL);
+                        if (!dom)
+                            goto cleanup;
+                        goto again;
+                } else if (!response || STREQ(response, "n") ||
+                           STREQ(response, "no")) {
+                    /* Ok, this is a little hack. Command did not succeed,
+                     * but the error was already reported. So we do not
+                     * want it to get reported twice */
+                    ret = true;
+                    goto cleanup;
+                } else {
+                    char *prompt;
+                    if (virAsprintf(&prompt, _("Sorry, response '%s' not "
+                        "understood. [Y/n] "), response) < 0)
+                        goto cleanup;
+                    response = vshReadline(ctl, prompt);
+                    VIR_FREE(prompt);
+                }
+            }
+        } else {
+            goto cleanup;
+        }
+    }
 
     vshPrint (ctl, _("Domain %s XML configuration edited.\n"),
               virDomainGetName(dom));
-- 
1.7.5.rc3




More information about the libvir-list mailing list