X-Git-Url: https://git.proxmox.com/?p=pve-client.git;a=blobdiff_plain;f=PVE%2FAPIClient%2FHelpers.pm;h=3429f4db21e6f736eab4515555fa91d4a6fe11b7;hp=7d855ec6ce58fc6775569fbb426c364dcc3f58bf;hb=4aba8d31fe4441125c4af295e67534797bb27bfb;hpb=2b267ba22efefb08ac15feac1107b920f6186f50 diff --git a/PVE/APIClient/Helpers.pm b/PVE/APIClient/Helpers.pm index 7d855ec..3429f4d 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,65 +20,13 @@ 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', delete => 'DELETE', }; -my $default_output_format = 'text'; -my $client_output_format = $default_output_format; - -sub set_output_format { - my ($format) = @_; - - if (!defined($format)) { - $client_output_format = $default_output_format; - } else { - $client_output_format = $format; - } -} - -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) = @_; @@ -164,7 +115,9 @@ sub find_method_info { my $stack = [ grep { length($_) > 0 } split('\/+' , $path)]; # skip empty fragments - my $child = $map_path_to_info->(get_api_definition(), $stack, $uri_param); + my $api = get_api_definition(); + + my $child = scalar(@$stack) ? $map_path_to_info->($api->{children}, $stack, $uri_param) : $api; if (!($child && $child->{info} && $child->{info}->{$method})) { return undef if $noerr; @@ -229,7 +182,7 @@ sub merge_api_definition_properties { sub complete_api_path { my ($text) = @_; - get_api_definition(); # make sure API data is loaded + my $api = get_api_definition(); # make sure API data is loaded $text =~ s!^/!!; @@ -238,10 +191,10 @@ sub complete_api_path { my $info; if (!defined($dir)) { $dir = ''; - $info = { children => $pve_api_definition }; + $info = $api; } else { my $stack = [ grep { length($_) > 0 } split('\/+' , $dir)]; # skip empty fragments - $info = $map_path_to_info->($pve_api_definition, $stack, {}); + $info = $map_path_to_info->($api->{children}, $stack, {}); } my $res = []; @@ -250,21 +203,20 @@ sub complete_api_path { if (my $children = $info->{children}) { foreach my $c (@$children) { my $ctext = $c->{text}; + push @$res, "${prefix}$ctext" if $ctext =~ m/^\Q$rest/; if ($ctext =~ m/^\{(\S+)\}$/) { - push @$res, "$prefix$ctext"; - push @$res, "$prefix$ctext/"; - if (length($rest)) { + if (length($rest) && $rest ne $ctext) { push @$res, "$prefix$rest"; - push @$res, "$prefix$rest/"; } - } elsif ($ctext =~ m/^\Q$rest/) { - push @$res, "$prefix$ctext"; - push @$res, "$prefix$ctext/" if $c->{children}; } } } } + if (scalar(@$res) == 1) { + $res = [$res->[0], "$res->[0]/"]; + } + return $res; } @@ -355,6 +307,10 @@ sub poll_task { sleep(2); } + if ($task_status->{exitstatus} ne "OK") { + die $task_status->{exitstatus}; + } + return $task_status->{exitstatus}; } @@ -424,5 +380,20 @@ sub ticket_cache_update { die $@ if $@; } +sub extract_even_elements { + my ($list) = @_; + + my $ind = 0; + return [ grep { ($ind++ % 2) == 0 } @$list ]; +} + +sub print_ordered_result { + my ($property_list, $data, $result_schema, $options) = @_; + + my $param_order = extract_even_elements($property_list); + + PVE::APIClient::CLIFormatter::query_terminal_options($options); + PVE::APIClient::CLIFormatter::print_api_result($data, $result_schema, $param_order, $options); +} 1;