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