]> git.proxmox.com Git - pve-client.git/blob - pveclient
091af96bb160a2ee7ab79a3e029794984770a9d0
[pve-client.git] / pveclient
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use lib '/usr/share/pve-client';
6 use lib '.';
7 use Data::Dumper;
8
9 use PVE::JSONSchema;
10 use PVE::CLIHandler;
11
12 use PVE::APIClient::LWP;
13 use PVE::APIClient::Helpers;
14 use PVE::APIClient::Commands::remote;
15 use PVE::APIClient::Commands::lxc;
16
17 use JSON;
18
19 sub print_usage {
20
21 my $text = "pveclient usage:\n\n";
22
23 $text .= "pveclient remote <help|add|remove> {options}\n\n";
24
25 $text .= "pveclient lxc <help|create|destroy|...> {options}\n\n";
26
27 $text .= "pveclient <get/set/create/delete> <path> {options}\n\n";
28
29 print STDERR $text;
30
31 }
32
33 sub 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);
39 my $param = PVE::JSONSchema::get_options($info->{parameters}, $args);
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
63 my $cli_class_handlers = {
64 lxc => 'PVE::APIClient::Commands::lxc',
65 remote => 'PVE::APIClient::Commands::remote',
66 };
67
68 my $cmd = shift || (print_usage() && exit(-1));
69
70 if ($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";
84 } elsif (my $class = $cli_class_handlers->{$cmd}) {
85 $class->run_cli_handler();
86 } elsif ($cmd eq 'bashcomplete') {
87
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 {
101
102 my $cmdline = substr($ENV{COMP_LINE}, 0, $ENV{COMP_POINT});
103 my ($bash_command, $cur, $prev) = @ARGV;
104 $cmdline =~ s/$cur$//;
105
106 my $args = PVE::Tools::split_args($cmdline);
107
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 }
113 }
114 }
115
116 } else {
117 print_usage();
118 }
119
120 exit(0);