From 2f85ab8fde6f6cd667cc3f4c0272cd5092cd5d9b Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Thu, 17 Mar 2022 11:26:03 +0100 Subject: [PATCH] schema: parse_config: optionally collect comments Signed-off-by: Wolfgang Bumiller --- 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.39.2