X-Git-Url: https://git.proxmox.com/?p=pve-client.git;a=blobdiff_plain;f=PVE%2FAPIClient%2FHelpers.pm;h=9d599d6cd27b0b3f83d0ca688468aa9e8733f430;hp=9ee00e9d127bcdc17308bc4964141adb10430840;hb=55d17e7e1afc13816e8cd92f928fc65ba60e6820;hpb=2ecf9b57e894b6308daee2e60a2d7f5bd417932c diff --git a/PVE/APIClient/Helpers.pm b/PVE/APIClient/Helpers.pm index 9ee00e9..9d599d6 100644 --- a/PVE/APIClient/Helpers.pm +++ b/PVE/APIClient/Helpers.pm @@ -9,6 +9,9 @@ use File::Path qw(make_path); use PVE::APIClient::JSONSchema; use PVE::APIClient::Exception qw(raise); +use PVE::APIClient::CLIFormatter; +use PVE::APIClient::CLIHandler; +use PVE::APIClient::PTY; use Encode::Locale; use Encode; use HTTP::Status qw(:constants); @@ -17,7 +20,7 @@ my $pve_api_definition; my $pve_api_definition_fn = "/usr/share/pve-client/pve-api-definition.dat"; -my $method_map = { +our $method_map = { create => 'POST', set => 'PUT', get => 'GET', @@ -41,40 +44,6 @@ sub get_output_format { return $client_output_format; } -sub print_result { - my ($data, $result_schema) = @_; - - my $format = get_output_format(); - - return if $result_schema->{type} eq 'null'; - - # TODO: implement different output formats ($format) - - if ($format eq 'json') { - print to_json($data, {utf8 => 1, allow_nonref => 1, canonical => 1, pretty => 1 }); - } elsif ($format eq 'text') { - my $type = $result_schema->{type}; - if ($type eq 'object') { - die "implement me"; - } elsif ($type eq 'array') { - my $item_type = $result_schema->{items}->{type}; - if ($item_type eq 'object') { - die "implement me"; - } elsif ($item_type eq 'array') { - die "implement me"; - } else { - foreach my $el (@$data) { - print "$el\n" - } - } - } else { - print "$data\n"; - } - } else { - die "internal error: unknown output format"; # should not happen - } -} - my $__real_remove_formats; $__real_remove_formats = sub { my ($properties) = @_; @@ -429,5 +398,30 @@ sub ticket_cache_update { die $@ if $@; } +sub extract_even_elements { + my ($list) = @_; + + my $ind = 0; + return [ grep { ($ind++ % 2) == 0 } @$list ]; +} + +sub print_result { + my ($data, $result_schema, $param_order) = @_; + + my $options = {}; + PVE::APIClient::CLIFormatter::query_terminal_options($options); + + my $format = get_output_format(); + PVE::APIClient::CLIFormatter::print_api_result( + $format, $data, $result_schema, $param_order, $options); +} + +sub print_ordered_result { + my ($property_list, $data, $result_schema) = @_; + + my $param_order = extract_even_elements($property_list); + + print_result($data, $result_schema, $param_order); +} 1;