[pve-devel] [pve-common] PVE::CLIFormatter - new 'yaml' renderer
Dietmar Maurer
dietmar at proxmox.com
Thu Jul 26 09:55:19 CEST 2018
Generates nice and readable output for list of string and simple objects.
Signed-off-by: Dietmar Maurer <dietmar at proxmox.com>
---
src/PVE/CLIFormatter.pm | 37 +++++++++++++++++++++++++++++++++++++
1 file changed, 37 insertions(+)
diff --git a/src/PVE/CLIFormatter.pm b/src/PVE/CLIFormatter.pm
index 8bb42ca..0424804 100644
--- a/src/PVE/CLIFormatter.pm
+++ b/src/PVE/CLIFormatter.pm
@@ -81,6 +81,43 @@ sub render_bytes {
PVE::JSONSchema::register_renderer('bytes', \&render_bytes);
+sub render_yaml {
+ my ($value) = @_;
+
+ # YAML like first level (use to_json for deeper levels)
+
+ if (my $class = ref($value)) {
+ if ($class eq 'HASH' && scalar(keys %$value)) {
+ my $res;
+ foreach my $key (keys %$value) {
+ my $el = $value->{$key};
+ if (!defined($el) || ref($el)) {
+ push @$res, "$key: " . to_json($el, { allow_nonref => 1, canonical => 1 });
+ } else {
+ push @$res, "$key: $el";
+ }
+ }
+ return join("\n", @$res);
+ } elsif ($class eq 'ARRAY' && scalar(@$value)) {
+ my $res;
+ foreach my $el (@$value) {
+ if (!defined($el) || ref($el)) {
+ push @$res, "- " . to_json($el, { allow_nonref => 1, canonical => 1 });
+ } else {
+ push @$res, "- $el";
+ }
+ }
+ return join("\n", @$res);
+ } else {
+ return to_json($value, { canonical => 1 });
+ }
+ } else {
+ return "$value";
+ }
+}
+
+PVE::JSONSchema::register_renderer('yaml', \&render_yaml);
+
sub query_terminal_options {
my ($options) = @_;
--
2.11.0
More information about the pve-devel
mailing list