improve bash completion
[pve-client.git] / pveclient
CommitLineData
29505e2c
DM
1#!/usr/bin/perl
2
3use strict;
4use warnings;
565bbc73 5use lib '/usr/share/pve-client';
c5111270 6use lib '.';
29505e2c
DM
7use Data::Dumper;
8
a04fd959 9use PVE::JSONSchema;
565bbc73
DM
10use PVE::CLIHandler;
11
29505e2c
DM
12use PVE::APIClient::LWP;
13use PVE::APIClient::Helpers;
565bbc73 14use PVE::APIClient::Commands::remote;
adf285bf 15use PVE::APIClient::Commands::lxc;
565bbc73 16
29505e2c
DM
17use JSON;
18
19sub print_usage {
20
c5111270
DM
21 my $text = "pveclient usage:\n\n";
22
23 $text .= "pveclient remote <help|add|remove> {options}\n\n";
24
adf285bf
DM
25 $text .= "pveclient lxc <help|create|destroy|...> {options}\n\n";
26
c5111270
DM
27 $text .= "pveclient <get/set/create/delete> <path> {options}\n\n";
28
29 print STDERR $text;
30
29505e2c
DM
31}
32
33sub call_method {
34 my ($path, $method, $args) = @_;
35
36 die "missing API path\n" if !defined($path);
37
38 my $info = PVE::APIClient::Helpers::lookup_api_method($path, $method);
a04fd959 39 my $param = PVE::JSONSchema::get_options($info->{parameters}, $args);
29505e2c
DM
40 print Dumper($param);
41
42 die "implement me";
43}
44
45# NOTE: This binary is just a placeholer - nothing implemented so far!
46
47#my $hostname = 'localhost';
48#my $username = 'root@pam';
49#
50#my $conn = PVE::APIClient::LWP->new(
51# username => $username,
52# #password => 'yourpassword',
53# #ticket => $ticket,
54# #csrftoken => $csrftoken,
55# host => $hostname,
56# # allow manual fingerprint verification
57# manual_verification => 1,
58# );
59
60#my $res = $conn->get("/", {});
61#print to_json($res, { pretty => 1, canonical => 1});
62
a8aa5b63
DM
63my $cli_class_handlers = {
64 lxc => 'PVE::APIClient::Commands::lxc',
65 remote => 'PVE::APIClient::Commands::remote',
66};
67
c5111270 68my $cmd = shift || (print_usage() && exit(-1));
29505e2c
DM
69
70if ($cmd eq 'get') {
71 my $method = 'GET';
72 my $path;
73 if (scalar(@ARGV) && $ARGV[0] !~ m/^\-/) {
74 $path = shift @ARGV;
75 }
76 my $res = call_method($path, $method, \@ARGV);
77 die "implement me";
78} elsif ($cmd eq 'set') {
79 die "implement me";
80} elsif ($cmd eq 'create') {
81 die "implement me";
82} elsif ($cmd eq 'delete') {
83 die "implement me";
a8aa5b63
DM
84} elsif (my $class = $cli_class_handlers->{$cmd}) {
85 $class->run_cli_handler();
dce6ecbc
DM
86} elsif ($cmd eq 'bashcomplete') {
87
a8aa5b63
DM
88 exit(0) if !(defined($ENV{COMP_LINE}) && defined($ENV{COMP_POINT}));
89
90 my $cmdlist = join('|', keys %$cli_class_handlers);
91 if ($ENV{COMP_LINE} =~ m/^(.*pveclient\s+($cmdlist)\s+)(.*)$/) {
92 my $cmd = $2;
93 my $class = $cli_class_handlers->{$cmd} || die "internal error";
94 $ENV{COMP_LINE} = "pveclient $3";
95 $ENV{COMP_POINT} = length($ENV{COMP_LINE});
96 @ARGV = ('bashcomplete', 'pveclient', $ARGV[1], $ARGV[2]);
97
98 $class->run_cli_handler();
99
100 } else {
dce6ecbc 101
a8aa5b63
DM
102 my $cmdline = substr($ENV{COMP_LINE}, 0, $ENV{COMP_POINT});
103 my ($bash_command, $cur, $prev) = @ARGV;
104 $cmdline =~ s/$cur$//;
dce6ecbc 105
a8aa5b63 106 my $args = PVE::Tools::split_args($cmdline);
dce6ecbc 107
a8aa5b63
DM
108 my @cmds = ('get', 'set', 'create', 'delete', keys %$cli_class_handlers);
109 if (scalar(@$args) == 1) {
110 foreach my $p (@cmds) {
111 print "$p\n" if $p =~ m/^$cur/;
112 }
dce6ecbc
DM
113 }
114 }
115
29505e2c
DM
116} else {
117 print_usage();
118}
119
565bbc73 120exit(0);