]> git.proxmox.com Git - pve-common.git/commitdiff
cli: remove all output formatter magic from CLIHandler
authorDietmar Maurer <dietmar@proxmox.com>
Tue, 24 Jul 2018 11:36:42 +0000 (13:36 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Thu, 26 Jul 2018 09:08:52 +0000 (11:08 +0200)
- 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()

src/PVE/CLIFormatter.pm
src/PVE/CLIHandler.pm
src/PVE/RESTHandler.pm

index 7a92f7a286dbd630a34fe392e37d9cade211fe37..22cd8f2378c803bbc977646f1ff8468b62dc89fc 100644 (file)
@@ -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';
 
index 71457070168ebaf809d1c7850c1c0ca9667d5bd1..2f59d2b7696ed9b510014d5a92e21bd57b5590b3 100644 (file)
@@ -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);
     }
 };
 
index 205e6efd130bf6d2e6079d67efc1742277b67f64..148b4d8654875e7d3d406b472ef1f44f778ff96f 100644 (file)
@@ -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);