mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-03 03:25:20 +00:00
vsh: introduce vshEditString
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>
This commit is contained in:
parent
3b31aeaae3
commit
47a8f6a99b
@ -8508,29 +8508,11 @@ cmdDesc(vshControl *ctl, const vshCmd *cmd)
|
|||||||
descArg = g_strdup(descDom);
|
descArg = g_strdup(descDom);
|
||||||
|
|
||||||
if (edit) {
|
if (edit) {
|
||||||
g_autoptr(vshTempFile) tmp = NULL;
|
|
||||||
g_autofree char *desc_edited = NULL;
|
g_autofree char *desc_edited = NULL;
|
||||||
char *tmpstr;
|
|
||||||
|
|
||||||
/* Create and open the temporary file. */
|
if (vshEditString(ctl, &desc_edited, descArg) < 0)
|
||||||
if (!(tmp = vshEditWriteToTempFile(ctl, descArg)))
|
|
||||||
return false;
|
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? */
|
/* Compare original XML with edited. Has it changed at all? */
|
||||||
if (STREQ(descDom, desc_edited)) {
|
if (STREQ(descDom, desc_edited)) {
|
||||||
if (title)
|
if (title)
|
||||||
|
@ -462,29 +462,12 @@ cmdNetworkDesc(vshControl *ctl, const vshCmd *cmd)
|
|||||||
descArg = g_strdup(descNet);
|
descArg = g_strdup(descNet);
|
||||||
|
|
||||||
if (edit) {
|
if (edit) {
|
||||||
g_autoptr(vshTempFile) tmp = NULL;
|
|
||||||
g_autofree char *desc_edited = NULL;
|
g_autofree char *desc_edited = NULL;
|
||||||
char *tmpstr;
|
|
||||||
|
|
||||||
/* Create and open the temporary file. */
|
/* Create and open the temporary file. */
|
||||||
if (!(tmp = vshEditWriteToTempFile(ctl, descArg)))
|
if (vshEditString(ctl, &desc_edited, descArg) < 0)
|
||||||
return false;
|
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? */
|
/* Compare original XML with edited. Has it changed at all? */
|
||||||
if (STREQ(descNet, desc_edited)) {
|
if (STREQ(descNet, desc_edited)) {
|
||||||
if (title)
|
if (title)
|
||||||
|
29
tools/vsh.c
29
tools/vsh.c
@ -2506,6 +2506,35 @@ vshEditReadBackFile(vshControl *ctl, const char *filename)
|
|||||||
return ret;
|
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. */
|
/* Tree listing helpers. */
|
||||||
|
|
||||||
|
@ -350,6 +350,7 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(vshTempFile, vshEditUnlinkTempfile);
|
|||||||
char *vshEditWriteToTempFile(vshControl *ctl, const char *doc);
|
char *vshEditWriteToTempFile(vshControl *ctl, const char *doc);
|
||||||
int vshEditFile(vshControl *ctl, const char *filename);
|
int vshEditFile(vshControl *ctl, const char *filename);
|
||||||
char *vshEditReadBackFile(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);
|
int vshAskReedit(vshControl *ctl, const char *msg, bool relax_avail);
|
||||||
|
|
||||||
/* terminal modifications */
|
/* terminal modifications */
|
||||||
|
Loading…
Reference in New Issue
Block a user