[pve-devel] [PATCH common] print_text_table: sort by leftmost column with no undefined value

Fabian Ebner f.ebner at proxmox.com
Mon Apr 27 11:41:01 CEST 2020


This restores the behavior for sort_key as it's described in the
comment for print_text_table.

Fixes warnings, e.g.
pvesh get /nodes/<NODE>/network
would print "use of uninitialized"-warnings if there are inactive
network interfaces, because for those, 'active' is undef.

Signed-off-by: Fabian Ebner <f.ebner at proxmox.com>
---
 src/PVE/CLIFormatter.pm | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/src/PVE/CLIFormatter.pm b/src/PVE/CLIFormatter.pm
index 0e9cbe6..9f141e1 100644
--- a/src/PVE/CLIFormatter.pm
+++ b/src/PVE/CLIFormatter.pm
@@ -144,6 +144,23 @@ sub data_to_text {
     }
 }
 
+sub leftmost_sortable_property {
+    my ($data, $properties) = @_;
+
+    foreach my $property (@{$properties}) {
+	my $sortable = 1;
+	foreach my $entry (@{$data}) {
+	    if (!defined($entry->{$property})) {
+		$sortable = 0;
+		last;
+	    }
+	}
+	return $property if $sortable eq 1;
+    }
+
+    return undef;
+}
+
 # prints a formatted table with a title row.
 # $data - the data to print (array of objects)
 # $returnprops -json schema property description
@@ -170,7 +187,7 @@ sub print_text_table {
     my $utf8 = $terminal_opts->{utf8};
     my $encoding = $terminal_opts->{encoding} // 'UTF-8';
 
-    $sort_key //= $props_to_print->[0];
+    $sort_key //= leftmost_sortable_property($data, $props_to_print);
 
     if (defined($sort_key) && $sort_key ne 0) {
 	my $type = $returnprops->{$sort_key}->{type} // 'string';
-- 
2.20.1





More information about the pve-devel mailing list