[pve-devel] [PATCH common v3 3/3] add print_api_list, adapt handle_cmd
Thomas Lamprecht
t.lamprecht at proxmox.com
Thu Jun 14 08:34:54 CEST 2018
On 6/12/18 12:41 PM, Stoiko Ivanov wrote:
> print_api_list takes an array of columns to print, the data, and the returns
> definitions of the API, and prints the data by using print_text_table.
>
> handle_cmd and handle_simple_cmd call the output sub with the returns definitons.
>
> Signed-off-by: Stoiko Ivanov <s.ivanov at proxmox.com>
> ---
> src/PVE/CLIHandler.pm | 31 +++++++++++++++++++++++++++++--
> 1 file changed, 29 insertions(+), 2 deletions(-)
>
> diff --git a/src/PVE/CLIHandler.pm b/src/PVE/CLIHandler.pm
> index f87720c..d0c629e 100644
> --- a/src/PVE/CLIHandler.pm
> +++ b/src/PVE/CLIHandler.pm
> @@ -459,6 +459,27 @@ sub print_entry {
> }
> }
>
> +# used to print the result of an API-listing - expects the API to return an array
> +# and to have the results key of the API call defined.
> +sub print_api_list {
> + my ($props_to_print, $data, $resultprops) = @_;
> + my $formatopts;
> +
> + foreach my $prop ( @$props_to_print ) {
> + my $propinfo = $resultprops->{$prop};
> + my $colopts = {
> + key => $prop,
> + title => $propinfo->{title},
> + default => $propinfo->{default},
> + title => $propinfo->{title},
> + cutoff => $propinfo->{print_width} // $propinfo->{maxLength},
> + };
> + push @$formatopts, $colopts;
> + }
> +
> + print_text_table($formatopts, $data);
> +}
> +
> sub verify_api {
> my ($class) = @_;
>
> @@ -560,7 +581,10 @@ my $handle_cmd = sub {
>
> my $res = $class->cli_handler($cmd_str, $name, $cmd_args, $arg_param, $uri_param, $read_password_func, $param_mapping_func);
>
> - &$outsub($res) if $outsub;
> + if (defined $outsub) {
> + my $returnproperties = $class->map_method_by_name($name)->{returns}->{items}->{properties};
This only works for API calls which return objects, not arrays.
please pass the whole return info to $outsub, i.e.:
my $returninfo = $class->map_method_by_name($name)->{returns}
> + $outsub->($res, $returnproperties);
> + }
> };
>
> my $handle_simple_cmd = sub {
> @@ -595,7 +619,10 @@ my $handle_simple_cmd = sub {
>
> my $res = $class->cli_handler($name, $name, \@ARGV, $arg_param, $uri_param, $read_password_func, $param_mapping_func);
>
> - &$outsub($res) if $outsub;
> + if (defined $outsub) {
> + my $returnproperties = $class->map_method_by_name($name)->{returns}->{items}->{properties};
see above
> + $outsub->($res, $returnproperties);
> + }
> };
>
> sub run_cli_handler {
>
More information about the pve-devel
mailing list