5
0
mirror of git://git.proxmox.com/git/pve-common.git synced 2024-12-22 21:33:47 +03:00

cli: remove all output formatter magic from CLIHandler

- 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()
This commit is contained in:
Dietmar Maurer 2018-07-24 13:36:42 +02:00
parent bf5e9f8440
commit 4cbcd1386f
3 changed files with 37 additions and 32 deletions

View 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';

View 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);
}
};

View 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);