On 2/28/24 16:51, Ján Tomko wrote:
Remove some code repetition between desc and net-desc commands.
Signed-off-by: Ján Tomko <jtomko(a)redhat.com>
---
tools/virsh-domain.c | 20 +-------------------
tools/virsh-network.c | 19 +------------------
tools/vsh.c | 27 +++++++++++++++++++++++++++
tools/vsh.h | 1 +
4 files changed, 30 insertions(+), 37 deletions(-)
diff --git a/tools/vsh.c b/tools/vsh.c
index 65deaa77e8..0543ba299f 100644
--- a/tools/vsh.c
+++ b/tools/vsh.c
@@ -2506,6 +2506,33 @@ vshEditReadBackFile(vshControl *ctl, const char *filename)
return ret;
}
+int
+vshEditString(vshControl *ctl, char **output, const char *string)
One argument per line please.
+{
+ 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. */
Michal