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