[pve-devel] r5073 - in pve-manager/pve2: . bin

svn-commits at proxmox.com svn-commits at proxmox.com
Fri Aug 27 11:39:18 CEST 2010


Author: dietmar
Date: 2010-08-27 09:39:18 +0000 (Fri, 27 Aug 2010)
New Revision: 5073

Modified:
   pve-manager/pve2/ChangeLog
   pve-manager/pve2/bin/pvesh
Log:
better help 


Modified: pve-manager/pve2/ChangeLog
===================================================================
--- pve-manager/pve2/ChangeLog	2010-08-27 09:38:10 UTC (rev 5072)
+++ pve-manager/pve2/ChangeLog	2010-08-27 09:39:18 UTC (rev 5073)
@@ -2,6 +2,7 @@
 
 	* bin/pvesh (list_dir): do not display all data - instead display
 	some info about available methods.
+	(pve_command): better 'help' implementation
 
 2010-08-26  Proxmox Support Team  <support at proxmox.com>
 

Modified: pve-manager/pve2/bin/pvesh
===================================================================
--- pve-manager/pve2/bin/pvesh	2010-08-27 09:38:10 UTC (rev 5072)
+++ pve-manager/pve2/bin/pvesh	2010-08-27 09:39:18 UTC (rev 5073)
@@ -9,6 +9,7 @@
 use strict;
 use Term::ReadLine;
 use File::Basename;
+use Getopt::Long;
 use HTTP::Status qw(:constants :is status_message);
 use Text::ParseWords;
 use PVE::JSONSchema;
@@ -73,6 +74,23 @@
     return $input;
 };
 
+sub reverse_map_cmd {
+    my $method = shift;
+
+    my $mmap = {
+	GET => 'get',
+	PUT => 'set',
+	POST => 'create',
+	DELETE => 'delete',
+    };
+
+    my $cmd = $mmap->{$method};
+
+    die "got strange value for method ('$method') - internal error" if !$cmd;
+
+    return $cmd;
+}
+
 sub map_cmd {
     my $cmd = shift;
 
@@ -114,20 +132,68 @@
     return $data;
 }
 
+sub find_resource_methods {
+    my ($path, $ihash) = @_;
+
+    for my $method (qw(GET POST PUT DELETE)) {
+	my $uri_param = {};
+	my ($handler, $info, $pm) = PVE::API2->find_handler($method, $path, $uri_param);
+	if ($handler && $info && !$ihash->{$info}) {
+	    $ihash->{$info} = {
+		path => $pm,
+		handler => $handler, 
+		info => $info, 
+		uri_param => $uri_param,
+	    };
+	}
+    }
+}
+
 sub print_help {
-    my ($cmd, $dir) = @_;
+    my ($path, $opts) = @_;
 
-    my $method = map_cmd($cmd);
+    my $ihash = {};
 
+    find_resource_methods($path, $ihash);
+
+    if (!scalar(keys(%$ihash))) {
+	die "no such resource\n";
+    }
+
     my $uri_param = {};
-    my ($handler, $info) = PVE::API2->find_handler($method, $dir, $uri_param);
-    if (!$handler || !$info) {
-	die "no '$cmd' handler for '$dir'\n";
+    my ($handler, $info, $pm) = PVE::API2->find_handler('GET', $path, $uri_param);
+    if ($handler && $info) {
+	# gather info about children	
+	eval {
+	    my $data = $handler->cli_handler2("ls $path", $info->{name}, [], [], $uri_param, $read_password);
+	    my $lnk = PVE::JSONSchema::method_get_child_link($info);
+	    my $children = extract_children($lnk, $data);
+
+	    foreach my $c (@$children) {
+		my $cp = abs_path($path, $c);
+		find_resource_methods($cp, $ihash);
+	    }
+	}
     }
 
-    print $handler->usage_str($info->{name}, "$cmd $dir", [], $uri_param, 'full', 1);
-}
+    foreach my $mi (sort { $a->{path} cmp $b->{path} } values %$ihash) {
+	my $method = $mi->{info}->{method};
 
+	# we skip index methods for now.
+	next if ($method eq 'GET') && PVE::JSONSchema::method_get_child_link($mi->{info});
+
+	my $path = $mi->{path};
+	$path =~ s|/+$||; # remove trailing slash
+
+	my $cmd = reverse_map_cmd($method);
+
+	print $mi->{handler}->usage_str($mi->{info}->{name}, "$cmd $path", [], $mi->{uri_param}, 
+					$opts->{verbose} ? 'full' : 'short', 1);
+	print "\n\n" if $opts->{verbose};
+    }
+ 
+};
+
 sub resource_cap {
     my ($path) = @_;
 
@@ -168,6 +234,27 @@
     return $res;
 }
 
+sub extract_children {
+    my ($lnk, $data) = @_;
+
+    my $res = [];
+
+    return $res if !($lnk && $data);
+
+    my $href = $lnk->{href};
+    if ($href =~ m/^\{(\S+)\}$/) {
+	my $prop = $1;
+
+	foreach my $elem (sort {$a->{$prop} cmp $b->{$prop}} @$data) {
+	    next if !ref($elem);
+	    my $value = $elem->{$prop};
+	    push @$res, $value;
+	}
+    }
+
+    return $res;
+}
+
 sub list_dir {
     my ($dir, $args) = @_;
 
@@ -181,25 +268,13 @@
 	die "resource does not define child links\n";
     }
 
-    my $data = $handler->cli_handler2("ls $dir", $info->{name}, $args, [], $uri_param, $read_password);
- 
+    my $data = $handler->cli_handler2("ls $dir", $info->{name}, $args, [], $uri_param, $read_password); 
     my $lnk = PVE::JSONSchema::method_get_child_link($info);
-    if ($lnk && $data) {
-	my $href = $lnk->{href};
-	if ($href =~ m/^\{(\S+)\}$/) {
-	    my $prop = $1;
+    my $children = extract_children($lnk, $data);
 
-	    foreach my $elem (sort {$a->{$prop} cmp $b->{$prop}} @$data) {
-		next if !ref($elem);
-
-		if (defined(my $value = $elem->{$prop})) {
-		    if ($value ne '') {
-			my $cap = resource_cap(abs_path($dir, $value));
-			print "$cap $value\n";
-		    }
-		}
-	    }
-	}
+    foreach my $c (@$children) {
+	my $cap = resource_cap(abs_path($dir, $c));
+	print "$cap $c\n";
     }
 }
 
@@ -210,9 +285,6 @@
 
     my $cmd = shift @$args;
 
-    #print "CMD: $cmd\n";
-    #print "ARGS: " . join (' ', @$args) . "\n";
-    
     if ($cmd eq 'cd') {
 
 	my $path =  shift @$args;
@@ -231,17 +303,27 @@
 
     } elsif ($cmd eq 'help') {
 
-	my $cmd = shift @$args;
+	my $help_usage_error = sub {
+	    die "usage: help [path] [--verbose]\n";
+	};
 
+	my $opts = {};
+
+	&$help_usage_error() if !Getopt::Long::GetOptionsFromArray($args, $opts, 'verbose');
+
 	my $path;
 	if (scalar(@$args) && $args->[0] !~ m/^\-/)  {
 	    $path = shift @$args;
 	}
 
-	die "usage: help <get|set|create|delete|ls> [path]\n" if !$cmd || scalar(@$args);
+	&$help_usage_error() if scalar(@$args);
 
-	print_help($cmd, abs_path($cdir, $path));
+	print "help [path] [--verbose]\n";
+	print "cd [path]\n";
+	print "ls [path]\n\n";
 
+	print_help(abs_path($cdir, $path), $opts);
+
     } elsif ($cmd eq 'ls') {
 	my $path;
 	if (scalar(@$args) && $args->[0] !~ m/^\-/)  {




More information about the pve-devel mailing list