From 59d3653b9157801cb1fcd2c80cc03df2db9437f8 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Wed, 11 Jul 2018 10:36:16 +0200 Subject: [PATCH] use new features from pve-common --- PVE/APIClient/Commands/config.pm | 10 +++++++++- PVE/APIClient/Commands/list.pm | 7 ++----- PVE/APIClient/Commands/remote.pm | 10 +++------- PVE/APIClient/Helpers.pm | 32 ++------------------------------ pveclient | 31 ++++++++++++------------------- 5 files changed, 28 insertions(+), 62 deletions(-) diff --git a/PVE/APIClient/Commands/config.pm b/PVE/APIClient/Commands/config.pm index a5171fb..b4316d8 100644 --- a/PVE/APIClient/Commands/config.pm +++ b/PVE/APIClient/Commands/config.pm @@ -9,6 +9,7 @@ use PVE::APIClient::JSONSchema qw(get_standard_option); use PVE::APIClient::Tools qw(extract_param); use PVE::APIClient::Config; +use PVE::APIClient::CLIFormatter; use PVE::APIClient::CLIHandler; use base qw(PVE::APIClient::CLIHandler); @@ -23,6 +24,7 @@ __PACKAGE__->register_method ({ description => "Dump default configuration.", parameters => { additionalProperties => 0, + properties => {}, }, returns => { type => 'object', @@ -90,7 +92,13 @@ __PACKAGE__->register_method ({ our $cmddef = { set => [ __PACKAGE__, 'set',], - list => [__PACKAGE__, 'list', undef, undef, sub { PVE::APIClient::Helpers::print_result(@_);}], + list => [__PACKAGE__, 'list', undef, undef, + sub { + my ($data, $schema, $options) = @_; + + PVE::APIClient::CLIFormatter::print_api_result($data, $schema, undef, $options); + } + ], }; 1; diff --git a/PVE/APIClient/Commands/list.pm b/PVE/APIClient/Commands/list.pm index 10a8f7c..1fb90bf 100644 --- a/PVE/APIClient/Commands/list.pm +++ b/PVE/APIClient/Commands/list.pm @@ -30,7 +30,6 @@ __PACKAGE__->register_method ({ additionalProperties => 0, properties => { remote => get_standard_option('pveclient-remote-name'), - 'format' => get_standard_option('pve-output-format'), }, }, returns => { @@ -43,9 +42,6 @@ __PACKAGE__->register_method ({ code => sub { my ($param) = @_; - my $format = PVE::APIClient::Tools::extract_param($param, 'format'); - PVE::APIClient::Helpers::set_output_format($format); - my $config = PVE::APIClient::Config->load(); my $conn = PVE::APIClient::Config->remote_conn($config, $param->{remote}); @@ -54,7 +50,8 @@ __PACKAGE__->register_method ({ our $cmddef = [ __PACKAGE__, 'list', ['remote'], {}, sub { - PVE::APIClient::Helpers::print_ordered_result($list_returns_properties, @_); + my ($data, $schema, $options) = @_; + PVE::APIClient::Helpers::print_ordered_result($list_returns_properties, $data, $schema, $options); }]; 1; diff --git a/PVE/APIClient/Commands/remote.pm b/PVE/APIClient/Commands/remote.pm index 2e5337a..bd13e2b 100644 --- a/PVE/APIClient/Commands/remote.pm +++ b/PVE/APIClient/Commands/remote.pm @@ -35,9 +35,7 @@ __PACKAGE__->register_method ({ description => "List remotes from your config file.", parameters => { additionalProperties => 0, - properties => { - 'format' => get_standard_option('pve-output-format'), - }, + properties => {}, }, returns => { type => 'array', @@ -49,9 +47,6 @@ __PACKAGE__->register_method ({ code => sub { my ($param) = @_; - my $format = PVE::APIClient::Tools::extract_param($param, 'format'); - PVE::APIClient::Helpers::set_output_format($format); - my $config = PVE::APIClient::Config->load(); my $res = []; @@ -212,7 +207,8 @@ our $cmddef = { set => [ __PACKAGE__, 'remote_set', ['name']], delete => [ __PACKAGE__, 'remote_delete', ['name']], list => [__PACKAGE__, 'remote_list', undef, {}, sub { - PVE::APIClient::Helpers::print_ordered_result($remote_list_returns_properties, @_); + my ($data, $schema, $options) = @_; + PVE::APIClient::Helpers::print_ordered_result($remote_list_returns_properties, $data, $schema, $options); }], }; diff --git a/PVE/APIClient/Helpers.pm b/PVE/APIClient/Helpers.pm index 9d599d6..106e497 100644 --- a/PVE/APIClient/Helpers.pm +++ b/PVE/APIClient/Helpers.pm @@ -27,23 +27,6 @@ our $method_map = { 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; -} - my $__real_remove_formats; $__real_remove_formats = sub { my ($properties) = @_; @@ -405,23 +388,12 @@ sub extract_even_elements { 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 ($property_list, $data, $result_schema, $options) = @_; my $param_order = extract_even_elements($property_list); - print_result($data, $result_schema, $param_order); + PVE::APIClient::CLIFormatter::print_api_result($data, $result_schema, $param_order, $options); } 1; diff --git a/pveclient b/pveclient index b12e5f6..c6c48c8 100755 --- a/pveclient +++ b/pveclient @@ -24,7 +24,7 @@ use PVE::APIClient::Commands::GuestStatus; use JSON; sub call_api_method { - my ($method, $param) = @_; + my ($method, $param, $options) = @_; my $path = PVE::APIClient::Tools::extract_param($param, 'api_path'); die "missing API path\n" if !defined($path); @@ -32,9 +32,6 @@ sub call_api_method { my $remote = PVE::APIClient::Tools::extract_param($param, 'remote'); die "missing remote\n" if !defined($remote); - my $format = PVE::APIClient::Tools::extract_param($param, 'format'); - PVE::APIClient::Helpers::set_output_format($format); - my $config = PVE::APIClient::Config->load(); my $uri_param = {}; @@ -103,18 +100,14 @@ if (my $info = PVE::APIClient::Helpers::extract_path_info($uri_param)) { $path_returns = $info->{returns}; } -$path_properties->{format} = get_standard_option('pve-output-format'), $path_properties->{remote} = get_standard_option('pveclient-remote-name'); $path_properties->{api_path} = $api_path_property; my $format_result = sub { - my ($data) = @_; - - my $format = PVE::APIClient::Helpers::get_output_format(); + my ($data, $schema, $options) = @_; - my $options = PVE::APIClient::CLIFormatter::query_terminal_options({}); - - PVE::APIClient::CLIFormatter::print_api_result($format, $data, $path_returns, undef, $options); + # NOTE; we need to use $path_returns instead of $schema + PVE::APIClient::CLIFormatter::print_api_result($data, $path_returns, undef, $options); }; __PACKAGE__->register_method ({ @@ -128,9 +121,9 @@ __PACKAGE__->register_method ({ }, returns => $path_returns, code => sub { - my ($param) = @_; + my ($param, $options) = @_; - return call_api_method('GET', $param); + return call_api_method('GET', $param, $options); }}); __PACKAGE__->register_method ({ @@ -144,9 +137,9 @@ __PACKAGE__->register_method ({ }, returns => $path_returns, code => sub { - my ($param) = @_; + my ($param, $options) = @_; - return call_api_method('PUT', $param); + return call_api_method('PUT', $param, $options); }}); __PACKAGE__->register_method ({ @@ -160,9 +153,9 @@ __PACKAGE__->register_method ({ }, returns => $path_returns, code => sub { - my ($param) = @_; + my ($param, $options) = @_; - return call_api_method('PUSH', $param); + return call_api_method('PUSH', $param, $options); }}); __PACKAGE__->register_method ({ @@ -176,9 +169,9 @@ __PACKAGE__->register_method ({ }, returns => $path_returns, code => sub { - my ($param) = @_; + my ($param, $options) = @_; - return call_api_method('DELETE', $param); + return call_api_method('DELETE', $param, $options); }}); __PACKAGE__->register_method ({ -- 2.39.2