[pve-devel] r5059 - in pve-manager/pve2: . bin
svn-commits at proxmox.com
svn-commits at proxmox.com
Thu Aug 26 10:14:55 CEST 2010
Author: dietmar
Date: 2010-08-26 08:14:55 +0000 (Thu, 26 Aug 2010)
New Revision: 5059
Modified:
pve-manager/pve2/ChangeLog
pve-manager/pve2/bin/pvesh
Log:
* bin/pvesh: use new cli_handler2(), implement help, cleanups
Modified: pve-manager/pve2/ChangeLog
===================================================================
--- pve-manager/pve2/ChangeLog 2010-08-26 08:12:57 UTC (rev 5058)
+++ pve-manager/pve2/ChangeLog 2010-08-26 08:14:55 UTC (rev 5059)
@@ -1,3 +1,7 @@
+2010-08-26 Proxmox Support Team <support at proxmox.com>
+
+ * bin/pvesh: use new cli_handler2(), implement help, cleanups
+
2010-08-24 Proxmox Support Team <support at proxmox.com>
* lib/PVE/APIDaemon.pm: use new RPCEnvironment
Modified: pve-manager/pve2/bin/pvesh
===================================================================
--- pve-manager/pve2/bin/pvesh 2010-08-26 08:12:57 UTC (rev 5058)
+++ pve-manager/pve2/bin/pvesh 2010-08-26 08:14:55 UTC (rev 5059)
@@ -62,23 +62,6 @@
return $ret;
}
-sub call_handler {
- my ($handler, $info, $dir, $params, $nooutput) = @_;
-
- my $data = $handler->handle($info, $params);
-
- return $data if $nooutput;
-
- warn "200 OK\n"; # always print OK status if successful
-
- return if $info && $info->{returns} &&
- $info->{returns}->{type} && $info->{returns}->{type} eq 'null';
-
- print to_json($data, {allow_nonref => 1, canonical => 1, pretty => 1 });
-
- return $data;
-}
-
my $read_password = sub {
my $attribs = $term->Attribs;
my $old = $attribs->{redisplay_function};
@@ -90,82 +73,65 @@
return $input;
};
-sub create_entry {
- my ($dir, $args) = @_;
+sub map_cmd {
+ my $cmd = shift;
- my $uri_param = {};
- my ($handler, $info) = PVE::API2->find_handler('POST', $dir, $uri_param);
- if (!$handler || !$info) {
- die "no 'create' handler for '$dir'\n";
- }
+ my $mmap = {
+ create => 'POST',
+ set => 'PUT',
+ get => 'GET',
+ delete => 'DELETE',
+ };
- my $opts = PVE::JSONSchema::get_options($info->{parameters}, $args, $uri_param, $read_password);
+ my $method = $mmap->{$cmd};
- # print "CREATE $dir " . Dumper($opts) . "\n";
+ die "unable to map method" if !$method;
- call_handler($handler, $info, $dir, $opts);
+ return $method;
}
-sub get_entry {
- my ($dir, $args) = @_;
+sub call_method {
+ my ($dir, $cmd, $args) = @_;
+ my $method = map_cmd($cmd);
+
my $uri_param = {};
- my ($handler, $info) = PVE::API2->find_handler('GET', $dir, $uri_param);
+ my ($handler, $info) = PVE::API2->find_handler($method, $dir, $uri_param);
if (!$handler || !$info) {
- die "no 'get' handler for '$dir'\n";
+ die "no '$cmd' handler for '$dir'\n";
}
- my $opts = PVE::JSONSchema::get_options($info->{parameters}, $args, $uri_param, $read_password);
+ my $data = $handler->cli_handler2("$cmd $dir", $info->{name}, $args, [], $uri_param, $read_password);
- # print "GET $dir " . Dumper($opts) . "\n";
+ warn "200 OK\n"; # always print OK status if successful
- call_handler($handler, $info, $dir, $opts);
-}
+ return if $info && $info->{returns} &&
+ $info->{returns}->{type} && $info->{returns}->{type} eq 'null';
-sub update_entry {
- my ($dir, $args) = @_;
+ print to_json($data, {allow_nonref => 1, canonical => 1, pretty => 1 });
- my $uri_param = {};
- my ($handler, $info) = PVE::API2->find_handler('PUT', $dir, $uri_param);
- if (!$handler || !$info) {
- die "no 'update' handler for '$dir'\n";
- }
-
- my $opts = PVE::JSONSchema::get_options($info->{parameters}, $args, $uri_param, $read_password);
-
- # print "PUT $dir " . Dumper($opts) . "\n";
-
- call_handler($handler, $info, $dir, $opts);
+ return $data;
}
-sub delete_entry {
- my ($dir) = @_;
+sub print_help {
+ my ($cmd, $dir) = @_;
+ my $method = map_cmd($cmd);
+
my $uri_param = {};
- my ($handler, $info) = PVE::API2->find_handler('DELETE', $dir, $uri_param);
+ my ($handler, $info) = PVE::API2->find_handler($method, $dir, $uri_param);
if (!$handler || !$info) {
- die "no 'delete' handler for '$dir'\n";
+ die "no '$cmd' handler for '$dir'\n";
}
-
- # print "DELETE $dir\n";
- call_handler($handler, $info, $dir, $uri_param);
+ print $handler->usage_str($info->{name}, "$cmd $dir", [], $uri_param, 'full', 1);
}
-sub test_dir {
- my ($dir, $uri_param) = @_;
-
- my ($handler, $info) = PVE::API2->find_handler('GET', $dir, $uri_param);
- return undef if !$handler || !$info;
-
- return wantarray ? ($handler, $info) : 1;
-}
-
sub list_dir {
my ($dir, $args) = @_;
my $uri_param = {};
- my ($handler, $info) = test_dir($dir, $uri_param);
+ my ($handler, $info) = PVE::API2->find_handler('GET', $dir, $uri_param);
if (!$handler || !$info) {
die "no such resource\n";
}
@@ -174,9 +140,7 @@
die "resource does not define child links\n";
}
- my $params = PVE::JSONSchema::get_options($info->{parameters}, $args, $uri_param, $read_password);
-
- my $data = call_handler($handler, $info, $dir, $params, 1);
+ my $data = $handler->cli_handler2("ls $dir", $info->{name}, $args, [], $uri_param, $read_password);
my $lnk = PVE::JSONSchema::method_get_child_link($info);
if ($lnk && $data) {
@@ -223,14 +187,24 @@
return;
} else {
my $new_dir = abs_path($cdir, $path);
- die "no such resource\n" if !test_dir($new_dir);
+ my ($handler, $info) = PVE::API2->find_handler('GET', $new_dir);
+ die "no such resource\n" if !$handler;
$cdir = $new_dir;
}
} elsif ($cmd eq 'help') {
- die "implement me!";
+ my $cmd = shift @$args;
+ my $path;
+ if (scalar(@$args) && $args->[0] !~ m/^\-/) {
+ $path = shift @$args;
+ }
+
+ die "usage: help <get|set|create|delete|ls> [path]\n" if !$cmd || scalar(@$args);
+
+ print_help($cmd, abs_path($cdir, $path));
+
} elsif ($cmd eq 'ls') {
my $path;
if (scalar(@$args) && $args->[0] !~ m/^\-/) {
@@ -246,7 +220,7 @@
$path = shift @$args;
}
- get_entry(abs_path($cdir, $path), $args);
+ call_method(abs_path($cdir, $path), $cmd, $args);
} elsif ($cmd eq 'create') {
@@ -255,7 +229,7 @@
$path = shift @$args;
}
- create_entry(abs_path($cdir, $path), $args);
+ call_method(abs_path($cdir, $path), $cmd, $args);
} elsif ($cmd eq 'delete') {
@@ -263,7 +237,7 @@
die "usage: delete [path]\n" if scalar(@$args);
- delete_entry(abs_path($cdir, $path));
+ call_method(abs_path($cdir, $cmd, $path));
} elsif ($cmd eq 'set') {
@@ -272,7 +246,7 @@
$path = shift @$args;
}
- update_entry(abs_path($cdir, $path), $args);
+ call_method(abs_path($cdir, $path), $cmd, $args);
} else {
die "unknown command '$cmd'\n";
More information about the pve-devel
mailing list