]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/CLIFormatter.pm
PVE::CLIFormatter::data_to_text - add parameter $options
[pve-common.git] / src / PVE / CLIFormatter.pm
index 867e4e9e24cb08f4cce26e5094513594790443d9..549ed3177964ea7d7950cc0acaed7ff1058e2875 100644 (file)
@@ -3,6 +3,7 @@ package PVE::CLIFormatter;
 use strict;
 use warnings;
 use I18N::Langinfo;
+use POSIX qw(strftime);
 
 use PVE::JSONSchema;
 use PVE::PTY;
@@ -10,6 +11,15 @@ 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_duration {
     my ($duration_in_seconds) = @_;
 
@@ -79,7 +89,7 @@ sub query_terminal_options {
 }
 
 sub data_to_text {
-    my ($data, $propdef) = @_;
+    my ($data, $propdef, $options) = @_;
 
     return '' if !defined($data);
 
@@ -95,7 +105,7 @@ sub data_to_text {
        if (defined(my $renderer = $propdef->{renderer})) {
            my $code = PVE::JSONSchema::get_renderer($renderer);
            die "internal error: unknown renderer '$renderer'" if !$code;
-           return $code->($data);
+           return $code->($data, $options);
        }
     }
 
@@ -160,7 +170,7 @@ sub print_text_table {
            my $prop = $props_to_print->[$i];
            my $propinfo = $returnprops->{$prop} // {};
 
-           my $text = data_to_text($entry->{$prop}, $propinfo);
+           my $text = data_to_text($entry->{$prop}, $propinfo, $options);
            my $lines = [ split(/\n/, $text) ];
            my $linecount = scalar(@$lines);
            $height = $linecount if $linecount > $height;
@@ -286,6 +296,24 @@ sub print_text_table {
     $writeln->($borderstring_b) if $border;
 }
 
+sub extract_properties_to_print {
+    my ($propdef) = @_;
+
+    my $required = [];
+    my $optional = [];
+
+    foreach my $key (keys %$propdef) {
+       my $prop = $propdef->{$key};
+       if ($prop->{optional}) {
+           push @$optional, $key;
+       } else {
+           push @$required, $key;
+       }
+    }
+
+    return [ sort(@$required), sort(@$optional) ];
+}
+
 # prints the result of an API GET call returning an array as a table.
 # takes formatting information from the results property of the call
 # if $props_to_print is provided, prints only those columns. otherwise
@@ -299,20 +327,21 @@ sub print_api_list {
 
     my $returnprops = $result_schema->{items}->{properties};
 
-    if (!defined($props_to_print)) {
-       $props_to_print = [ sort keys %$returnprops ];
-       if (!scalar(@$props_to_print)) {
-           my $all_props = {};
-           foreach my $obj (@{$data}) {
-               foreach my $key (keys %{$obj}) {
-                   $all_props->{ $key } = 1;
-               }
+    $props_to_print = extract_properties_to_print($returnprops)
+       if !defined($props_to_print);
+
+    if (!scalar(@$props_to_print)) {
+       my $all_props = {};
+       foreach my $obj (@$data) {
+           foreach my $key (keys %$obj) {
+               $all_props->{$key} = 1;
            }
-           $props_to_print = [ sort keys %{$all_props} ];
        }
-       die "unable to detect list properties\n" if !scalar(@$props_to_print);
+       $props_to_print = [ sort keys %{$all_props} ];
     }
 
+    die "unable to detect list properties\n" if !scalar(@$props_to_print);
+
     print_text_table($data, $returnprops, $props_to_print, $options);
 }
 
@@ -334,10 +363,12 @@ sub print_api_result {
        my $encoding = $options->{encoding} // 'UTF-8';
        my $type = $result_schema->{type};
        if ($type eq 'object') {
-           $props_to_print = [ sort keys %$data ] if !defined($props_to_print);
+           $props_to_print = extract_properties_to_print($result_schema->{properties})
+               if !defined($props_to_print);
+           $props_to_print = [ sort keys %$data ] if !scalar(@$props_to_print);
            my $kvstore = [];
            foreach my $key (@$props_to_print) {
-               push @$kvstore, { key => $key, value => data_to_text($data->{$key}, $result_schema->{properties}->{$key}) };
+               push @$kvstore, { key => $key, value => data_to_text($data->{$key}, $result_schema->{properties}->{$key}, $options) };
            }
            my $schema = { type => 'array', items => { type => 'object' }};
            $options->{border} = $format eq 'text';
@@ -350,7 +381,7 @@ sub print_api_result {
                print_api_list($data, $result_schema, $props_to_print, $options);
            } else {
                foreach my $entry (@$data) {
-                   print encode($encoding, data_to_text($entry) . "\n");
+                   print encode($encoding, data_to_text($entry, $result_schema->{items}, $options) . "\n");
                }
            }
        } else {