X-Git-Url: https://git.proxmox.com/?p=pve-client.git;a=blobdiff_plain;f=pveclient;h=f18dab902c231510aa8de7d2f76efa36007b9eef;hp=8ea109d042ed7683a59681f85babad86f34a2e7d;hb=41d185e8ec50461eedff102b86654ac50c70c51e;hpb=565bbc73ca66a8a0612df1c04e8f5b4a07f02ae1 diff --git a/pveclient b/pveclient index 8ea109d..f18dab9 100755 --- a/pveclient +++ b/pveclient @@ -2,73 +2,135 @@ use strict; use warnings; +use Cwd 'abs_path'; use lib '/usr/share/pve-client'; +use lib '.'; use Data::Dumper; +use PVE::JSONSchema; use PVE::CLIHandler; use PVE::APIClient::LWP; use PVE::APIClient::Helpers; +use PVE::APIClient::Commands::config; use PVE::APIClient::Commands::remote; +use PVE::APIClient::Commands::list; +use PVE::APIClient::Commands::lxc; +use PVE::APIClient::Commands::help; use JSON; -sub print_usage { - - die "Usage: implement me"; - exit(-1); -} - sub call_method { my ($path, $method, $args) = @_; die "missing API path\n" if !defined($path); my $info = PVE::APIClient::Helpers::lookup_api_method($path, $method); - my $param = PVE::APIClient::Helpers::get_options($info->{parameters}, $args); + my $param = PVE::JSONSchema::get_options($info->{parameters}, $args); print Dumper($param); die "implement me"; } -# NOTE: This binary is just a placeholer - nothing implemented so far! -#my $hostname = 'localhost'; -#my $username = 'root@pam'; -# -#my $conn = PVE::APIClient::LWP->new( -# username => $username, -# #password => 'yourpassword', -# #ticket => $ticket, -# #csrftoken => $csrftoken, -# host => $hostname, -# # allow manual fingerprint verification -# manual_verification => 1, -# ); +my $cli_class_handlers = { + list => 'PVE::APIClient::Commands::list', + lxc => 'PVE::APIClient::Commands::lxc', + remote => 'PVE::APIClient::Commands::remote', + config => 'PVE::APIClient::Commands::config', + help => 'PVE::APIClient::Commands::help', +}; -#my $res = $conn->get("/", {}); -#print to_json($res, { pretty => 1, canonical => 1}); +my $cmd = shift; +if (!defined($cmd)) { + PVE::APIClient::Commands::help->help({}); + exit(-1); +} -my $cmd = shift || print_usage(); +my $method_map = { + create => 'POST', + set => 'PUT', + get => 'GET', + delete => 'DELETE', +}; -if ($cmd eq 'get') { - my $method = 'GET'; +if (my $method = $method_map->{$cmd}) { my $path; if (scalar(@ARGV) && $ARGV[0] !~ m/^\-/) { $path = shift @ARGV; } my $res = call_method($path, $method, \@ARGV); die "implement me"; -} elsif ($cmd eq 'set') { - die "implement me"; -} elsif ($cmd eq 'create') { - die "implement me"; -} elsif ($cmd eq 'delete') { - die "implement me"; -} elsif ($cmd eq 'remote') { - PVE::APIClient::Commands::remote->run_cli_handler(); +} elsif (my $class = $cli_class_handlers->{$cmd}) { + $class->run_cli_handler(); +} elsif ($cmd eq 'bashcomplete') { + + exit(0) if !(defined($ENV{COMP_LINE}) && defined($ENV{COMP_POINT})); + + my $cmdlist = join('|', keys %$cli_class_handlers); + if ($ENV{COMP_LINE} =~ m/^(.*pveclient\s+($cmdlist)\s+)(.*)$/) { + my $cmd = $2; + my $class = $cli_class_handlers->{$cmd} || die "internal error"; + + if ($cmd eq 'list') { # simple commands + $ENV{COMP_LINE} = "pveclient $3"; + $ENV{COMP_POINT} = length($ENV{COMP_LINE}); + @ARGV = ('bashcomplete', 'pveclient', $ARGV[1], $ARGV[2]); + } else { + $ENV{COMP_LINE} = "pveclient $3"; + $ENV{COMP_POINT} = length($ENV{COMP_LINE}); + @ARGV = ('bashcomplete', 'pveclient', $ARGV[1], $ARGV[2]); + } + $class->run_cli_handler(); + + } else { + + my $cmdline = substr($ENV{COMP_LINE}, 0, $ENV{COMP_POINT}); + my ($bash_command, $cur, $prev) = @ARGV; + $cmdline =~ s/\Q$cur\E$//; + + my $args = PVE::Tools::split_args($cmdline); + + my @cmds = (keys %$method_map, keys %$cli_class_handlers); + if (scalar(@$args) == 1) { + foreach my $p (@cmds) { + print "$p\n" if $p =~ m/^$cur/; + } + } elsif (scalar(@$args) == 2) { + if (my $method = $method_map->{$args->[1]}) { + PVE::APIClient::Helpers::complete_api_path($cur); + } + } elsif (scalar(@$args) >= 3) { + my $path = $args->[2]; + if (my $method = $method_map->{$args->[1]}) { + if (my $info = PVE::APIClient::Helpers::lookup_api_method($path, $method, 1)) { + my $prop = $info->{parameters}->{properties}; + PVE::APIClient::Helpers::complete_api_call_options($method, $prop, $prev, $cur, $args); + } + } + } + } + +} elsif ($cmd eq 'packagedepends') { + # experimental code to print required perl packages + my $packages = {}; + my $dir = Cwd::getcwd; + + foreach my $k (keys %INC) { + my $file = abs_path($INC{$k}); + next if $file =~ m/^\Q$dir\E/; + my $res = `dpkg -S '$file'`; + if ($res && $res =~ m/^(\S+): $file$/) { + my $debian_package = $1; + $debian_package =~ s/:amd64$//; + $packages->{$debian_package} = 1; + } else { + die "unable to find package for '$file'\n"; + } + } + print join("\n", sort(keys %$packages)) . "\n"; } else { - print_usage(); + PVE::APIClient::Commands::help->help({}); } exit(0);