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

Philip Abernethy p.abernethy at proxmox.com
Wed Oct 11 18:58:06 CEST 2017


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

diff --git a/src/PVE/CLIHandler.pm b/src/PVE/CLIHandler.pm
index 683403b..7131bf8 100644
--- a/src/PVE/CLIHandler.pm
+++ b/src/PVE/CLIHandler.pm
@@ -446,29 +446,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();
@@ -502,60 +479,58 @@ 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');
+	# Auto-complete commands
+	push @cmd, &$expand_command_name($base, shift @$args);
+	$base = $base->{$cmd[-1]};
+	if (ref($base) eq 'HASH' && defined($base->{alias})) {
+	    # If command is an alias, reset $base and move to aliased command
+	    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') {
-	&$print_bash_completion($cmddef, 0, @$args);
+    if ($cmd[0] eq 'bashcomplete') {
+	shift @cmd;
+	&$print_bash_completion($cmddef, 0, @cmd);
 	return;
     }
 
     &$preparefunc() if $preparefunc;
 
-    unshift @$args, shift @$cmd;
-    my $base = $def;
-    while (scalar(@$args) > 0) {
-	last if (ref($base) eq 'ARRAY');
-	# Auto-complete commands
-	push @$cmd, &$expand_command_name($base, shift @$args);
-	$base = $base->{$cmd->[-1]};
-	if (ref($base) eq 'HASH' && defined($base->{alias})) {
-	    # If command is an alias, reset $base and move to aliased command
-	    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);
     } elsif ($name eq 'help') {
 	# Find command help is wanted for
@@ -567,16 +542,16 @@ my $handle_cmd  = sub {
 	unshift @ARGV, join(' ', @help_cmd);
     }
 
-    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) {
@@ -596,7 +571,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;
 	}
     }
@@ -643,15 +618,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