]> git.proxmox.com Git - pve-client.git/blame - pveclient
implement bash completion for get/set/create/delete
[pve-client.git] / pveclient
CommitLineData
29505e2c
DM
1#!/usr/bin/perl
2
3use strict;
4use warnings;
f5fcd826 5use Cwd 'abs_path';
565bbc73 6use lib '/usr/share/pve-client';
c5111270 7use lib '.';
29505e2c
DM
8use Data::Dumper;
9
a04fd959 10use PVE::JSONSchema;
565bbc73
DM
11use PVE::CLIHandler;
12
29505e2c
DM
13use PVE::APIClient::LWP;
14use PVE::APIClient::Helpers;
565bbc73 15use PVE::APIClient::Commands::remote;
5b090843 16use PVE::APIClient::Commands::list;
adf285bf 17use PVE::APIClient::Commands::lxc;
63c02d8c 18use PVE::APIClient::Commands::help;
565bbc73 19
29505e2c
DM
20use JSON;
21
29505e2c
DM
22sub 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);
a04fd959 28 my $param = PVE::JSONSchema::get_options($info->{parameters}, $args);
29505e2c
DM
29 print Dumper($param);
30
31 die "implement me";
32}
33
3a1bc22f 34
a8aa5b63 35my $cli_class_handlers = {
5b090843 36 list => 'PVE::APIClient::Commands::list',
a8aa5b63
DM
37 lxc => 'PVE::APIClient::Commands::lxc',
38 remote => 'PVE::APIClient::Commands::remote',
63c02d8c 39 help => 'PVE::APIClient::Commands::help',
a8aa5b63
DM
40};
41
63c02d8c
DM
42my $cmd = shift;
43if (!defined($cmd)) {
44 PVE::APIClient::Commands::help->help({});
45 exit(-1);
46}
29505e2c 47
3a1bc22f
DM
48my $method_map = {
49 create => 'POST',
50 set => 'PUT',
51 get => 'GET',
52 delete => 'DELETE',
53};
54
55if (my $method = $method_map->{$cmd}) {
29505e2c
DM
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";
a8aa5b63
DM
62} elsif (my $class = $cli_class_handlers->{$cmd}) {
63 $class->run_cli_handler();
dce6ecbc
DM
64} elsif ($cmd eq 'bashcomplete') {
65
a8aa5b63
DM
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 {
dce6ecbc 79
a8aa5b63
DM
80 my $cmdline = substr($ENV{COMP_LINE}, 0, $ENV{COMP_POINT});
81 my ($bash_command, $cur, $prev) = @ARGV;
3a1bc22f 82 $cmdline =~ s/\Q$cur\E$//;
dce6ecbc 83
a8aa5b63 84 my $args = PVE::Tools::split_args($cmdline);
dce6ecbc 85
3a1bc22f 86 my @cmds = (keys %$method_map, keys %$cli_class_handlers);
a8aa5b63
DM
87 if (scalar(@$args) == 1) {
88 foreach my $p (@cmds) {
89 print "$p\n" if $p =~ m/^$cur/;
90 }
3a1bc22f
DM
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 }
dce6ecbc
DM
103 }
104 }
105
f5fcd826
DM
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";
29505e2c 124} else {
63c02d8c 125 PVE::APIClient::Commands::help->help({});
29505e2c
DM
126}
127
565bbc73 128exit(0);