[pve-devel] applied: [pve-manager 1/6] pvesh: do not use CLIHandler $option parameter, simplify code

Dietmar Maurer dietmar at proxmox.com
Thu Jul 26 13:14:13 CEST 2018


Signed-off-by: Dietmar Maurer <dietmar at proxmox.com>
---
 bin/pvesh | 83 +++++++++++++++++++++++++++++++++------------------------------
 1 file changed, 44 insertions(+), 39 deletions(-)

diff --git a/bin/pvesh b/bin/pvesh
index 36c9a900..5dab90c3 100755
--- a/bin/pvesh
+++ b/bin/pvesh
@@ -103,18 +103,17 @@ sub check_proxyto {
 }
 
 sub proxy_handler {
-    my ($node, $remip, $path, $cmd, $param, $noout) = @_;
+    my ($node, $remip, $path, $cmd, $param) = @_;
 
     my $args = [];
     foreach my $key (keys %$param) {
+	next if $key eq 'quiet' || $key eq 'output-format'; # just to  be sure
 	push @$args, "--$key", $param->{$key};
     }
 
-    push @$args, '--quiet' if $noout;
-
     my $remcmd = ['ssh', '-o', 'BatchMode=yes', "root\@$remip",
 		  'pvesh', '--noproxy', $cmd, $path,
-		  '--format', 'json'];
+		  '--output-format', 'json'];
 
     if (scalar(@$args)) {
 	my $cmdargs = [String::ShellQuote::shell_quote(@$args)];
@@ -202,7 +201,6 @@ sub extract_path_info {
 
 
 my $path_properties = {};
-my $path_returns = { type => 'null' };
 
 my $api_path_property = {
     description => "API path.",
@@ -219,7 +217,6 @@ if (my $info = extract_path_info($uri_param)) {
 	next if defined($uri_param->{$key});
 	$path_properties->{$key} = $info->{parameters}->{properties}->{$key};
     }
-    $path_returns = $info->{returns};
 }
 
 $path_properties->{api_path} = $api_path_property;
@@ -229,23 +226,18 @@ $path_properties->{noproxy} = {
     optional => 1,
 };
 
-my $format_result = sub {
-    my ($data, $schema, $options) = @_;
-
-    return if $opt_nooutput || $options->{quiet}; # fixme:??
-
-    PVE::CLIFormatter::print_api_result($data, $path_returns, undef, $options);
-};
-
 sub call_api_method {
-    my ($cmd, $param, $options) = @_;
+    my ($cmd, $param) = @_;
 
     my $method = $method_map->{$cmd} || die "unable to map command '$cmd'";
 
     my $path = PVE::Tools::extract_param($param, 'api_path');
     die "missing API path\n" if !defined($path);
 
-    $opt_nooutput = 1 if $options->{quiet};
+    my $stdopts =  PVE::RESTHandler::extract_standard_output_properties($param);
+    PVE::CLIFormatter::query_terminal_options($stdopts);
+
+    $opt_nooutput = 1 if $stdopts->{quiet};
 
     my $uri_param = {};
     my ($handler, $info) = PVE::API2->find_handler($method, $path, $uri_param);
@@ -253,16 +245,21 @@ sub call_api_method {
 	die "no '$cmd' handler for '$path'\n";
     }
 
+    my $data;
     my ($node, $remip) = check_proxyto($info, $uri_param);
-    return proxy_handler($node, $remip, $path, $cmd, $param, $opt_nooutput) if $node;
+    if ($node) {
+	$data = proxy_handler($node, $remip, $path, $cmd, $param);
+    } else {
+	foreach my $p (keys %$uri_param) {
+	    $param->{$p} = $uri_param->{$p};
+	}
 
-    foreach my $p (keys %$uri_param) {
-	$param->{$p} = $uri_param->{$p};
+	$data = $handler->handle($info, $param);
     }
 
-    my $data = $handler->handle($info, $param, $options);
+    return if $opt_nooutput || $stdopts->{quiet};
 
-    return $data;
+    PVE::CLIFormatter::print_api_result($data, $info->{returns}, undef, $stdopts);
 }
 
 __PACKAGE__->register_method ({
@@ -272,13 +269,15 @@ __PACKAGE__->register_method ({
     description => "Call API GET on <api_path>.",
     parameters => {
 	additionalProperties => 0,
-	properties => $path_properties,
+	properties => PVE::RESTHandler::add_standard_output_properties($path_properties),
     },
-    returns => $path_returns,
+    returns => { type => 'null' },
     code => sub {
-	my ($param, $options) = @_;
+	my ($param) = @_;
+
+	call_api_method('get', $param);
 
-	return call_api_method('get', $param, $options);
+	return undef;
     }});
 
 __PACKAGE__->register_method ({
@@ -290,11 +289,13 @@ __PACKAGE__->register_method ({
 	additionalProperties => 0,
 	properties => $path_properties,
     },
-    returns => $path_returns,
+    returns => { type => 'null' },
     code => sub {
-	my ($param, $options) = @_;
+	my ($param) = @_;
+
+	call_api_method('set', $param);
 
-	return call_api_method('set', $param, $options);
+	return undef;
     }});
 
 __PACKAGE__->register_method ({
@@ -306,11 +307,13 @@ __PACKAGE__->register_method ({
 	additionalProperties => 0,
 	properties => $path_properties,
     },
-    returns => $path_returns,
+    returns => { type => 'null' },
     code => sub {
-	my ($param, $options) = @_;
+	my ($param) = @_;
 
-	return call_api_method('create', $param, $options);
+	call_api_method('create', $param);
+
+	return undef;
     }});
 
 __PACKAGE__->register_method ({
@@ -322,11 +325,13 @@ __PACKAGE__->register_method ({
 	additionalProperties => 0,
 	properties => $path_properties,
     },
-    returns => $path_returns,
+    returns => { type => 'null' },
     code => sub {
-	my ($param, $options) = @_;
+	my ($param) = @_;
 
-	return call_api_method('delete', $param, $options);
+	call_api_method('delete', $param);
+
+	return undef;
     }});
 
 __PACKAGE__->register_method ({
@@ -358,7 +363,7 @@ __PACKAGE__->register_method ({
     },
     returns => { type => 'null' },
     code => sub {
-	my ($param, $options) = @_;
+	my ($param) = @_;
 
 	my $path = $param->{api_path};
 
@@ -398,10 +403,10 @@ __PACKAGE__->register_method ({
 
 our $cmddef = {
     usage => [ __PACKAGE__, 'usage', ['api_path']],
-    get => [ __PACKAGE__, 'get', ['api_path'], {}, $format_result ],
-    set => [ __PACKAGE__, 'set', ['api_path'], {}, $format_result ],
-    create => [ __PACKAGE__, 'create', ['api_path'], {}, $format_result ],
-    delete => [ __PACKAGE__, 'delete', ['api_path'], {}, $format_result ],
+    get => [ __PACKAGE__, 'get', ['api_path']],
+    set => [ __PACKAGE__, 'set', ['api_path']],
+    create => [ __PACKAGE__, 'create', ['api_path']],
+    delete => [ __PACKAGE__, 'delete', ['api_path']],
 };
 
 my $cmd = $ARGV[0];
-- 
2.11.0




More information about the pve-devel mailing list