use SectionConfig for PVE::APIClient::Config
[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 my $cli_class_handlers = {
34 lxc => 'PVE::APIClient::Commands::lxc',
35 remote => 'PVE::APIClient::Commands::remote',
36 help => 'PVE::APIClient::Commands::help',
37 };
38
39 my $cmd = shift;
40 if (!defined($cmd)) {
41 PVE::APIClient::Commands::help->help({});
42 exit(-1);
43 }
44
45 if ($cmd eq 'get') {
46 my $method = 'GET';
47 my $path;
48 if (scalar(@ARGV) && $ARGV[0] !~ m/^\-/) {
49 $path = shift @ARGV;
50 }
51 my $res = call_method($path, $method, \@ARGV);
52 die "implement me";
53 } elsif ($cmd eq 'set') {
54 die "implement me";
55 } elsif ($cmd eq 'create') {
56 die "implement me";
57 } elsif ($cmd eq 'delete') {
58 die "implement me";
59 } elsif (my $class = $cli_class_handlers->{$cmd}) {
60 $class->run_cli_handler();
61 } elsif ($cmd eq 'bashcomplete') {
62
63 exit(0) if !(defined($ENV{COMP_LINE}) && defined($ENV{COMP_POINT}));
64
65 my $cmdlist = join('|', keys %$cli_class_handlers);
66 if ($ENV{COMP_LINE} =~ m/^(.*pveclient\s+($cmdlist)\s+)(.*)$/) {
67 my $cmd = $2;
68 my $class = $cli_class_handlers->{$cmd} || die "internal error";
69 $ENV{COMP_LINE} = "pveclient $3";
70 $ENV{COMP_POINT} = length($ENV{COMP_LINE});
71 @ARGV = ('bashcomplete', 'pveclient', $ARGV[1], $ARGV[2]);
72
73 $class->run_cli_handler();
74
75 } else {
76
77 my $cmdline = substr($ENV{COMP_LINE}, 0, $ENV{COMP_POINT});
78 my ($bash_command, $cur, $prev) = @ARGV;
79 $cmdline =~ s/$cur$//;
80
81 my $args = PVE::Tools::split_args($cmdline);
82
83 my @cmds = ('get', 'set', 'create', 'delete', keys %$cli_class_handlers);
84 if (scalar(@$args) == 1) {
85 foreach my $p (@cmds) {
86 print "$p\n" if $p =~ m/^$cur/;
87 }
88 }
89 }
90
91 } elsif ($cmd eq 'packagedepends') {
92 # experimental code to print required perl packages
93 my $packages = {};
94 my $dir = Cwd::getcwd;
95
96 foreach my $k (keys %INC) {
97 my $file = abs_path($INC{$k});
98 next if $file =~ m/^\Q$dir\E/;
99 my $res = `dpkg -S '$file'`;
100 if ($res && $res =~ m/^(\S+): $file$/) {
101 my $debian_package = $1;
102 $debian_package =~ s/:amd64$//;
103 $packages->{$debian_package} = 1;
104 } else {
105 die "unable to find package for '$file'\n";
106 }
107 }
108 print join("\n", sort(keys %$packages)) . "\n";
109 } else {
110 PVE::APIClient::Commands::help->help({});
111 }
112
113 exit(0);