use SectionConfig for PVE::APIClient::Config
[pve-client.git] / pveclient
CommitLineData
29505e2c
DM
1#!/usr/bin/perl
2
3use strict;
4use warnings;
f5fcd826 5use Cwd 'abs_path';
565bbc73 6use lib '/usr/share/pve-client';
c5111270 7use lib '.';
29505e2c
DM
8use Data::Dumper;
9
a04fd959 10use PVE::JSONSchema;
565bbc73
DM
11use PVE::CLIHandler;
12
29505e2c
DM
13use PVE::APIClient::LWP;
14use PVE::APIClient::Helpers;
565bbc73 15use PVE::APIClient::Commands::remote;
adf285bf 16use PVE::APIClient::Commands::lxc;
63c02d8c 17use PVE::APIClient::Commands::help;
565bbc73 18
29505e2c
DM
19use JSON;
20
29505e2c
DM
21sub 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);
a04fd959 27 my $param = PVE::JSONSchema::get_options($info->{parameters}, $args);
29505e2c
DM
28 print Dumper($param);
29
30 die "implement me";
31}
32
a8aa5b63
DM
33my $cli_class_handlers = {
34 lxc => 'PVE::APIClient::Commands::lxc',
35 remote => 'PVE::APIClient::Commands::remote',
63c02d8c 36 help => 'PVE::APIClient::Commands::help',
a8aa5b63
DM
37};
38
63c02d8c
DM
39my $cmd = shift;
40if (!defined($cmd)) {
41 PVE::APIClient::Commands::help->help({});
42 exit(-1);
43}
29505e2c
DM
44
45if ($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";
a8aa5b63
DM
59} elsif (my $class = $cli_class_handlers->{$cmd}) {
60 $class->run_cli_handler();
dce6ecbc
DM
61} elsif ($cmd eq 'bashcomplete') {
62
a8aa5b63
DM
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 {
dce6ecbc 76
a8aa5b63
DM
77 my $cmdline = substr($ENV{COMP_LINE}, 0, $ENV{COMP_POINT});
78 my ($bash_command, $cur, $prev) = @ARGV;
79 $cmdline =~ s/$cur$//;
dce6ecbc 80
a8aa5b63 81 my $args = PVE::Tools::split_args($cmdline);
dce6ecbc 82
a8aa5b63
DM
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 }
dce6ecbc
DM
88 }
89 }
90
f5fcd826
DM
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";
29505e2c 109} else {
63c02d8c 110 PVE::APIClient::Commands::help->help({});
29505e2c
DM
111}
112
565bbc73 113exit(0);