]> git.proxmox.com Git - pve-client.git/blobdiff - PVE/APIClient/Commands/list.pm
adopt code for changes in pve-common
[pve-client.git] / PVE / APIClient / Commands / list.pm
index 2030c515423d0fecbd393a1395e37da62562785c..10a8f7c5afd0831b994d17c2966aefc8e4693dea 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('pve-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;