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

svn-commits at proxmox.com svn-commits at proxmox.com
Fri Aug 27 12:43:46 CEST 2010


Author: dietmar
Date: 2010-08-27 10:43:46 +0000 (Fri, 27 Aug 2010)
New Revision: 5074

Modified:
   pve-manager/pve2/ChangeLog
   pve-manager/pve2/bin/pvesh
Log:
	implement simple completion function


Modified: pve-manager/pve2/ChangeLog
===================================================================
--- pve-manager/pve2/ChangeLog	2010-08-27 09:39:18 UTC (rev 5073)
+++ pve-manager/pve2/ChangeLog	2010-08-27 10:43:46 UTC (rev 5074)
@@ -2,7 +2,8 @@
 
 	* bin/pvesh (list_dir): do not display all data - instead display
 	some info about available methods.
-	(pve_command): better 'help' implementation
+	(pve_command): better 'help' implementation,
+	implement simple completion function
 
 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:39:18 UTC (rev 5073)
+++ pve-manager/pve2/bin/pvesh	2010-08-27 10:43:46 UTC (rev 5074)
@@ -3,9 +3,9 @@
 # NOTE: this is just an experimental prototype
 
 # TODO:
-# implement nice help messages (use JSON-Schema to generate them)
 # implement auto-completion
-
+# implement persistent history ?
+ 
 use strict;
 use Term::ReadLine;
 use File::Basename;
@@ -35,7 +35,42 @@
 print "entering PVE shell - type 'help' for help\n";
 
 my $term = new Term::ReadLine ('pvesh');
+my $attribs = $term->Attribs;
 
+$attribs->{completion_function} = sub {
+    my ($text, $line, $start) = @_;
+
+    my $prefix = substr($line, 0, $start);
+    if ($prefix =~ /^\s*$/) { # first word (command completeion)
+	return qw(help ls cd get set create delete quit);
+    }
+
+    if ($prefix =~ /^\s*\S+\s+$/) { # second word (path completion)
+	my ($dir, undef, $rest) = $text =~ m|^(.+/)?(([^/]*))?$|;
+    
+	my $path = abs_path($cdir, $dir);
+
+	my @res = ();
+
+	my $uri_param = {};
+	my ($handler, $info) = PVE::API2->find_handler('GET', $path, $uri_param);
+	if ($handler && $info) {
+	    eval {
+		my $data = $handler->handle($info, $uri_param);
+		my $lnk = PVE::JSONSchema::method_get_child_link($info);
+		my $children = extract_children($lnk, $data);
+
+		foreach my $c (@$children) {
+		    push @res, $dir ? "$dir$c" : $c;
+		}
+	    }; # ignore errors
+	}
+	return @res;
+    }
+
+    return qw();
+};
+
 sub abs_path {
     my ($current, $path) = @_;
 
@@ -165,7 +200,7 @@
     if ($handler && $info) {
 	# gather info about children	
 	eval {
-	    my $data = $handler->cli_handler2("ls $path", $info->{name}, [], [], $uri_param, $read_password);
+	    my $data = $handler->handle($info, $uri_param);
 	    my $lnk = PVE::JSONSchema::method_get_child_link($info);
 	    my $children = extract_children($lnk, $data);
 
@@ -173,7 +208,7 @@
 		my $cp = abs_path($path, $c);
 		find_resource_methods($cp, $ihash);
 	    }
-	}
+	}; # ignore errors ?
     }
 
     foreach my $mi (sort { $a->{path} cmp $b->{path} } values %$ihash) {
@@ -382,6 +417,9 @@
     if ($input =~ m/^\s*q(uit)?\s*$/) {
 	exit (0);
     }
+
+    $term->addhistory($input);
+
     eval {
 	pve_command ($input);
     };




More information about the pve-devel mailing list