]> git.proxmox.com Git - pve-client.git/blob - pveclient
implement bash completion for get/set/create/delete
[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 $ENV{COMP_LINE} = "pveclient $3";
73 $ENV{COMP_POINT} = length($ENV{COMP_LINE});
74 @ARGV = ('bashcomplete', 'pveclient', $ARGV[1], $ARGV[2]);
75
76 $class->run_cli_handler();
77
78 } else {
79
80 my $cmdline = substr($ENV{COMP_LINE}, 0, $ENV{COMP_POINT});
81 my ($bash_command, $cur, $prev) = @ARGV;
82 $cmdline =~ s/\Q$cur\E$//;
83
84 my $args = PVE::Tools::split_args($cmdline);
85
86 my @cmds = (keys %$method_map, keys %$cli_class_handlers);
87 if (scalar(@$args) == 1) {
88 foreach my $p (@cmds) {
89 print "$p\n" if $p =~ m/^$cur/;
90 }
91 } elsif (scalar(@$args) == 2) {
92 if (my $method = $method_map->{$args->[1]}) {
93 PVE::APIClient::Helpers::complete_api_path($cur);
94 }
95 } elsif (scalar(@$args) >= 3) {
96 my $path = $args->[2];
97 if (my $method = $method_map->{$args->[1]}) {
98 if (my $info = PVE::APIClient::Helpers::lookup_api_method($path, $method, 1)) {
99 my $prop = $info->{parameters}->{properties};
100 PVE::APIClient::Helpers::complete_api_call_options($method, $prop, $prev, $cur, $args);
101 }
102 }
103 }
104 }
105
106 } elsif ($cmd eq 'packagedepends') {
107 # experimental code to print required perl packages
108 my $packages = {};
109 my $dir = Cwd::getcwd;
110
111 foreach my $k (keys %INC) {
112 my $file = abs_path($INC{$k});
113 next if $file =~ m/^\Q$dir\E/;
114 my $res = `dpkg -S '$file'`;
115 if ($res && $res =~ m/^(\S+): $file$/) {
116 my $debian_package = $1;
117 $debian_package =~ s/:amd64$//;
118 $packages->{$debian_package} = 1;
119 } else {
120 die "unable to find package for '$file'\n";
121 }
122 }
123 print join("\n", sort(keys %$packages)) . "\n";
124 } else {
125 PVE::APIClient::Commands::help->help({});
126 }
127
128 exit(0);