]> git.proxmox.com Git - pve-client.git/blob - pveclient
fix bash completion for simple command classes like 'list'.
[pve-client.git] / pveclient
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use Cwd 'abs_path';
6 use lib '/usr/share/pve-client';
7 use lib '.';
8 use Data::Dumper;
9
10 use PVE::JSONSchema;
11 use PVE::CLIHandler;
12
13 use PVE::APIClient::LWP;
14 use PVE::APIClient::Helpers;
15 use PVE::APIClient::Commands::remote;
16 use PVE::APIClient::Commands::list;
17 use PVE::APIClient::Commands::lxc;
18 use PVE::APIClient::Commands::help;
19
20 use JSON;
21
22 sub call_method {
23 my ($path, $method, $args) = @_;
24
25 die "missing API path\n" if !defined($path);
26
27 my $info = PVE::APIClient::Helpers::lookup_api_method($path, $method);
28 my $param = PVE::JSONSchema::get_options($info->{parameters}, $args);
29 print Dumper($param);
30
31 die "implement me";
32 }
33
34
35 my $cli_class_handlers = {
36 list => 'PVE::APIClient::Commands::list',
37 lxc => 'PVE::APIClient::Commands::lxc',
38 remote => 'PVE::APIClient::Commands::remote',
39 help => 'PVE::APIClient::Commands::help',
40 };
41
42 my $cmd = shift;
43 if (!defined($cmd)) {
44 PVE::APIClient::Commands::help->help({});
45 exit(-1);
46 }
47
48 my $method_map = {
49 create => 'POST',
50 set => 'PUT',
51 get => 'GET',
52 delete => 'DELETE',
53 };
54
55 if (my $method = $method_map->{$cmd}) {
56 my $path;
57 if (scalar(@ARGV) && $ARGV[0] !~ m/^\-/) {
58 $path = shift @ARGV;
59 }
60 my $res = call_method($path, $method, \@ARGV);
61 die "implement me";
62 } elsif (my $class = $cli_class_handlers->{$cmd}) {
63 $class->run_cli_handler();
64 } elsif ($cmd eq 'bashcomplete') {
65
66 exit(0) if !(defined($ENV{COMP_LINE}) && defined($ENV{COMP_POINT}));
67
68 my $cmdlist = join('|', keys %$cli_class_handlers);
69 if ($ENV{COMP_LINE} =~ m/^(.*pveclient\s+($cmdlist)\s+)(.*)$/) {
70 my $cmd = $2;
71 my $class = $cli_class_handlers->{$cmd} || die "internal error";
72
73 if ($cmd eq 'list') { # simple commands
74 $ENV{COMP_LINE} = "pveclient $3";
75 $ENV{COMP_POINT} = length($ENV{COMP_LINE});
76 @ARGV = ('bashcomplete', 'pveclient', $ARGV[1], $ARGV[2]);
77 } else {
78 $ENV{COMP_LINE} = "pveclient $3";
79 $ENV{COMP_POINT} = length($ENV{COMP_LINE});
80 @ARGV = ('bashcomplete', 'pveclient', $ARGV[1], $ARGV[2]);
81 }
82 $class->run_cli_handler();
83
84 } else {
85
86 my $cmdline = substr($ENV{COMP_LINE}, 0, $ENV{COMP_POINT});
87 my ($bash_command, $cur, $prev) = @ARGV;
88 $cmdline =~ s/\Q$cur\E$//;
89
90 my $args = PVE::Tools::split_args($cmdline);
91
92 my @cmds = (keys %$method_map, keys %$cli_class_handlers);
93 if (scalar(@$args) == 1) {
94 foreach my $p (@cmds) {
95 print "$p\n" if $p =~ m/^$cur/;
96 }
97 } elsif (scalar(@$args) == 2) {
98 if (my $method = $method_map->{$args->[1]}) {
99 PVE::APIClient::Helpers::complete_api_path($cur);
100 }
101 } elsif (scalar(@$args) >= 3) {
102 my $path = $args->[2];
103 if (my $method = $method_map->{$args->[1]}) {
104 if (my $info = PVE::APIClient::Helpers::lookup_api_method($path, $method, 1)) {
105 my $prop = $info->{parameters}->{properties};
106 PVE::APIClient::Helpers::complete_api_call_options($method, $prop, $prev, $cur, $args);
107 }
108 }
109 }
110 }
111
112 } elsif ($cmd eq 'packagedepends') {
113 # experimental code to print required perl packages
114 my $packages = {};
115 my $dir = Cwd::getcwd;
116
117 foreach my $k (keys %INC) {
118 my $file = abs_path($INC{$k});
119 next if $file =~ m/^\Q$dir\E/;
120 my $res = `dpkg -S '$file'`;
121 if ($res && $res =~ m/^(\S+): $file$/) {
122 my $debian_package = $1;
123 $debian_package =~ s/:amd64$//;
124 $packages->{$debian_package} = 1;
125 } else {
126 die "unable to find package for '$file'\n";
127 }
128 }
129 print join("\n", sort(keys %$packages)) . "\n";
130 } else {
131 PVE::APIClient::Commands::help->help({});
132 }
133
134 exit(0);