[pve-devel] [PATCH common] schema: parse_config: optionally collect comments

Wolfgang Bumiller w.bumiller at proxmox.com
Thu Mar 17 11:26:03 CET 2022


Signed-off-by: Wolfgang Bumiller <w.bumiller at proxmox.com>
---
 src/PVE/JSONSchema.pm | 24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)

diff --git a/src/PVE/JSONSchema.pm b/src/PVE/JSONSchema.pm
index 65055e0..2caf109 100644
--- a/src/PVE/JSONSchema.pm
+++ b/src/PVE/JSONSchema.pm
@@ -1823,8 +1823,8 @@ sub get_options {
 }
 
 # A way to parse configuration data by giving a json schema
-sub parse_config {
-    my ($schema, $filename, $raw) = @_;
+sub parse_config : prototype($$$;$) {
+    my ($schema, $filename, $raw, $comment_key) = @_;
 
     # do fast check (avoid validate_schema($schema))
     die "got strange schema" if !$schema->{type} ||
@@ -1832,10 +1832,24 @@ sub parse_config {
 
     my $cfg = {};
 
+    my $comment_data;
+    my $handle_comment = sub { $_[0] =~ /^#/ };
+    if (defined($comment_key)) {
+	$comment_data = '';
+	my $comment_re = qr/^\Q$comment_key\E:\s*(.*\S)\s*$/;
+	$handle_comment = sub {
+	    if ($_[0] =~ /^\#(.*)\s*$/ || $_[0] =~ $comment_re) {
+		$comment_data .= PVE::Tools::decode_text($1) . "\n";
+		return 1;
+	    }
+	    return undef;
+	};
+    }
+
     while ($raw =~ /^\s*(.+?)\s*$/gm) {
 	my $line = $1;
 
-	next if $line =~ /^#/;
+	next if $handle_comment->($line);
 
 	if ($line =~ m/^(\S+?):\s*(.*)$/) {
 	    my $key = $1;
@@ -1851,6 +1865,10 @@ sub parse_config {
 	}
     }
 
+    if (defined($comment_data)) {
+	$cfg->{$comment_key} = $comment_data;
+    }
+
     my $errors = {};
     check_prop($cfg, $schema, '', $errors);
 
-- 
2.30.2






More information about the pve-devel mailing list