From a8aa5b63711a9b0ee8536aac91901b1b38609c09 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Thu, 24 May 2018 13:28:28 +0200 Subject: [PATCH] improve bash completion --- pveclient | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/pveclient b/pveclient index d8d4d15..091af96 100755 --- a/pveclient +++ b/pveclient @@ -60,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') { @@ -76,22 +81,35 @@ if ($cmd eq 'get') { die "implement me"; } elsif ($cmd eq 'delete') { die "implement me"; -} elsif ($cmd eq 'lxc') { - PVE::APIClient::Commands::lxc->run_cli_handler(); -} 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') { - my $cmdline = substr($ENV{COMP_LINE}, 0, $ENV{COMP_POINT}); - $cmdline =~ s/$cur$//; + 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 ($bash_command, $cur, $prev) = @ARGV; + my $cmdline = substr($ENV{COMP_LINE}, 0, $ENV{COMP_POINT}); + my ($bash_command, $cur, $prev) = @ARGV; + $cmdline =~ s/$cur$//; - my $args = [split(/\s+/, $cmdline)]; + my $args = PVE::Tools::split_args($cmdline); - if (scalar(@$args) == 1) { - foreach my $p (qw(get set create delete lxc remote)) { - print "$p\n" if $p =~ m/^$cur/; + 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/; + } } } -- 2.39.2