]> git.proxmox.com Git - pve-client.git/commitdiff
update files from pve-common
authorDietmar Maurer <dietmar@proxmox.com>
Wed, 4 Jul 2018 06:53:36 +0000 (08:53 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Wed, 4 Jul 2018 06:53:36 +0000 (08:53 +0200)
PVE/APIClient/CLIFormatter.pm
PVE/APIClient/JSONSchema.pm

index add7a3a615292d1c12cccd48108a30e43d4a24db..57e0c8912ed04a005afd9b7d3ffadbe434e40ed9 100644 (file)
@@ -10,6 +10,58 @@ use JSON;
 use utf8;
 use Encode;
 
 use utf8;
 use Encode;
 
+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::APIClient::JSONSchema::register_renderer('duration', \&render_duration);
+
+sub render_fraction_as_percentage {
+    my ($fraction) = @_;
+
+    return sprintf("%.2f%%", $fraction*100);
+}
+
+PVE::APIClient::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);
+    }
+
+    return sprintf "%.2f $units[$max_unit]", $value;
+}
+
+PVE::APIClient::JSONSchema::register_renderer('bytes', \&render_bytes);
+
 sub query_terminal_options {
     my ($options) = @_;
 
 sub query_terminal_options {
     my ($options) = @_;
 
@@ -234,6 +286,24 @@ sub print_text_table {
     $writeln->($borderstring_b) if $border;
 }
 
     $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
 # 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
@@ -247,20 +317,21 @@ sub print_api_list {
 
     my $returnprops = $result_schema->{items}->{properties};
 
 
     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);
 }
 
     print_text_table($data, $returnprops, $props_to_print, $options);
 }
 
@@ -282,7 +353,9 @@ sub print_api_result {
        my $encoding = $options->{encoding} // 'UTF-8';
        my $type = $result_schema->{type};
        if ($type eq 'object') {
        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}) };
            my $kvstore = [];
            foreach my $key (@$props_to_print) {
                push @$kvstore, { key => $key, value => data_to_text($data->{$key}, $result_schema->{properties}->{$key}) };
index dd0a2c66aa48ef9ef8fb2de064be70d2857124c1..c5f000a4e47e268fdecf3c35a99d255f8d6a3202 100644 (file)
@@ -105,6 +105,14 @@ register_standard_option('fingerprint-sha256', {
     pattern => '([A-Fa-f0-9]{2}:){31}[A-Fa-f0-9]{2}',
 });
 
     pattern => '([A-Fa-f0-9]{2}:){31}[A-Fa-f0-9]{2}',
 });
 
+register_standard_option('pve-output-format', {
+    type => 'string',
+    description => 'Output format.',
+    enum => [ 'text', 'plain', 'json' ],
+    optional => 1,
+    default => 'text',
+});
+
 my $format_list = {};
 
 sub register_format {
 my $format_list = {};
 
 sub register_format {
@@ -1081,6 +1089,11 @@ my $default_schema_noref = {
            optional => 1,
            description => "This provides the title of the property",
        },
            optional => 1,
            description => "This provides the title of the property",
        },
+       renderer => {
+           type => "string",
+           optional => 1,
+           description => "This is used to provide rendering hints to format cli command output.",
+       },
        requires => {
            type => [ "string", "object" ],
            optional => 1,
        requires => {
            type => [ "string", "object" ],
            optional => 1,