]> git.proxmox.com Git - pve-client.git/commitdiff
update files from pve-common
authorDietmar Maurer <dietmar@proxmox.com>
Wed, 11 Jul 2018 07:44:04 +0000 (09:44 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Wed, 11 Jul 2018 07:44:04 +0000 (09:44 +0200)
PVE/APIClient/CLIFormatter.pm
PVE/APIClient/CLIHandler.pm
PVE/APIClient/JSONSchema.pm
PVE/APIClient/RESTHandler.pm

index d34a03b384a025bc0bedd45a88dafed487f10168..ed4f266b6c9f4576514879393fcec098c82e12ee 100644 (file)
@@ -102,7 +102,9 @@ sub data_to_text {
 
     return '' if !defined($data);
 
-    if (defined($propdef)) {
+    my $human_readable = $options->{'human-readable'} // 1;
+
+     if ($human_readable && defined($propdef)) {
        if (my $type = $propdef->{type}) {
            if ($type eq 'boolean') {
                return $data ? 1 : 0;
@@ -133,7 +135,8 @@ sub data_to_text {
 # - sort_key: can be used to sort after a column, if it isn't set we sort
 #   after the leftmost column (with no undef value in $data) this can be
 #   turned off by passing 0 as sort_key
-# - border: print with/without table header and asciiart border
+# - noborder: print without asciiart border
+# - noheader: print without table header
 # - columns: limit output width (if > 0)
 # - utf8: use utf8 characters for table delimiters
 
@@ -141,7 +144,8 @@ sub print_text_table {
     my ($data, $returnprops, $props_to_print, $options) = @_;
 
     my $sort_key = $options->{sort_key};
-    my $border = $options->{border};
+    my $border = !$options->{noborder};
+    my $header = !$options->{noheader};
     my $columns = $options->{columns};
     my $utf8 = $options->{utf8};
     my $encoding = $options->{encoding} // 'UTF-8';
@@ -286,15 +290,20 @@ sub print_text_table {
     };
 
     $writeln->($borderstring_t) if $border;
-    my $text = sprintf $formatstring, map { $colopts->{$_}->{title} } @$props_to_print;
-    $writeln->($text);
 
-    foreach my $coldata (@$tabledata) {
-       $writeln->($borderstring_m) if $border;
+    if ($header) {
+       my $text = sprintf $formatstring, map { $colopts->{$_}->{title} } @$props_to_print;
+       $writeln->($text);
+    }
+
+    for (my $i = 0; $i < scalar(@$tabledata); $i++) {
+       my $coldata = $tabledata->[$i];
+
+       $writeln->($borderstring_m) if $border && ($i != 0 || $header);
 
        for (my $i = 0; $i < $coldata->{height}; $i++) {
 
-           $text = sprintf $formatstring, map {
+           my $text = sprintf $formatstring, map {
                substr($coldata->{rowdata}->{$_}->{lines}->[$i] // '', 0, $colopts->{$_}->{cutoff});
            } @$props_to_print;
 
@@ -355,7 +364,9 @@ sub print_api_list {
 }
 
 sub print_api_result {
-    my ($format, $data, $result_schema, $props_to_print, $options) = @_;
+    my ($data, $result_schema, $props_to_print, $options) = @_;
+
+    return if $options->{quiet};
 
     if (!defined($options)) {
        $options = query_terminal_options({});
@@ -363,12 +374,17 @@ sub print_api_result {
        $options = { %$options }; # copy
     }
 
+    my $format = $options->{format} // 'text';
+
     return if $result_schema->{type} eq 'null';
 
     if ($format eq 'json') {
+       # Note: we always use utf8 encoding for json format
+       print to_json($data, {utf8 => 1, allow_nonref => 1, canonical => 1 }) . "\n";
+    } elsif ($format eq 'json-pretty') {
        # Note: we always use utf8 encoding for json format
        print to_json($data, {utf8 => 1, allow_nonref => 1, canonical => 1, pretty => 1 });
-    } elsif ($format eq 'text' || $format eq 'plain') {
+    } elsif ($format eq 'text') {
        my $encoding = $options->{encoding} // 'UTF-8';
        my $type = $result_schema->{type};
        if ($type eq 'object') {
@@ -377,14 +393,13 @@ sub print_api_result {
            $props_to_print = [ sort keys %$data ] if !scalar(@$props_to_print);
            my $kvstore = [];
            foreach my $key (@$props_to_print) {
+               next if !defined($data->{$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';
            print_api_list($kvstore, $schema, ['key', 'value'], $options);
        } elsif ($type eq 'array') {
            return if !scalar(@$data);
-           $options->{border} = $format eq 'text';
            my $item_type = $result_schema->{items}->{type};
            if ($item_type eq 'object') {
                print_api_list($data, $result_schema, $props_to_print, $options);
@@ -394,6 +409,7 @@ sub print_api_result {
                    push @$kvstore, { value => $value };
                }
                my $schema = { type => 'array', items => { type => 'object', properties => { value => $result_schema->{items} }}};
+               $options->{noheader} = 1;
                print_api_list($kvstore, $schema, ['value'], $options);
            }
        } else {
index f38c6951ecd8a2f6b7c626c90df70f3db4b3cc2f..09595a4e1a5d44f091a24db2dea1766a1398d85e 100644 (file)
@@ -550,11 +550,13 @@ my $handle_cmd  = sub {
     my ($class, $name, $arg_param, $uri_param, $outsub) = @{$def || []};
     $abort->("unknown command '$cmd_str'") if !$class;
 
-    my $res = $class->cli_handler($cmd_str, $name, $cmd_args, $arg_param, $uri_param, $param_cb);
+    my $options = PVE::APIClient::CLIFormatter::query_terminal_options({});
+
+    my $res = $class->cli_handler($cmd_str, $name, $cmd_args, $arg_param, $uri_param, $param_cb, $options);
 
     if (defined $outsub) {
        my $result_schema = $class->map_method_by_name($name)->{returns};
-       $outsub->($res, $result_schema);
+       $outsub->($res, $result_schema, $options);
     }
 };
 
@@ -588,11 +590,13 @@ my $handle_simple_cmd = sub {
 
     &$preparefunc() if $preparefunc;
 
-    my $res = $class->cli_handler($name, $name, \@ARGV, $arg_param, $uri_param, $param_cb);
+    my $options = PVE::APIClient::CLIFormatter::query_terminal_options({});
+
+    my $res = $class->cli_handler($name, $name, \@ARGV, $arg_param, $uri_param, $param_cb, $options);
 
     if (defined $outsub) {
        my $result_schema = $class->map_method_by_name($name)->{returns};
-       $outsub->($res, $result_schema);
+       $outsub->($res, $result_schema, $options);
     }
 };
 
index c5f000a4e47e268fdecf3c35a99d255f8d6a3202..a7942f72b86a34dd8d81a69dc4275d14f62e1136 100644 (file)
@@ -108,7 +108,7 @@ register_standard_option('fingerprint-sha256', {
 register_standard_option('pve-output-format', {
     type => 'string',
     description => 'Output format.',
-    enum => [ 'text', 'plain', 'json' ],
+    enum => [ 'text', 'json', 'json-pretty' ],
     optional => 1,
     default => 'text',
 });
index 22e81691f3fc2e3f2ea599c579ce5444af0c9203..59f43329c3cef4da23308a7a48632ec49938c004 100644 (file)
@@ -394,7 +394,7 @@ sub find_handler {
 }
 
 sub handle {
-    my ($self, $info, $param) = @_;
+    my ($self, $info, $param, $output_options) = @_;
 
     my $func = $info->{code};
 
@@ -414,7 +414,8 @@ sub handle {
        $param->{'extra-args'} = [map { /^(.*)$/ } @$extra] if $extra;
     }
 
-    my $result = &$func($param); 
+    $output_options //= {};
+    my $result = &$func($param, $output_options);
 
     # todo: this is only to be safe - disable?
     if (my $schema = $info->{returns}) {
@@ -745,22 +746,64 @@ my $replace_file_names_with_contents = sub {
     return $param;
 };
 
+our $standard_output_options = {
+    format => PVE::APIClient::JSONSchema::get_standard_option('pve-output-format'),
+    noheader => {
+       description => "Do not show column headers (for 'text' format).",
+       type => 'boolean',
+       optional => 1,
+       default => 1,
+    },
+    noborder => {
+       description => "Do not draw borders (for 'text' format).",
+       type => 'boolean',
+       optional => 1,
+       default => 1,
+    },
+    quiet => {
+        description => "Suppress printing results.",
+        type => 'boolean',
+        optional => 1,
+    },
+    'human-readable' => {
+        description => "Call output rendering functions to produce human readable text.",
+        type => 'boolean',
+        optional => 1,
+       default => 1,
+    }
+};
+
+sub add_standard_output_parameters {
+    my ($org_schema) = @_;
+
+    my $schema = { %$org_schema };
+    $schema->{properties} = { %{$schema->{properties}}, %$standard_output_options };
+
+    return $schema;
+};
+
 sub cli_handler {
-    my ($self, $prefix, $name, $args, $arg_param, $fixed_param, $param_cb) = @_;
+    my ($self, $prefix, $name, $args, $arg_param, $fixed_param, $param_cb, $options) = @_;
 
     my $info = $self->map_method_by_name($name);
+    $options //= {};
 
     my $res;
     eval {
        my $param_map = {};
        $param_map = $compute_param_mapping_hash->($param_cb->($name)) if $param_cb;
-       my $param = PVE::APIClient::JSONSchema::get_options($info->{parameters}, $args, $arg_param, $fixed_param, $param_map);
+       my $schema = add_standard_output_parameters($info->{parameters});
+       my $param = PVE::APIClient::JSONSchema::get_options($schema, $args, $arg_param, $fixed_param, $param_map);
+
+       foreach my $opt (keys %$standard_output_options) {
+           $options->{$opt} = delete $param->{$opt} if defined($param->{$opt});
+       }
 
        if (defined($param_map)) {
            $replace_file_names_with_contents->($param, $param_map);
        }
 
-       $res = $self->handle($info, $param);
+       $res = $self->handle($info, $param, $options);
     };
     if (my $err = $@) {
        my $ec = ref($err);