[pve-devel] [PATCH common 2/3] cli: code cleanup

Philip Abernethy p.abernethy at proxmox.com
Fri Oct 6 13:14:36 CEST 2017


Removes obsolete subroutine and some unnecessary parameters for command
handlers.
---
 src/PVE/CLIHandler.pm | 89 +++++++++++++++++----------------------------------
 1 file changed, 30 insertions(+), 59 deletions(-)

diff --git a/src/PVE/CLIHandler.pm b/src/PVE/CLIHandler.pm
index 13bd168..d480e70 100644
--- a/src/PVE/CLIHandler.pm
+++ b/src/PVE/CLIHandler.pm
@@ -414,29 +414,6 @@ complete -o default -C '$exename bashcomplete' $exename
 __EOD__
 }
 
-sub find_cli_class_source {
-    my ($name) = @_;
-
-    my $filename;
-
-    $name =~ s/-/_/g;
-
-    my $cpath = "PVE/CLI/${name}.pm";
-    my $spath = "PVE/Service/${name}.pm";
-    foreach my $p (@INC) {
-	foreach my $s (($cpath, $spath)) {
-	    my $testfn = "$p/$s";
-	    if (-f $testfn) {
-		$filename = $testfn;
-		last;
-	    }
-	}
-	last if defined($filename);
-    }
-
-    return $filename;
-}
-
 sub generate_asciidoc_synopsys {
     my ($class) = @_;
     $class->generate_asciidoc_synopsis();
@@ -470,71 +447,68 @@ sub setup_environment {
 }
 
 my $handle_cmd  = sub {
-    my ($def, $cmdname, $cmd, $args, $pwcallback, $preparefunc, $stringfilemap) = @_;
-
-    $cmddef = $def;
-    $exename = $cmdname;
+    my ($args, $pwcallback, $preparefunc, $stringfilemap) = @_;
 
     $cmddef->{help} = [ __PACKAGE__, 'help', ['cmd'] ];
 
+    my @cmd;
+    my $base = $cmddef;
+    while (scalar(@$args) > 0) {
+	last if (ref($base) eq 'ARRAY');
+	push @cmd, &$expand_command_name($base, shift @$args);
+	$base = $base->{$cmd[-1]};
+	if (ref($base) eq 'HASH' && defined($base->{alias})) {
+	    my @alias = split(/ /, $base->{alias});
+	    $base = $cmddef;
+	    undef(@cmd);
+	    while (@alias) {
+		unshift @$args, pop @alias;
+	    }
+	}
+    }
+
     # call verifyapi before setup_environment(), because we do not want to
     # execute any real code in this case
 
-    if (!defined($cmd->[0])) {
+    if (!defined($cmd[0])) {
 	print_usage_short (\*STDERR, "no command specified");
 	exit (-1);
-    } elsif ($cmd->[0] eq 'verifyapi') {
+    } elsif ($cmd[0] eq 'verifyapi') {
 	PVE::RESTHandler::validate_method_schemas();
 	return;
     }
 
     $cli_handler_class->setup_environment();
 
-    if ($cmd->[0] eq 'bashcomplete') {
+    if ($cmd[0] eq 'bashcomplete') {
 	&$print_bash_completion($cmddef, 0, @$args);
 	return;
     }
 
     &$preparefunc() if $preparefunc;
 
-    unshift @$args, shift @$cmd;
-    my $base = $def;
-    while (scalar(@$args) > 0) {
-	last if (ref($base) eq 'ARRAY');
-	push @$cmd, &$expand_command_name($base, shift @$args);
-	$base = $base->{$cmd->[-1]};
-	if (ref($base) eq 'HASH' && defined($base->{alias})) {
-	    my @alias = split(/ /, $base->{alias});
-	    $base = $def;
-	    undef(@$cmd);
-	    while (@alias) {
-		unshift @$args, pop @alias;
-	    }
-	}
-    }
-
     if (ref($base) eq 'HASH') {
-	&$print_help_short (\*STDERR, $cmd, "incomplete command '" . join(' ', @$cmd) . "'");
+	&$print_help_short (\*STDERR, \@cmd, "incomplete command '" . join(' ', @cmd) . "'");
 	exit (-1);
     }
 
     my ($class, $name, $arg_param, $uri_param, $outsub) = @{$base || []};
 
     if (!$class) {
-	print_usage_short (\*STDERR, "unknown command '" . join(' ', @$cmd) . "'");
+	print_usage_short (\*STDERR, "unknown command '" . join(' ', @cmd) . "'");
 	exit (-1);
     }
 
-    my $prefix = "$exename " . join(' ', @$cmd);
+    my $prefix = "$exename " . join(' ', @cmd);
     my $res = $class->cli_handler($prefix, $name, \@ARGV, $arg_param, $uri_param, $pwcallback, $stringfilemap);
 
     &$outsub($res) if $outsub;
 };
 
 my $handle_simple_cmd = sub {
-    my ($def, $args, $pwcallback, $preparefunc, $stringfilemap) = @_;
+    my ($args, $pwcallback, $preparefunc, $stringfilemap) = @_;
 
-    my ($class, $name, $arg_param, $uri_param, $outsub) = @{$def};
+    my ($class, $name, $arg_param, $uri_param, $outsub) = @{$cmddef};
     die "no class specified" if !$class;
 
     if (scalar(@$args) >= 1) {
@@ -554,7 +528,7 @@ my $handle_simple_cmd = sub {
     if (scalar(@$args) >= 1) {
 	if ($args->[0] eq 'bashcomplete') {
 	    shift @$args;
-	    &$print_bash_completion({ $name => $def }, $name, @$args);
+	    &$print_bash_completion({ $name => $cmddef }, $name, @$args);
 	    return;
 	}
     }
@@ -601,15 +575,12 @@ sub run_cli_handler {
     initlog($exename);
 
     no strict 'refs';
-    my $def = ${"${class}::cmddef"};
-    $cmddef = $def;
+    $cmddef = ${"${class}::cmddef"};
 
-    if (ref($def) eq 'ARRAY') {
-	&$handle_simple_cmd($def, \@ARGV, $pwcallback, $preparefunc, $stringfilemap);
+    if (ref($cmddef) eq 'ARRAY') {
+	&$handle_simple_cmd(\@ARGV, $pwcallback, $preparefunc, $stringfilemap);
     } else {
-	$cmddef = $def;
-	my @cmd = shift @ARGV;
-	&$handle_cmd($cmddef, $exename, \@cmd, \@ARGV, $pwcallback, $preparefunc, $stringfilemap);
+	&$handle_cmd(\@ARGV, $pwcallback, $preparefunc, $stringfilemap);
     }
 
     exit 0;
-- 
2.11.0





More information about the pve-devel mailing list