]> git.proxmox.com Git - pve-client.git/commitdiff
PVE::APIClient::Helpers::extract_even_elements - new helper
authorDietmar Maurer <dietmar@proxmox.com>
Tue, 26 Jun 2018 11:46:50 +0000 (13:46 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Mon, 2 Jul 2018 08:05:45 +0000 (10:05 +0200)
PVE/APIClient/Commands/list.pm
PVE/APIClient/Commands/remote.pm
PVE/APIClient/Config.pm
PVE/APIClient/Helpers.pm
pveclient

index 2030c515423d0fecbd393a1395e37da62562785c..f5388f1214001282a9b477f8ecb4ccec45435aa9 100644 (file)
@@ -6,11 +6,21 @@ use JSON;
 
 use PVE::APIClient::JSONSchema qw(get_standard_option);
 
+use PVE::APIClient::Helpers;
 use PVE::APIClient::Config;
 use PVE::APIClient::CLIHandler;
 
 use base qw(PVE::APIClient::CLIHandler);
 
+# define as array to keep ordering
+my $list_returns_properties = [
+    'vmid' => get_standard_option('pve-vmid'),
+    'node' => get_standard_option('pve-node'),
+    'type' => { type => 'string' },
+    'status' =>  { type => 'string' },
+    'name' => { type => 'string', optional => 1 },
+    ];
+
 __PACKAGE__->register_method ({
     name => 'list',
     path => 'list',
@@ -20,36 +30,31 @@ __PACKAGE__->register_method ({
        additionalProperties => 0,
        properties => {
            remote => get_standard_option('pveclient-remote-name'),
-           format => get_standard_option('pveclient-output-format'),
+           'format' => get_standard_option('pveclient-output-format'),
+       },
+    },
+    returns => {
+       type => 'array',
+       items => {
+           type => 'object',
+           properties => { @$list_returns_properties },
        },
     },
-    returns => { type => 'null'},
     code => sub {
        my ($param) = @_;
 
+       my $format = PVE::APIClient::Tools::extract_param($param, 'format');
+       PVE::APIClient::Helpers::set_output_format($format);
+
        my $config = PVE::APIClient::Config->load();
        my $conn = PVE::APIClient::Config->remote_conn($config, $param->{remote});
-       my $resources = $conn->get('api2/json/cluster/resources', { type => 'vm' });
-
-       if (!defined($param->{format}) or $param->{format} eq 'text') {
-           my $headers = ['Node', 'VMID', 'Type', 'Name', 'Status'];
-           my $data = [];
-           for my $el (@$resources) {
-               push(@$data, [$el->{node}, $el->{vmid}, $el->{type}, $el->{name}, $el->{status}]);
-           }
-
-           printf("%10s %10s %10s %10s %10s\n", @$headers);
-           for my $row (@$data) {
-               printf("%10s %10s %10s %10s %10s\n", @$row);
-           }
-       } else {
-           print JSON::to_json($resources, {utf8 => 1, pretty => 1});
-       }
-
-       return undef;
+
+       return $conn->get('api2/json/cluster/resources', { type => 'vm' });
     }});
 
 
-our $cmddef = [ __PACKAGE__, 'list', ['remote']];
+our $cmddef = [ __PACKAGE__, 'list', ['remote'], {}, sub {
+    PVE::APIClient::Helpers::print_ordered_result($list_returns_properties, @_);
+}];
 
 1;
index b23390150f50588bb748fe9224af61fd91ba0bf0..6124bbe6489f867b959dd213b3a6aad3a9f8690f 100644 (file)
@@ -19,6 +19,15 @@ sub read_password {
    return PVE::APIClient::PTY::read_password("Remote password: ")
 }
 
+# define as array to keep ordering
+my $remote_list_returns_properties = [
+    name => get_standard_option('pveclient-remote-name'),
+    host => { type => 'string', format => 'address' },
+    username => { type => 'string' },
+    port => { type => 'integer', optional => 1 },
+    fingerprint =>  { type => 'string', optional => 1 },
+    ];
+
 __PACKAGE__->register_method ({
     name => 'remote_list',
     path => 'remote_list',
@@ -26,20 +35,33 @@ __PACKAGE__->register_method ({
     description => "List remotes from your config file.",
     parameters => {
        additionalProperties => 0,
+       properties => {
+           'format' => get_standard_option('pveclient-output-format'),
+       },
+    },
+    returns => {
+       type => 'array',
+       items => {
+           type => 'object',
+           properties => { @$remote_list_returns_properties },
+       },
     },
-    returns => { type => 'null' },
     code => sub {
+       my ($param) = @_;
+
+       my $format = PVE::APIClient::Tools::extract_param($param, 'format');
+       PVE::APIClient::Helpers::set_output_format($format);
+
        my $config = PVE::APIClient::Config->load();
 
-       printf("%10s %10s %10s %10s %100s\n", "Name", "Host", "Port", "Username", "Fingerprint");
+       my $res = [];
        for my $name (keys %{$config->{ids}}) {
            my $data = $config->{ids}->{$name};
            next if $data->{type} ne 'remote';
-           printf("%10s %10s %10s %10s %100s\n", $name, $data->{'host'},
-                  $data->{'port'} // '-', $data->{'username'}, $data->{'fingerprint'} // '-');
+           push @$res, $data;
        }
 
-       return undef;
+       return $res;
     }});
 
 __PACKAGE__->register_method ({
@@ -189,7 +211,9 @@ our $cmddef = {
     add => [ __PACKAGE__, 'remote_add', ['name', 'host', 'username']],
     set => [ __PACKAGE__, 'remote_set', ['name']],
     delete => [ __PACKAGE__, 'remote_delete', ['name']],
-    list => [__PACKAGE__, 'remote_list'],
+    list => [__PACKAGE__, 'remote_list', undef, {}, sub {
+       PVE::APIClient::Helpers::print_ordered_result($remote_list_returns_properties, @_);
+    }],
 };
 
 1;
index a783ab3870a4f01bdd9fe85988c40c50e3427173..9ab68fc2c2dc835ac694018fe61961293d5dbd42 100644 (file)
@@ -31,7 +31,7 @@ my $complete_remote_name = sub {
 PVE::APIClient::JSONSchema::register_standard_option('pveclient-output-format', {
     type => 'string',
     description => 'Output format.',
-    enum => [ 'text', 'json' ],
+    enum => [ 'text', 'plain', 'json' ],
     optional => 1,
     default => 'text',
 });
index ed1822d4a514dea06b3807c6c9feab7097d7fd50..3e779b6924b976e3132c3019607117b257e59157 100644 (file)
@@ -9,6 +9,9 @@ use File::Path qw(make_path);
 
 use PVE::APIClient::JSONSchema;
 use PVE::APIClient::Exception qw(raise);
+use PVE::APIClient::CLIFormatter;
+use PVE::APIClient::CLIHandler;
+use PVE::APIClient::PTY;
 use Encode::Locale;
 use Encode;
 use HTTP::Status qw(:constants);
@@ -395,5 +398,24 @@ sub ticket_cache_update {
     die $@ if $@;
 }
 
+sub extract_even_elements {
+    my ($list) = @_;
+
+    my $ind = 0;
+    return [ grep { ($ind++ % 2) == 0 } @$list ];
+}
+
+sub print_ordered_result {
+    my ($property_list, $data, $result_schema) = @_;
+
+    my $format = get_output_format();
+    my $param_order = extract_even_elements($property_list);
+
+    $options = {};
+
+    PVE::APIClient::CLIFormatter::query_terminal_options($options);
+
+    PVE::APIClient::CLIFormatter::print_api_result($format, $data, $result_schema, $param_order, $options);
+}
 
 1;
index 7688ffe7670cc335d25676280724bf519aa76b69..c87b2cc8eabb7b220d8c392eb43dae96fca5dfe6 100755 (executable)
--- a/pveclient
+++ b/pveclient
@@ -8,6 +8,7 @@ use Cwd 'abs_path';
 use Data::Dumper;
 
 use PVE::APIClient::JSONSchema qw(register_standard_option get_standard_option);
+use PVE::APIClient::CLIFormatter;
 use PVE::APIClient::CLIHandler;
 use PVE::APIClient::PTY;
 
@@ -109,7 +110,10 @@ my $format_result = sub {
     my ($data) = @_;
 
     my $format = PVE::APIClient::Helpers::get_output_format();
-    PVE::APIClient::CLIHandler::print_api_result($format, $data, $path_returns);
+
+    my $options = PVE::APIClient::CLIFormatter::query_terminal_options({});
+
+    PVE::APIClient::CLIFormatter::print_api_result($format, $data, $path_returns, undef, $options);
 };
 
 __PACKAGE__->register_method ({