[pve-devel] [PATCH v2 common] cli-formatter: avoid warning when trying to sort on undefined key

Christian Ebner c.ebner at proxmox.com
Tue Nov 26 13:10:12 CET 2019


Example:
    pvesh get /nodes/{node}/qemu/{vmid}/rrddata --timeframe day

If the sorting key is not defined in the dataset, e.g. when a VM was not running
for some time within the given timeframe, this resulted in several ugly warnings.

Signed-off-by: Christian Ebner <c.ebner at proxmox.com>
---

v2: Oops, v1 is nonsense and breaks sorting.

 src/PVE/CLIFormatter.pm | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/PVE/CLIFormatter.pm b/src/PVE/CLIFormatter.pm
index 0e9cbe6..21fa2df 100644
--- a/src/PVE/CLIFormatter.pm
+++ b/src/PVE/CLIFormatter.pm
@@ -175,9 +175,11 @@ sub print_text_table {
     if (defined($sort_key) && $sort_key ne 0) {
 	my $type = $returnprops->{$sort_key}->{type} // 'string';
 	if ($type eq 'integer' || $type eq 'number') {
-	    @$data = sort { $a->{$sort_key} <=> $b->{$sort_key} } @$data;
+	    @$data = sort { $a->{$sort_key} <=> $b->{$sort_key}
+		if defined $a->{$sort_key} && defined $b->{$sort_key} } @$data;
 	} else {
-	    @$data = sort { $a->{$sort_key} cmp $b->{$sort_key} } @$data;
+	    @$data = sort { $a->{$sort_key} cmp $b->{$sort_key}
+		if defined $a->{$sort_key} && defined $b->{$sort_key} } @$data;
 	}
     }
 
-- 
2.20.1




More information about the pve-devel mailing list