public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Fabian Ebner <f.ebner@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH/RFC v2 manager 3/3] vzdump: notes-template: replace unknown variable with error string
Date: Tue,  3 May 2022 13:18:00 +0200	[thread overview]
Message-ID: <20220503111800.64511-3-f.ebner@proxmox.com> (raw)
In-Reply-To: <20220503111800.64511-1-f.ebner@proxmox.com>

rather than not setting the notes at all. They still can contain
useful information, and it likely is surprising to users to lose all
of the note when an unkown variable (or simply mistyped) is provided.

Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
---
 PVE/VZDump.pm | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/PVE/VZDump.pm b/PVE/VZDump.pm
index 05cc1bec..79dcd62b 100644
--- a/PVE/VZDump.pm
+++ b/PVE/VZDump.pm
@@ -71,7 +71,7 @@ sub run_command {
 }
 
 my $generate_notes = sub {
-    my ($notes_template, $task) = @_;
+    my ($notes_template, $task, $logfd) = @_;
 
     my $info = {
 	cluster => PVE::Cluster::get_clinfo()->{cluster}->{name},
@@ -89,10 +89,14 @@ my $generate_notes = sub {
 
     $notes_template =~ s/\\(.)/$unescape->($1)/eg;
 
-    my $vars = join('|', keys $info->%*);
-    $notes_template =~ s/\{\{($vars)\}\}/$info->{$1}/g;
+    my $replace_var = sub {
+	my ($var) = @_;
+	return $info->{$var} if exists($info->{$var});
+	debugmsg('warn', "notes-template: unknown variable '$var'", $logfd);
+	return "<unknown variable '$var'>";
+    };
 
-    die "unexpected variable name '$1'" if $notes_template =~ m/\{\{([^\s}]+)\}\}/;
+    $notes_template =~ s/\{\{([^\s}]+)\}\}/$replace_var->($1)/eg;
 
     return $notes_template;
 };
@@ -1045,7 +1049,7 @@ sub exec_backup_task {
 
 	    if ($opts->{'notes-template'} && $opts->{'notes-template'} ne '') {
 		debugmsg('info', "adding notes to backup", $logfd);
-		my $notes = eval { $generate_notes->($opts->{'notes-template'}, $task); };
+		my $notes = eval { $generate_notes->($opts->{'notes-template'}, $task, $logfd); };
 		if (my $err = $@) {
 		    debugmsg('warn', "unable to add notes - $err", $logfd);
 		} else {
-- 
2.30.2





  parent reply	other threads:[~2022-05-03 11:18 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-03 11:17 [pve-devel] [PATCH v2 manager 1/3] vzdump: notes-template: avoid escaping meta-characters upon replace Fabian Ebner
2022-05-03 11:17 ` [pve-devel] [PATCH v2 manager 2/3] vzdump: notes-template: improve check for unknown variable Fabian Ebner
2022-05-04  6:21   ` [pve-devel] applied: " Thomas Lamprecht
2022-05-03 11:18 ` Fabian Ebner [this message]
2022-05-04  6:23   ` [pve-devel] [PATCH/RFC v2 manager 3/3] vzdump: notes-template: replace unknown variable with error string Thomas Lamprecht
2022-05-04  6:20 ` [pve-devel] applied: [PATCH v2 manager 1/3] vzdump: notes-template: avoid escaping meta-characters upon replace Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220503111800.64511-3-f.ebner@proxmox.com \
    --to=f.ebner@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal