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