]> xenbits.xensource.com Git - libvirt.git/commitdiff
vsh: introduce vshEditString
authorJán Tomko <jtomko@redhat.com>
Wed, 28 Feb 2024 15:31:35 +0000 (16:31 +0100)
committerJán Tomko <jtomko@redhat.com>
Mon, 11 Mar 2024 15:06:46 +0000 (16:06 +0100)
Remove some code repetition between desc and net-desc commands.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
tools/virsh-domain.c
tools/virsh-network.c
tools/vsh.c
tools/vsh.h

index 2e9510983cd25fe099f4805ec887d55b09d1283f..c134208fd4d9b8e94300966d740daef00ff37f21 100644 (file)
@@ -8508,29 +8508,11 @@ cmdDesc(vshControl *ctl, const vshCmd *cmd)
             descArg = g_strdup(descDom);
 
         if (edit) {
-            g_autoptr(vshTempFile) tmp = NULL;
             g_autofree char *desc_edited = NULL;
-            char *tmpstr;
 
-            /* Create and open the temporary file. */
-            if (!(tmp = vshEditWriteToTempFile(ctl, descArg)))
+            if (vshEditString(ctl, &desc_edited, descArg) < 0)
                 return false;
 
-            /* Start the editor. */
-            if (vshEditFile(ctl, tmp) == -1)
-                return false;
-
-            /* Read back the edited file. */
-            if (!(desc_edited = vshEditReadBackFile(ctl, tmp)))
-                return false;
-
-            /* strip a possible newline at the end of file; some
-             * editors enforce a newline, this makes editing
-             * more convenient */
-            if ((tmpstr = strrchr(desc_edited, '\n')) &&
-                *(tmpstr+1) == '\0')
-                *tmpstr = '\0';
-
             /* Compare original XML with edited.  Has it changed at all? */
             if (STREQ(descDom, desc_edited)) {
                 if (title)
index c676fc603c9664900a4eafaa6392a8c9a610c6e0..399035f8cf698191d57e03f3402d6ec4500400c8 100644 (file)
@@ -462,29 +462,12 @@ cmdNetworkDesc(vshControl *ctl, const vshCmd *cmd)
             descArg = g_strdup(descNet);
 
         if (edit) {
-            g_autoptr(vshTempFile) tmp = NULL;
             g_autofree char *desc_edited = NULL;
-            char *tmpstr;
 
             /* Create and open the temporary file. */
-            if (!(tmp = vshEditWriteToTempFile(ctl, descArg)))
+            if (vshEditString(ctl, &desc_edited, descArg) < 0)
                 return false;
 
-            /* Start the editor. */
-            if (vshEditFile(ctl, tmp) == -1)
-                return false;
-
-            /* Read back the edited file. */
-            if (!(desc_edited = vshEditReadBackFile(ctl, tmp)))
-                return false;
-
-            /* strip a possible newline at the end of file; some
-             * editors enforce a newline, this makes editing the title
-             * more convenient */
-            if ((tmpstr = strrchr(desc_edited, '\n')) &&
-                *(tmpstr+1) == '\0')
-                *tmpstr = '\0';
-
             /* Compare original XML with edited.  Has it changed at all? */
             if (STREQ(descNet, desc_edited)) {
                 if (title)
index 65deaa77e82dde0c8747f32070a267c9b583ca35..4fe4d9166fa5872454d10f83371cd02fded905d1 100644 (file)
@@ -2506,6 +2506,35 @@ vshEditReadBackFile(vshControl *ctl, const char *filename)
     return ret;
 }
 
+int
+vshEditString(vshControl *ctl,
+              char **output,
+              const char *string)
+{
+    g_autoptr(vshTempFile) tmp = NULL;
+    char *tmpstr;
+
+    /* Create and open the temporary file. */
+    if (!(tmp = vshEditWriteToTempFile(ctl, string)))
+        return -1;
+
+    /* Start the editor. */
+    if (vshEditFile(ctl, tmp) == -1)
+        return -1;
+
+    /* Read back the edited file. */
+    if (!(*output = vshEditReadBackFile(ctl, tmp)))
+        return -1;
+
+    /* strip a possible newline at the end of file; some
+     * editors enforce a newline, this makes editing
+     * more convenient */
+    if ((tmpstr = strrchr(*output, '\n')) &&
+        *(tmpstr+1) == '\0')
+        *tmpstr = '\0';
+
+    return 0;
+}
 
 /* Tree listing helpers.  */
 
index 2a1be29b1c7bab7219f8568a12dbe3d609ab5200..6f8e3895b7c87ac06fa5b7cc4255a6c3ee82d6b2 100644 (file)
@@ -350,6 +350,7 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(vshTempFile, vshEditUnlinkTempfile);
 char *vshEditWriteToTempFile(vshControl *ctl, const char *doc);
 int vshEditFile(vshControl *ctl, const char *filename);
 char *vshEditReadBackFile(vshControl *ctl, const char *filename);
+int vshEditString(vshControl *ctl, char **output, const char *string);
 int vshAskReedit(vshControl *ctl, const char *msg, bool relax_avail);
 
 /* terminal modifications */