From: Wolfgang Bumiller <w.bumiller@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH common] schema: parse_config: optionally collect comments
Date: Thu, 17 Mar 2022 11:26:03 +0100 [thread overview]
Message-ID: <20220317102603.49426-4-w.bumiller@proxmox.com> (raw)
In-Reply-To: <20220317102603.49426-1-w.bumiller@proxmox.com>
Signed-off-by: Wolfgang Bumiller <w.bumiller@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
next prev parent reply other threads:[~2022-03-17 10:26 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-03-17 10:26 [pve-devel] [PATCH manager+common 0/2] drop custom node config parser Wolfgang Bumiller
2022-03-17 10:26 ` [pve-devel] [PATCH manager 1/2] nodeconfig: use common " Wolfgang Bumiller
2022-03-17 13:27 ` [pve-devel] applied: " Thomas Lamprecht
2022-03-17 10:26 ` [pve-devel] [PATCH manager 2/2] api: fill in nodeconfig schema Wolfgang Bumiller
2022-03-17 13:28 ` [pve-devel] applied: " Thomas Lamprecht
2022-03-17 10:26 ` Wolfgang Bumiller [this message]
2022-03-17 11:27 ` [pve-devel] applied: [PATCH common] schema: parse_config: optionally collect comments 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=20220317102603.49426-4-w.bumiller@proxmox.com \
--to=w.bumiller@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