]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/CLIFormatter.pm
bump version to 8.1.2
[pve-common.git] / src / PVE / CLIFormatter.pm
index 4f18fa9e58d4f85d23a6b2637779f01652aa5772..6977fd9978dcace2f4440ed3a900c40770782c1c 100644 (file)
@@ -4,90 +4,27 @@ use strict;
 use warnings;
 
 use I18N::Langinfo;
-use POSIX qw(strftime);
-use CPAN::Meta::YAML; # comes with perl-modules
+use YAML::XS; # supports Dumping JSON::PP::Boolean
+$YAML::XS::Boolean = "JSON::PP";
 
 use PVE::JSONSchema;
 use PVE::PTY;
+use PVE::Format;
 
 use JSON;
 use utf8;
 use Encode;
 
-sub render_timestamp {
-    my ($epoch) = @_;
-
-    # ISO 8601 date format
-    return strftime("%F %H:%M:%S", localtime($epoch));
-}
-
-PVE::JSONSchema::register_renderer('timestamp', \&render_timestamp);
-
-sub render_timestamp_gmt {
-    my ($epoch) = @_;
-
-    # ISO 8601 date format, standard Greenwich time zone
-    return strftime("%F %H:%M:%S", gmtime($epoch));
-}
-
-PVE::JSONSchema::register_renderer('timestamp_gmt', \&render_timestamp_gmt);
-
-sub render_duration {
-    my ($duration_in_seconds) = @_;
-
-    my $text = '';
-    my $rest = $duration_in_seconds;
-
-    my $step = sub {
-       my ($unit, $unitlength) = @_;
-
-       if ((my $v = int($rest/$unitlength)) > 0) {
-           $text .= " " if length($text);
-           $text .= "${v}${unit}";
-           $rest -= $v * $unitlength;
-       }
-    };
-
-    $step->('w', 7*24*3600);
-    $step->('d', 24*3600);
-    $step->('h', 3600);
-    $step->('m', 60);
-    $step->('s', 1);
-
-    return $text;
-}
-
-PVE::JSONSchema::register_renderer('duration', \&render_duration);
-
-sub render_fraction_as_percentage {
-    my ($fraction) = @_;
-
-    return sprintf("%.2f%%", $fraction*100);
-}
-
-PVE::JSONSchema::register_renderer(
-    'fraction_as_percentage', \&render_fraction_as_percentage);
-
-sub render_bytes {
-    my ($value) = @_;
-
-    my @units = qw(B KiB MiB GiB TiB PiB);
-
-    my $max_unit = 0;
-    if ($value > 1023) {
-        $max_unit = int(log($value)/log(1024));
-        $value /= 1024**($max_unit);
-    }
-    my $unit = $units[$max_unit];
-    return sprintf "%.2f $unit", $value;
-}
-
-PVE::JSONSchema::register_renderer('bytes', \&render_bytes);
+PVE::JSONSchema::register_renderer('timestamp', \&PVE::Format::render_timestamp);
+PVE::JSONSchema::register_renderer('timestamp_gmt', \&PVE::Format::render_timestamp_gmt);
+PVE::JSONSchema::register_renderer('duration', \&PVE::Format::render_duration);
+PVE::JSONSchema::register_renderer('fraction_as_percentage', \&PVE::Format::render_fraction_as_percentage);
+PVE::JSONSchema::register_renderer('bytes', \&PVE::Format::render_bytes);
 
 sub render_yaml {
     my ($value) = @_;
 
-    my $data = CPAN::Meta::YAML::Dump($value);
+    my $data = YAML::XS::Dump($value);
     $data =~ s/^---[\n\s]//; # remove yaml marker
 
     return $data;
@@ -162,8 +99,8 @@ sub print_text_table {
     $terminal_opts //= query_terminal_options({});
 
     my $sort_key = $options->{sort_key};
-    my $border = !$options->{noborder};
-    my $header = !$options->{noheader};
+    my $show_border = !$options->{noborder};
+    my $show_header = !$options->{noheader};
 
     my $columns = $terminal_opts->{columns};
     my $utf8 = $terminal_opts->{utf8};
@@ -186,10 +123,7 @@ sub print_text_table {
 
     my $colopts = {};
 
-    my $borderstring_m = '';
-    my $borderstring_b = '';
-    my $borderstring_t = '';
-    my $borderstring_h = '';
+    my $border = { m => '', b => '', t => '', h => '' };
     my $formatstring = '';
 
     my $column_count = scalar(@$props_to_print);
@@ -254,54 +188,54 @@ sub print_text_table {
            cutoff => $cutoff,
        };
 
-       if ($border) {
+       if ($show_border) {
            if ($i == 0 && ($column_count == 1)) {
                if ($utf8) {
                    $formatstring .= "│ %$alignstr${cutoff}s │";
-                   $borderstring_t .= "┌─" . ('─' x $cutoff) . "─┐";
-                   $borderstring_h .= "╞═" . ('═' x $cutoff) . '═╡';
-                   $borderstring_m .= "├─" . ('─' x $cutoff) . "─┤";
-                   $borderstring_b .= "└─" . ('─' x $cutoff) . "─┘";
+                   $border->{t} .= "┌─" . ('─' x $cutoff) . "─┐";
+                   $border->{h} .= "╞═" . ('═' x $cutoff) . '═╡';
+                   $border->{m} .= "├─" . ('─' x $cutoff) . "─┤";
+                   $border->{b} .= "└─" . ('─' x $cutoff) . "─┘";
                } else {
                    $formatstring .= "| %$alignstr${cutoff}s |";
-                   $borderstring_m .= "+-" . ('-' x $cutoff) . "-+";
-                   $borderstring_h .= "+=" . ('=' x $cutoff) . '=';
+                   $border->{m} .= "+-" . ('-' x $cutoff) . "-+";
+                   $border->{h} .= "+=" . ('=' x $cutoff) . '=';
                }
            } elsif ($i == 0) {
                if ($utf8) {
                    $formatstring .= "│ %$alignstr${cutoff}s ";
-                   $borderstring_t .= "┌─" . ('─' x $cutoff) . '─';
-                   $borderstring_h .= "╞═" . ('═' x $cutoff) . '═';
-                   $borderstring_m .= "├─" . ('─' x $cutoff) . '─';
-                   $borderstring_b .= "└─" . ('─' x $cutoff) . '─';
+                   $border->{t} .= "┌─" . ('─' x $cutoff) . '─';
+                   $border->{h} .= "╞═" . ('═' x $cutoff) . '═';
+                   $border->{m} .= "├─" . ('─' x $cutoff) . '─';
+                   $border->{b} .= "└─" . ('─' x $cutoff) . '─';
                } else {
                    $formatstring .= "| %$alignstr${cutoff}s ";
-                   $borderstring_m .= "+-" . ('-' x $cutoff) . '-';
-                   $borderstring_h .= "+=" . ('=' x $cutoff) . '=';
+                   $border->{m} .= "+-" . ('-' x $cutoff) . '-';
+                   $border->{h} .= "+=" . ('=' x $cutoff) . '=';
                }
            } elsif ($i == ($column_count - 1)) {
                if ($utf8) {
                    $formatstring .= "│ %$alignstr${cutoff}s │";
-                   $borderstring_t .= "┬─" . ('─' x $cutoff) . "─┐";
-                   $borderstring_h .= "╪═" . ('═' x $cutoff) . '═╡';
-                   $borderstring_m .= "┼─" . ('─' x $cutoff) . "─┤";
-                   $borderstring_b .= "┴─" . ('─' x $cutoff) . "─┘";
+                   $border->{t} .= "┬─" . ('─' x $cutoff) . "─┐";
+                   $border->{h} .= "╪═" . ('═' x $cutoff) . '═╡';
+                   $border->{m} .= "┼─" . ('─' x $cutoff) . "─┤";
+                   $border->{b} .= "┴─" . ('─' x $cutoff) . "─┘";
                } else {
                    $formatstring .= "| %$alignstr${cutoff}s |";
-                   $borderstring_m .= "+-" . ('-' x $cutoff) . "-+";
-                   $borderstring_h .= "+=" . ('=' x $cutoff) . "=+";
+                   $border->{m} .= "+-" . ('-' x $cutoff) . "-+";
+                   $border->{h} .= "+=" . ('=' x $cutoff) . "=+";
                }
            } else {
                if ($utf8) {
                    $formatstring .= "│ %$alignstr${cutoff}s ";
-                   $borderstring_t .= "┬─" . ('─' x $cutoff) . '─';
-                   $borderstring_h .= "╪═" . ('═' x $cutoff) . '═';
-                   $borderstring_m .= "┼─" . ('─' x $cutoff) . '─';
-                   $borderstring_b .= "┴─" . ('─' x $cutoff) . '─';
+                   $border->{t} .= "┬─" . ('─' x $cutoff) . '─';
+                   $border->{h} .= "╪═" . ('═' x $cutoff) . '═';
+                   $border->{m} .= "┼─" . ('─' x $cutoff) . '─';
+                   $border->{b} .= "┴─" . ('─' x $cutoff) . '─';
                } else {
                    $formatstring .= "| %$alignstr${cutoff}s ";
-                   $borderstring_m .= "+-" . ('-' x $cutoff) . '-';
-                   $borderstring_h .= "+=" . ('=' x $cutoff) . '=';
+                   $border->{m} .= "+-" . ('-' x $cutoff) . '-';
+                   $border->{h} .= "+=" . ('=' x $cutoff) . '=';
                }
            }
        } else {
@@ -310,8 +244,8 @@ sub print_text_table {
        }
     }
 
-    $borderstring_t = $borderstring_m if !length($borderstring_t);
-    $borderstring_b = $borderstring_m if !length($borderstring_b);
+    $border->{t} = $border->{m} if !length($border->{t});
+    $border->{b} = $border->{m} if !length($border->{b});
 
     my $writeln = sub {
        my ($text) = @_;
@@ -323,27 +257,25 @@ sub print_text_table {
        }
     };
 
-    $writeln->($borderstring_t) if $border;
+    $writeln->($border->{t}) if $show_border;
 
-    my $borderstring_sep;
-    if ($header) {
+    if ($show_header) {
        my $text = sprintf $formatstring, map { $colopts->{$_}->{title} } @$props_to_print;
        $writeln->($text);
-       $borderstring_sep = $borderstring_h;
+       $border->{sep} = $border->{h};
     } else {
-       $borderstring_sep = $borderstring_m;
+       $border->{sep} = $border->{m};
     }
 
     for (my $i = 0; $i < scalar(@$tabledata); $i++) {
        my $coldata = $tabledata->[$i];
 
-       if ($border && ($i != 0 || $header)) {
-           $writeln->($borderstring_sep);
-           $borderstring_sep = $borderstring_m;
+       if ($show_border && ($i != 0 || $show_header)) {
+           $writeln->($border->{sep});
+           $border->{sep} = $border->{m};
        }
 
        for (my $i = 0; $i < $coldata->{height}; $i++) {
-
            my $text = sprintf $formatstring, map {
                substr($coldata->{rowdata}->{$_}->{lines}->[$i] // '', 0, $colopts->{$_}->{cutoff});
            } @$props_to_print;
@@ -352,7 +284,7 @@ sub print_text_table {
        }
     }
 
-    $writeln->($borderstring_b) if $border;
+    $writeln->($border->{b}) if $show_border;
 }
 
 sub extract_properties_to_print {
@@ -440,7 +372,7 @@ sub print_api_result {
     }
 
     if ($format eq 'yaml') {
-       print encode('UTF-8', CPAN::Meta::YAML::Dump($data));
+       print encode('UTF-8', YAML::XS::Dump($data));
     } elsif ($format eq 'json') {
        # Note: we always use utf8 encoding for json format
        print to_json($data, {utf8 => 1, allow_nonref => 1, canonical => 1 }) . "\n";
@@ -462,7 +394,12 @@ sub print_api_result {
            my $schema = { type => 'array', items => { type => 'object' }};
            print_api_list($kvstore, $schema, ['key', 'value'], $options, $terminal_opts);
        } elsif ($type eq 'array') {
-           return if !scalar(@$data);
+           if (ref($data) eq 'ARRAY') {
+               return if !scalar(@$data);
+           } elsif (ref($data) eq 'HASH') {
+               return if !scalar($data->%*);
+               die "got hash object, but result schema specified array!\n"
+           }
            my $item_type = $result_schema->{items}->{type};
            if ($item_type eq 'object') {
                print_api_list($data, $result_schema, $props_to_print, $options, $terminal_opts);