[pve-devel] [pve-common v2 1/2] cli: remove all output formatter magic from CLIHandler

Dietmar Maurer dietmar at proxmox.com
Wed Jul 25 09:30:04 CEST 2018


- and rename format to 'output-format'

- provide a method to add properties to schema:
	PVE::RESTHandler::add_standard_output_properties

- provide methods to extract output properties:
	PVE::RESTHandler::extract_standard_output_properties()

Signed-off-by: Dietmar Maurer <dietmar at proxmox.com>
---
 src/PVE/CLIFormatter.pm |  2 +-
 src/PVE/CLIHandler.pm   | 12 ++++-------
 src/PVE/RESTHandler.pm  | 55 ++++++++++++++++++++++++++++---------------------
 3 files changed, 37 insertions(+), 32 deletions(-)

diff --git a/src/PVE/CLIFormatter.pm b/src/PVE/CLIFormatter.pm
index 7a92f7a..22cd8f2 100644
--- a/src/PVE/CLIFormatter.pm
+++ b/src/PVE/CLIFormatter.pm
@@ -374,7 +374,7 @@ sub print_api_result {
 	$options = { %$options }; # copy
     }
 
-    my $format = $options->{format} // 'text';
+    my $format = $options->{'output-format'} // 'text';
 
     return if $result_schema->{type} eq 'null';
 
diff --git a/src/PVE/CLIHandler.pm b/src/PVE/CLIHandler.pm
index 7145707..2f59d2b 100644
--- a/src/PVE/CLIHandler.pm
+++ b/src/PVE/CLIHandler.pm
@@ -550,13 +550,11 @@ my $handle_cmd  = sub {
     my ($class, $name, $arg_param, $uri_param, $outsub) = @{$def || []};
     $abort->("unknown command '$cmd_str'") if !$class;
 
-    my $options = PVE::CLIFormatter::query_terminal_options({});
-
-    my $res = $class->cli_handler($cmd_str, $name, $cmd_args, $arg_param, $uri_param, $param_cb, $options);
+     my $res = $class->cli_handler($cmd_str, $name, $cmd_args, $arg_param, $uri_param, $param_cb);
 
     if (defined $outsub) {
 	my $result_schema = $class->map_method_by_name($name)->{returns};
-	$outsub->($res, $result_schema, $options);
+	$outsub->($res, $result_schema);
     }
 };
 
@@ -590,13 +588,11 @@ my $handle_simple_cmd = sub {
 
     &$preparefunc() if $preparefunc;
 
-    my $options = PVE::CLIFormatter::query_terminal_options({});
-
-    my $res = $class->cli_handler($name, $name, \@ARGV, $arg_param, $uri_param, $param_cb, $options);
+    my $res = $class->cli_handler($name, $name, \@ARGV, $arg_param, $uri_param, $param_cb);
 
     if (defined $outsub) {
 	my $result_schema = $class->map_method_by_name($name)->{returns};
-	$outsub->($res, $result_schema, $options);
+	$outsub->($res, $result_schema);
     }
 };
 
diff --git a/src/PVE/RESTHandler.pm b/src/PVE/RESTHandler.pm
index 205e6ef..148b4d8 100644
--- a/src/PVE/RESTHandler.pm
+++ b/src/PVE/RESTHandler.pm
@@ -394,7 +394,7 @@ sub find_handler {
 }
 
 sub handle {
-    my ($self, $info, $param, $output_options) = @_;
+    my ($self, $info, $param) = @_;
 
     my $func = $info->{code};
 
@@ -414,8 +414,7 @@ sub handle {
 	$param->{'extra-args'} = [map { /^(.*)$/ } @$extra] if $extra;
     }
 
-    $output_options //= {};
-    my $result = &$func($param, $output_options);
+    my $result = &$func($param);
 
     # todo: this is only to be safe - disable?
     if (my $schema = $info->{returns}) {
@@ -747,7 +746,7 @@ my $replace_file_names_with_contents = sub {
 };
 
 our $standard_output_options = {
-    format => PVE::JSONSchema::get_standard_option('pve-output-format'),
+    'output-format' => PVE::JSONSchema::get_standard_option('pve-output-format'),
     noheader => {
 	description => "Do not show column headers (for 'text' format).",
 	type => 'boolean',
@@ -773,41 +772,51 @@ our $standard_output_options = {
     }
 };
 
-sub add_standard_output_parameters {
-    my ($org_schema) = @_;
+sub add_standard_output_properties {
+    my ($propdef, $list) = @_;
 
-    my $schema = { %$org_schema };
-    $schema->{properties} = { %{$schema->{properties}}, %$standard_output_options };
+    $propdef //= {};
 
-    return $schema;
-};
+    $list //= [ keys %$standard_output_options ];
+
+    my $res = { %$propdef }; # copy
+
+    foreach my $opt (@$list) {
+	die "no such standard output option '$opt'\n" if !defined($standard_output_options->{$opt});
+	die "detected overwriten standard CLI parameter '$opt'\n" if defined($res->{$opt});
+	$res->{$opt} = $standard_output_options->{$opt};
+    }
+
+    return $res;
+}
+
+sub extract_standard_output_properties {
+    my ($data) = @_;
+
+    my $options = {};
+    foreach my $opt (keys %$standard_output_options) {
+	$options->{$opt} = delete $data->{$opt} if defined($data->{$opt});
+    }
+
+    return $options;
+}
 
 sub cli_handler {
-    my ($self, $prefix, $name, $args, $arg_param, $fixed_param, $param_cb, $options) = @_;
+    my ($self, $prefix, $name, $args, $arg_param, $fixed_param, $param_cb) = @_;
 
     my $info = $self->map_method_by_name($name);
-    $options //= {};
-
-    my $add_stdopts = $options->{'std-output-opts'};
 
     my $res;
     eval {
 	my $param_map = {};
 	$param_map = $compute_param_mapping_hash->($param_cb->($name)) if $param_cb;
-	my $schema = $add_stdopts ? add_standard_output_parameters($info->{parameters}) : $info->{parameters};
-	my $param = PVE::JSONSchema::get_options($schema, $args, $arg_param, $fixed_param, $param_map);
-
-	if ($add_stdopts) {
-	    foreach my $opt (keys %$standard_output_options) {
-		$options->{$opt} = delete $param->{$opt} if defined($param->{$opt});
-	    }
-	}
+	my $param = PVE::JSONSchema::get_options($info->{parameters}, $args, $arg_param, $fixed_param, $param_map);
 
 	if (defined($param_map)) {
 	    $replace_file_names_with_contents->($param, $param_map);
 	}
 
-	$res = $self->handle($info, $param, $options);
+	$res = $self->handle($info, $param);
     };
     if (my $err = $@) {
 	my $ec = ref($err);
-- 
2.11.0




More information about the pve-devel mailing list