]>
Commit | Line | Data |
---|---|---|
f53ad23a DM |
1 | package PVE::CLIFormatter; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use JSON; | |
6 | ||
7 | sub data_to_text { | |
8 | my ($data) = @_; | |
9 | ||
10 | return '' if !defined($data); | |
11 | ||
12 | if (my $class = ref($data)) { | |
13 | return to_json($data, { utf8 => 1, canonical => 1 }); | |
14 | } else { | |
15 | return "$data"; | |
16 | } | |
17 | } | |
18 | ||
19 | # prints a formatted table with a title row. | |
20 | # $data - the data to print (array of objects) | |
21 | # $returnprops -json schema property description | |
22 | # $props_to_print - ordered list of properties to print | |
23 | # $sort_key can be used to sort after a column, if it isn't set we sort | |
24 | # after the leftmost column (with no undef value in $data) this can be | |
25 | # turned off by passing 0 as $sort_key | |
26 | sub print_text_table { | |
27 | my ($data, $returnprops, $props_to_print, $sort_key) = @_; | |
28 | ||
29 | my $autosort = 1; | |
30 | if (defined($sort_key) && $sort_key eq 0) { | |
31 | $autosort = 0; | |
32 | $sort_key = undef; | |
33 | } | |
34 | ||
35 | my $colopts = {}; | |
36 | my $formatstring = ''; | |
37 | ||
38 | my $column_count = scalar(@$props_to_print); | |
39 | ||
40 | for (my $i = 0; $i < $column_count; $i++) { | |
41 | my $prop = $props_to_print->[$i]; | |
42 | my $propinfo = $returnprops->{$prop} // {}; | |
43 | ||
44 | my $title = $propinfo->{title} // $prop; | |
45 | my $cutoff = $propinfo->{print_width} // $propinfo->{maxLength}; | |
46 | ||
47 | # calculate maximal print width and cutoff | |
48 | my $titlelen = length($title); | |
49 | ||
50 | my $longest = $titlelen; | |
51 | my $sortable = $autosort; | |
52 | foreach my $entry (@$data) { | |
53 | my $len = length(data_to_text($entry->{$prop})) // 0; | |
54 | $longest = $len if $len > $longest; | |
55 | $sortable = 0 if !defined($entry->{$prop}); | |
56 | } | |
57 | $cutoff = $longest if !defined($cutoff) || $cutoff > $longest; | |
58 | $sort_key //= $prop if $sortable; | |
59 | ||
60 | $colopts->{$prop} = { | |
61 | title => $title, | |
62 | default => $propinfo->{default} // '', | |
63 | cutoff => $cutoff, | |
64 | }; | |
65 | ||
66 | # skip alignment and cutoff on last column | |
67 | $formatstring .= ($i == ($column_count - 1)) ? "%s\n" : "%-${cutoff}s "; | |
68 | } | |
69 | ||
70 | printf $formatstring, map { $colopts->{$_}->{title} } @$props_to_print; | |
71 | ||
72 | if (defined($sort_key)) { | |
73 | my $type = $returnprops->{$sort_key}->{type} // 'string'; | |
74 | if ($type eq 'integer' || $type eq 'number') { | |
75 | @$data = sort { $a->{$sort_key} <=> $b->{$sort_key} } @$data; | |
76 | } else { | |
77 | @$data = sort { $a->{$sort_key} cmp $b->{$sort_key} } @$data; | |
78 | } | |
79 | } | |
80 | ||
81 | foreach my $entry (@$data) { | |
82 | printf $formatstring, map { | |
83 | substr(data_to_text($entry->{$_}) // $colopts->{$_}->{default}, | |
84 | 0, $colopts->{$_}->{cutoff}); | |
85 | } @$props_to_print; | |
86 | } | |
87 | } | |
88 | ||
89 | # prints the result of an API GET call returning an array as a table. | |
90 | # takes formatting information from the results property of the call | |
91 | # if $props_to_print is provided, prints only those columns. otherwise | |
92 | # takes all fields of the results property, with a fallback | |
93 | # to all fields occuring in items of $data. | |
94 | sub print_api_list { | |
95 | my ($data, $result_schema, $props_to_print, $sort_key) = @_; | |
96 | ||
97 | die "can only print object lists\n" | |
98 | if !($result_schema->{type} eq 'array' && $result_schema->{items}->{type} eq 'object'); | |
99 | ||
100 | my $returnprops = $result_schema->{items}->{properties}; | |
101 | ||
102 | if (!defined($props_to_print)) { | |
103 | $props_to_print = [ sort keys %$returnprops ]; | |
104 | if (!scalar(@$props_to_print)) { | |
105 | my $all_props = {}; | |
106 | foreach my $obj (@{$data}) { | |
107 | foreach my $key (keys %{$obj}) { | |
108 | $all_props->{ $key } = 1; | |
109 | } | |
110 | } | |
111 | $props_to_print = [ sort keys %{$all_props} ]; | |
112 | } | |
113 | die "unable to detect list properties\n" if !scalar(@$props_to_print); | |
114 | } | |
115 | ||
116 | print_text_table($data, $returnprops, $props_to_print, $sort_key); | |
117 | } | |
118 | ||
119 | sub print_api_result { | |
120 | my ($format, $data, $result_schema, $props_to_print, $sort_key) = @_; | |
121 | ||
122 | return if $result_schema->{type} eq 'null'; | |
123 | ||
124 | if ($format eq 'json') { | |
125 | print to_json($data, {utf8 => 1, allow_nonref => 1, canonical => 1, pretty => 1 }); | |
126 | } elsif ($format eq 'text') { | |
127 | my $type = $result_schema->{type}; | |
128 | if ($type eq 'object') { | |
129 | $props_to_print = [ sort keys %$data ] if !defined($props_to_print); | |
130 | foreach my $key (@$props_to_print) { | |
131 | print $key . ": " . data_to_text($data->{$key}) . "\n"; | |
132 | } | |
133 | } elsif ($type eq 'array') { | |
134 | return if !scalar(@$data); | |
135 | my $item_type = $result_schema->{items}->{type}; | |
136 | if ($item_type eq 'object') { | |
137 | print_api_list($data, $result_schema, $props_to_print, $sort_key); | |
138 | } else { | |
139 | foreach my $entry (@$data) { | |
140 | print data_to_text($entry) . "\n"; | |
141 | } | |
142 | } | |
143 | } else { | |
144 | print "$data\n"; | |
145 | } | |
146 | } else { | |
147 | die "internal error: unknown output format"; # should not happen | |
148 | } | |
149 | } | |
150 | ||
151 | 1; |