]> git.proxmox.com Git - pve-client.git/blob - pveclient
implement basic websocket protocol
[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::lxc;
17
18 use JSON;
19
20 sub print_usage {
21
22 my $text = "pveclient usage:\n\n";
23
24 $text .= "pveclient remote <help|add|remove> {options}\n\n";
25
26 $text .= "pveclient lxc <help|create|destroy|...> {options}\n\n";
27
28 $text .= "pveclient <get/set/create/delete> <path> {options}\n\n";
29
30 print STDERR $text;
31
32 }
33
34 sub call_method {
35 my ($path, $method, $args) = @_;
36
37 die "missing API path\n" if !defined($path);
38
39 my $info = PVE::APIClient::Helpers::lookup_api_method($path, $method);
40 my $param = PVE::JSONSchema::get_options($info->{parameters}, $args);
41 print Dumper($param);
42
43 die "implement me";
44 }
45
46 # NOTE: This binary is just a placeholer - nothing implemented so far!
47
48 #my $hostname = 'localhost';
49 #my $username = 'root@pam';
50 #
51 #my $conn = PVE::APIClient::LWP->new(
52 # username => $username,
53 # #password => 'yourpassword',
54 # #ticket => $ticket,
55 # #csrftoken => $csrftoken,
56 # host => $hostname,
57 # # allow manual fingerprint verification
58 # manual_verification => 1,
59 # );
60
61 #my $res = $conn->get("/", {});
62 #print to_json($res, { pretty => 1, canonical => 1});
63
64 my $cli_class_handlers = {
65 lxc => 'PVE::APIClient::Commands::lxc',
66 remote => 'PVE::APIClient::Commands::remote',
67 };
68
69 my $cmd = shift || (print_usage() && exit(-1));
70
71 if ($cmd eq 'get') {
72 my $method = 'GET';
73 my $path;
74 if (scalar(@ARGV) && $ARGV[0] !~ m/^\-/) {
75 $path = shift @ARGV;
76 }
77 my $res = call_method($path, $method, \@ARGV);
78 die "implement me";
79 } elsif ($cmd eq 'set') {
80 die "implement me";
81 } elsif ($cmd eq 'create') {
82 die "implement me";
83 } elsif ($cmd eq 'delete') {
84 die "implement me";
85 } elsif (my $class = $cli_class_handlers->{$cmd}) {
86 $class->run_cli_handler();
87 } elsif ($cmd eq 'bashcomplete') {
88
89 exit(0) if !(defined($ENV{COMP_LINE}) && defined($ENV{COMP_POINT}));
90
91 my $cmdlist = join('|', keys %$cli_class_handlers);
92 if ($ENV{COMP_LINE} =~ m/^(.*pveclient\s+($cmdlist)\s+)(.*)$/) {
93 my $cmd = $2;
94 my $class = $cli_class_handlers->{$cmd} || die "internal error";
95 $ENV{COMP_LINE} = "pveclient $3";
96 $ENV{COMP_POINT} = length($ENV{COMP_LINE});
97 @ARGV = ('bashcomplete', 'pveclient', $ARGV[1], $ARGV[2]);
98
99 $class->run_cli_handler();
100
101 } else {
102
103 my $cmdline = substr($ENV{COMP_LINE}, 0, $ENV{COMP_POINT});
104 my ($bash_command, $cur, $prev) = @ARGV;
105 $cmdline =~ s/$cur$//;
106
107 my $args = PVE::Tools::split_args($cmdline);
108
109 my @cmds = ('get', 'set', 'create', 'delete', keys %$cli_class_handlers);
110 if (scalar(@$args) == 1) {
111 foreach my $p (@cmds) {
112 print "$p\n" if $p =~ m/^$cur/;
113 }
114 }
115 }
116
117 } elsif ($cmd eq 'packagedepends') {
118 # experimental code to print required perl packages
119 my $packages = {};
120 my $dir = Cwd::getcwd;
121
122 foreach my $k (keys %INC) {
123 my $file = abs_path($INC{$k});
124 next if $file =~ m/^\Q$dir\E/;
125 my $res = `dpkg -S '$file'`;
126 if ($res && $res =~ m/^(\S+): $file$/) {
127 my $debian_package = $1;
128 $debian_package =~ s/:amd64$//;
129 $packages->{$debian_package} = 1;
130 } else {
131 die "unable to find package for '$file'\n";
132 }
133 }
134 print join("\n", sort(keys %$packages)) . "\n";
135 } else {
136 print_usage();
137 }
138
139 exit(0);