]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/CLIHandler.pm
cli: print_text_table: fix for poperties without schema
[pve-common.git] / src / PVE / CLIHandler.pm
index 3013cc213dd2069365a67391c1b2a5f3b9a2c5cc..0502c4867987c119cef1046796e5941c12ae07a4 100644 (file)
@@ -445,15 +445,15 @@ sub data_to_text {
 # $data - the data to print (array of objects)
 # $returnprops -json schema property description
 # $props_to_print - ordered list of properties to print
-# $sort_key is either a column name, or integer 1 which simply
-# sorts the output according to the leftmost column not containing
-# any undef. if not specified, we do not change order.
+# $sort_key can be used to sort after a column, if it isn't set we sort
+#   after the leftmost column (with no undef value in $data) this can be
+#   turned off by passing 0 as $sort_key
 sub print_text_table {
     my ($data, $returnprops, $props_to_print, $sort_key) = @_;
 
-    my $autosort = 0;
-    if (defined($sort_key) && ($sort_key eq 1)) {
-       $autosort = 1;
+    my $autosort = 1;
+    if (defined($sort_key) && $sort_key eq 0) {
+       $autosort = 0;
        $sort_key = undef;
     }
 
@@ -464,7 +464,7 @@ sub print_text_table {
 
     for (my $i = 0; $i < $column_count; $i++) {
        my $prop = $props_to_print->[$i];
-       my $propinfo = $returnprops->{$prop};
+       my $propinfo = $returnprops->{$prop} // {};
        die "undefined property '$prop'" if !$propinfo;
 
        my $title = $propinfo->{title} // $prop;
@@ -480,7 +480,7 @@ sub print_text_table {
            $longest = $len if $len > $longest;
            $sortable = 0 if !defined($entry->{$prop});
        }
-       $cutoff = (defined($cutoff) && $cutoff < $longest) ? $cutoff : $longest;
+       $cutoff = $longest if !defined($cutoff) || $cutoff > $longest;
        $sort_key //= $prop if $sortable;
 
        $colopts->{$prop} = {
@@ -489,14 +489,15 @@ sub print_text_table {
            cutoff => $cutoff,
        };
 
+       # skip alignment and cutoff on last column
        $formatstring .= ($i == ($column_count - 1)) ? "%s\n" : "%-${cutoff}s ";
     }
 
     printf $formatstring, map { $colopts->{$_}->{title} } @$props_to_print;
 
     if (defined($sort_key)) {
-       if ($returnprops->{$sort_key}->{type} eq 'integer' ||
-           $returnprops->{$sort_key}->{type} eq 'number') {
+       my $type = $returnprops->{$sort_key}->{type};
+       if ($type eq 'integer' || $type eq 'number') {
            @$data = sort { $a->{$sort_key} <=> $b->{$sort_key} } @$data;
        } else {
            @$data = sort { $a->{$sort_key} cmp $b->{$sort_key} } @$data;
@@ -551,14 +552,9 @@ sub print_api_result {
     } elsif ($format eq 'text') {
        my $type = $result_schema->{type};
        if ($type eq 'object') {
-           if (defined($props_to_print)) {
-               foreach my $key (@$props_to_print) {
-                   print $key . ": " .  data_to_text($data->{$key}) . "\n";
-               }
-           } else {
-               foreach my $key (sort keys %$data) {
-                   print $key . ": " .  data_to_text($data->{$key}) . "\n";
-               }
+           $props_to_print = [ sort keys %$data ] if !defined($props_to_print);
+           foreach my $key (@$props_to_print) {
+               print $key . ": " .  data_to_text($data->{$key}) . "\n";
            }
        } elsif ($type eq 'array') {
            return if !scalar(@$data);