]> git.proxmox.com Git - pve-client.git/blobdiff - pveclient
improve bash completion
[pve-client.git] / pveclient
index 165505fe45f33b485c5029d955234890827ec93e..091af96bb160a2ee7ab79a3e029794984770a9d0 100755 (executable)
--- a/pveclient
+++ b/pveclient
@@ -12,6 +12,7 @@ use PVE::CLIHandler;
 use PVE::APIClient::LWP;
 use PVE::APIClient::Helpers;
 use PVE::APIClient::Commands::remote;
+use PVE::APIClient::Commands::lxc;
 
 use JSON;
 
@@ -21,6 +22,8 @@ sub print_usage {
 
     $text .= "pveclient remote <help|add|remove> {options}\n\n";
 
+    $text .= "pveclient lxc <help|create|destroy|...> {options}\n\n";
+
     $text .= "pveclient <get/set/create/delete> <path> {options}\n\n";
 
     print STDERR $text;
@@ -57,6 +60,11 @@ sub call_method {
 #my $res = $conn->get("/", {});
 #print to_json($res, { pretty => 1, canonical => 1});
 
+my $cli_class_handlers = {
+    lxc => 'PVE::APIClient::Commands::lxc',
+    remote => 'PVE::APIClient::Commands::remote',
+};
+
 my $cmd = shift || (print_usage() && exit(-1));
 
 if ($cmd eq 'get') {
@@ -73,8 +81,38 @@ if ($cmd eq 'get') {
     die "implement me";
 } elsif ($cmd eq 'delete') {
     die "implement me";
-} elsif ($cmd eq 'remote') {
-    PVE::APIClient::Commands::remote->run_cli_handler();
+} elsif (my $class = $cli_class_handlers->{$cmd}) {
+    $class->run_cli_handler();
+} elsif ($cmd eq 'bashcomplete') {
+
+    exit(0) if !(defined($ENV{COMP_LINE}) && defined($ENV{COMP_POINT}));
+
+    my $cmdlist = join('|', keys %$cli_class_handlers);
+    if ($ENV{COMP_LINE} =~ m/^(.*pveclient\s+($cmdlist)\s+)(.*)$/) {
+       my $cmd = $2;
+       my $class = $cli_class_handlers->{$cmd} || die "internal error";
+       $ENV{COMP_LINE} = "pveclient $3";
+       $ENV{COMP_POINT} = length($ENV{COMP_LINE});
+       @ARGV = ('bashcomplete', 'pveclient', $ARGV[1], $ARGV[2]);
+
+       $class->run_cli_handler();
+
+    } else {
+
+       my $cmdline = substr($ENV{COMP_LINE}, 0, $ENV{COMP_POINT});
+       my ($bash_command, $cur, $prev) = @ARGV;
+       $cmdline =~ s/$cur$//;
+
+       my $args = PVE::Tools::split_args($cmdline);
+
+       my @cmds = ('get', 'set', 'create', 'delete', keys %$cli_class_handlers);
+       if (scalar(@$args) == 1) {
+           foreach my $p (@cmds) {
+               print "$p\n" if $p =~ m/^$cur/;
+           }
+       }
+    }
+
 } else {
     print_usage();
 }