[pve-devel] [PATCH pve-client] Use print_text_table in 'list' and 'remote list'

René Jochum r.jochum at proxmox.com
Mon Jun 25 09:25:49 CEST 2018


Signed-off-by: René Jochum <r.jochum at proxmox.com>
---
 PVE/APIClient/Commands/list.pm   | 22 ++++++++++++++++------
 PVE/APIClient/Commands/remote.pm | 21 ++++++++++++++++++---
 2 files changed, 34 insertions(+), 9 deletions(-)

diff --git a/PVE/APIClient/Commands/list.pm b/PVE/APIClient/Commands/list.pm
index 2030c51..92f613d 100644
--- a/PVE/APIClient/Commands/list.pm
+++ b/PVE/APIClient/Commands/list.pm
@@ -32,16 +32,26 @@ __PACKAGE__->register_method ({
 	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 $formatopts = [
+		{key => 'node', title => 'Node'},
+		{key => 'vmid', title => 'VMID'},
+		{key => 'type', title => 'Type'},
+		{key => 'name', title => 'Name'},
+		{key => 'status', title => 'Status'},
+	    ];
+
 	    my $data = [];
 	    for my $el (@$resources) {
-		push(@$data, [$el->{node}, $el->{vmid}, $el->{type}, $el->{name}, $el->{status}]);
+		push(@$data, {
+		    node => $el->{node},
+		    vmid => $el->{vmid},
+		    type => $el->{type},
+		    name => $el->{name},
+		    status => $el->{status}});
 	    }
 
-	    printf("%10s %10s %10s %10s %10s\n", @$headers);
-	    for my $row (@$data) {
-		printf("%10s %10s %10s %10s %10s\n", @$row);
-	    }
+	    my $data = [sort { $a->{vmid} cmp $b->{vmid} } @$data];
+	    PVE::APIClient::CLIHandler::print_text_table($formatopts, $data);
 	} else {
 	    print JSON::to_json($resources, {utf8 => 1, pretty => 1});
 	}
diff --git a/PVE/APIClient/Commands/remote.pm b/PVE/APIClient/Commands/remote.pm
index b233901..a5b0c34 100644
--- a/PVE/APIClient/Commands/remote.pm
+++ b/PVE/APIClient/Commands/remote.pm
@@ -31,14 +31,29 @@ __PACKAGE__->register_method ({
     code => sub {
 	my $config = PVE::APIClient::Config->load();
 
-	printf("%10s %10s %10s %10s %100s\n", "Name", "Host", "Port", "Username", "Fingerprint");
+	my $formatopts = [
+	    {key => 'name', title => 'Name'},
+	    {key => 'host', title => 'Host'},
+	    {key => 'port', title => 'Port', default => '-'},
+	    {key => 'username', title => 'Username'},
+	    {key => 'fingerprint', title => 'Fingerprint', default => '-'},
+	];
+
+	my $tabledata = [];
 	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(@$tabledata, {
+		name => $name,
+		host => $data->{'host'},
+		port => $data->{'port'},
+		username => $data->{'username'},
+		fingerprint => $data->{'fingerprint'}});
 	}
 
+	PVE::APIClient::CLIHandler::print_text_table($formatopts, $tabledata);
+
 	return undef;
     }});
 
-- 
2.11.0



More information about the pve-devel mailing list