]>
Commit | Line | Data |
---|---|---|
f53ad23a DM |
1 | package PVE::CLIFormatter; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
fb3a1b29 | 5 | |
4ac85000 | 6 | use I18N::Langinfo; |
99d02523 FG |
7 | use YAML::XS; # supports Dumping JSON::PP::Boolean |
8 | $YAML::XS::Boolean = "JSON::PP"; | |
4ac85000 | 9 | |
84142f1d | 10 | use PVE::JSONSchema; |
4ac85000 | 11 | use PVE::PTY; |
57b33852 | 12 | use PVE::Format; |
fb3a1b29 | 13 | |
f53ad23a | 14 | use JSON; |
eb1c51c2 DM |
15 | use utf8; |
16 | use Encode; | |
f53ad23a | 17 | |
57b33852 SR |
18 | PVE::JSONSchema::register_renderer('timestamp', |
19 | \&PVE::Format::render_timestamp); | |
20 | PVE::JSONSchema::register_renderer('timestamp_gmt', | |
21 | \&PVE::Format::render_timestamp_gmt); | |
22 | PVE::JSONSchema::register_renderer('duration', | |
23 | \&PVE::Format::render_duration); | |
24 | PVE::JSONSchema::register_renderer('fraction_as_percentage', | |
25 | \&PVE::Format::render_fraction_as_percentage); | |
26 | PVE::JSONSchema::register_renderer('bytes', | |
27 | \&PVE::Format::render_bytes); | |
f495c868 | 28 | |
ba752c80 DM |
29 | sub render_yaml { |
30 | my ($value) = @_; | |
31 | ||
99d02523 | 32 | my $data = YAML::XS::Dump($value); |
ba752c80 DM |
33 | $data =~ s/^---[\n\s]//; # remove yaml marker |
34 | ||
35 | return $data; | |
36 | } | |
37 | ||
38 | PVE::JSONSchema::register_renderer('yaml', \&render_yaml); | |
39 | ||
4ac85000 DM |
40 | sub query_terminal_options { |
41 | my ($options) = @_; | |
42 | ||
43 | $options //= {}; | |
44 | ||
45 | if (-t STDOUT) { | |
46 | ($options->{columns}) = PVE::PTY::tcgetsize(*STDOUT); | |
47 | } | |
48 | ||
49 | $options->{encoding} = I18N::Langinfo::langinfo(I18N::Langinfo::CODESET()); | |
50 | ||
51 | $options->{utf8} = 1 if $options->{encoding} eq 'UTF-8'; | |
52 | ||
53 | return $options; | |
54 | } | |
55 | ||
f53ad23a | 56 | sub data_to_text { |
b01a09e7 | 57 | my ($data, $propdef, $options, $terminal_opts) = @_; |
f53ad23a | 58 | |
3cd6f2f3 DM |
59 | return '' if !defined($data); |
60 | ||
b01a09e7 DM |
61 | $terminal_opts //= {}; |
62 | ||
352b7a14 DM |
63 | my $human_readable = $options->{'human-readable'} // 1; |
64 | ||
65 | if ($human_readable && defined($propdef)) { | |
84142f1d DM |
66 | if (my $type = $propdef->{type}) { |
67 | if ($type eq 'boolean') { | |
68 | return $data ? 1 : 0; | |
69 | } | |
70 | } | |
71 | if (!defined($data) && defined($propdef->{default})) { | |
72 | return "($propdef->{default})"; | |
73 | } | |
74 | if (defined(my $renderer = $propdef->{renderer})) { | |
75 | my $code = PVE::JSONSchema::get_renderer($renderer); | |
76 | die "internal error: unknown renderer '$renderer'" if !$code; | |
b01a09e7 | 77 | return $code->($data, $options, $terminal_opts); |
84142f1d DM |
78 | } |
79 | } | |
f53ad23a DM |
80 | |
81 | if (my $class = ref($data)) { | |
b51b930d DM |
82 | # JSON::PP::Boolean requires allow_nonref |
83 | return to_json($data, { allow_nonref => 1, canonical => 1 }); | |
f53ad23a DM |
84 | } else { |
85 | return "$data"; | |
86 | } | |
87 | } | |
88 | ||
89 | # prints a formatted table with a title row. | |
90 | # $data - the data to print (array of objects) | |
91 | # $returnprops -json schema property description | |
92 | # $props_to_print - ordered list of properties to print | |
0c22b00c | 93 | # $options |
2e2a4502 | 94 | # - sort_key: can be used to sort after a specific column, if it isn't set we sort |
890d25d9 | 95 | # after the leftmost column. This can be turned off by passing 0 as sort_key |
352b7a14 DM |
96 | # - noborder: print without asciiart border |
97 | # - noheader: print without table header | |
0c22b00c DM |
98 | # - columns: limit output width (if > 0) |
99 | # - utf8: use utf8 characters for table delimiters | |
100 | ||
f53ad23a | 101 | sub print_text_table { |
b01a09e7 DM |
102 | my ($data, $returnprops, $props_to_print, $options, $terminal_opts) = @_; |
103 | ||
104 | $terminal_opts //= query_terminal_options({}); | |
0c22b00c DM |
105 | |
106 | my $sort_key = $options->{sort_key}; | |
507310df TL |
107 | my $show_border = !$options->{noborder}; |
108 | my $show_header = !$options->{noheader}; | |
b01a09e7 DM |
109 | |
110 | my $columns = $terminal_opts->{columns}; | |
111 | my $utf8 = $terminal_opts->{utf8}; | |
112 | my $encoding = $terminal_opts->{encoding} // 'UTF-8'; | |
f53ad23a | 113 | |
2e2a4502 | 114 | $sort_key //= $props_to_print->[0]; |
41d554d9 | 115 | |
2e2a4502 | 116 | if (defined($sort_key) && $sort_key ne 0) { |
41d554d9 | 117 | my $type = $returnprops->{$sort_key}->{type} // 'string'; |
890d25d9 | 118 | my $cmpfn; |
41d554d9 | 119 | if ($type eq 'integer' || $type eq 'number') { |
890d25d9 | 120 | $cmpfn = sub { $_[0] <=> $_[1] }; |
41d554d9 | 121 | } else { |
890d25d9 | 122 | $cmpfn = sub { $_[0] cmp $_[1] }; |
41d554d9 | 123 | } |
890d25d9 FE |
124 | @$data = sort { |
125 | PVE::Tools::safe_compare($a->{$sort_key}, $b->{$sort_key}, $cmpfn) | |
126 | } @$data; | |
f53ad23a DM |
127 | } |
128 | ||
129 | my $colopts = {}; | |
793ad69b | 130 | |
507310df | 131 | my $border = { m => '', b => '', t => '', h => '' }; |
f53ad23a DM |
132 | my $formatstring = ''; |
133 | ||
134 | my $column_count = scalar(@$props_to_print); | |
135 | ||
41d554d9 DM |
136 | my $tabledata = []; |
137 | ||
138 | foreach my $entry (@$data) { | |
139 | ||
140 | my $height = 1; | |
141 | my $rowdata = {}; | |
142 | ||
143 | for (my $i = 0; $i < $column_count; $i++) { | |
144 | my $prop = $props_to_print->[$i]; | |
145 | my $propinfo = $returnprops->{$prop} // {}; | |
146 | ||
b01a09e7 | 147 | my $text = data_to_text($entry->{$prop}, $propinfo, $options, $terminal_opts); |
41d554d9 DM |
148 | my $lines = [ split(/\n/, $text) ]; |
149 | my $linecount = scalar(@$lines); | |
150 | $height = $linecount if $linecount > $height; | |
151 | ||
152 | my $width = 0; | |
153 | foreach my $line (@$lines) { | |
154 | my $len = length($line); | |
155 | $width = $len if $len > $width; | |
156 | } | |
157 | ||
b9474c96 DM |
158 | $width = ($width =~ m/^(\d+)$/) ? int($1) : 0; # untaint int |
159 | ||
41d554d9 DM |
160 | $rowdata->{$prop} = { |
161 | lines => $lines, | |
162 | width => $width, | |
163 | }; | |
164 | } | |
165 | ||
166 | push @$tabledata, { | |
167 | height => $height, | |
168 | rowdata => $rowdata, | |
169 | }; | |
170 | } | |
171 | ||
f53ad23a DM |
172 | for (my $i = 0; $i < $column_count; $i++) { |
173 | my $prop = $props_to_print->[$i]; | |
174 | my $propinfo = $returnprops->{$prop} // {}; | |
2ec79c0f DM |
175 | my $type = $propinfo->{type} // 'string'; |
176 | my $alignstr = ($type eq 'integer' || $type eq 'number') ? '' : '-'; | |
f53ad23a DM |
177 | |
178 | my $title = $propinfo->{title} // $prop; | |
179 | my $cutoff = $propinfo->{print_width} // $propinfo->{maxLength}; | |
180 | ||
181 | # calculate maximal print width and cutoff | |
182 | my $titlelen = length($title); | |
183 | ||
184 | my $longest = $titlelen; | |
41d554d9 DM |
185 | foreach my $coldata (@$tabledata) { |
186 | my $rowdata = $coldata->{rowdata}->{$prop}; | |
187 | $longest = $rowdata->{width} if $rowdata->{width} > $longest; | |
f53ad23a DM |
188 | } |
189 | $cutoff = $longest if !defined($cutoff) || $cutoff > $longest; | |
f53ad23a DM |
190 | |
191 | $colopts->{$prop} = { | |
192 | title => $title, | |
f53ad23a DM |
193 | cutoff => $cutoff, |
194 | }; | |
195 | ||
507310df | 196 | if ($show_border) { |
eb1c51c2 DM |
197 | if ($i == 0 && ($column_count == 1)) { |
198 | if ($utf8) { | |
2ec79c0f | 199 | $formatstring .= "│ %$alignstr${cutoff}s │"; |
507310df TL |
200 | $border->{t} .= "┌─" . ('─' x $cutoff) . "─┐"; |
201 | $border->{h} .= "╞═" . ('═' x $cutoff) . '═╡'; | |
202 | $border->{m} .= "├─" . ('─' x $cutoff) . "─┤"; | |
203 | $border->{b} .= "└─" . ('─' x $cutoff) . "─┘"; | |
eb1c51c2 | 204 | } else { |
2ec79c0f | 205 | $formatstring .= "| %$alignstr${cutoff}s |"; |
507310df TL |
206 | $border->{m} .= "+-" . ('-' x $cutoff) . "-+"; |
207 | $border->{h} .= "+=" . ('=' x $cutoff) . '='; | |
eb1c51c2 DM |
208 | } |
209 | } elsif ($i == 0) { | |
210 | if ($utf8) { | |
2ec79c0f | 211 | $formatstring .= "│ %$alignstr${cutoff}s "; |
507310df TL |
212 | $border->{t} .= "┌─" . ('─' x $cutoff) . '─'; |
213 | $border->{h} .= "╞═" . ('═' x $cutoff) . '═'; | |
214 | $border->{m} .= "├─" . ('─' x $cutoff) . '─'; | |
215 | $border->{b} .= "└─" . ('─' x $cutoff) . '─'; | |
eb1c51c2 | 216 | } else { |
2ec79c0f | 217 | $formatstring .= "| %$alignstr${cutoff}s "; |
507310df TL |
218 | $border->{m} .= "+-" . ('-' x $cutoff) . '-'; |
219 | $border->{h} .= "+=" . ('=' x $cutoff) . '='; | |
eb1c51c2 DM |
220 | } |
221 | } elsif ($i == ($column_count - 1)) { | |
222 | if ($utf8) { | |
2ec79c0f | 223 | $formatstring .= "│ %$alignstr${cutoff}s │"; |
507310df TL |
224 | $border->{t} .= "┬─" . ('─' x $cutoff) . "─┐"; |
225 | $border->{h} .= "╪═" . ('═' x $cutoff) . '═╡'; | |
226 | $border->{m} .= "┼─" . ('─' x $cutoff) . "─┤"; | |
227 | $border->{b} .= "┴─" . ('─' x $cutoff) . "─┘"; | |
eb1c51c2 | 228 | } else { |
2ec79c0f | 229 | $formatstring .= "| %$alignstr${cutoff}s |"; |
507310df TL |
230 | $border->{m} .= "+-" . ('-' x $cutoff) . "-+"; |
231 | $border->{h} .= "+=" . ('=' x $cutoff) . "=+"; | |
eb1c51c2 | 232 | } |
793ad69b | 233 | } else { |
eb1c51c2 | 234 | if ($utf8) { |
2ec79c0f | 235 | $formatstring .= "│ %$alignstr${cutoff}s "; |
507310df TL |
236 | $border->{t} .= "┬─" . ('─' x $cutoff) . '─'; |
237 | $border->{h} .= "╪═" . ('═' x $cutoff) . '═'; | |
238 | $border->{m} .= "┼─" . ('─' x $cutoff) . '─'; | |
239 | $border->{b} .= "┴─" . ('─' x $cutoff) . '─'; | |
eb1c51c2 | 240 | } else { |
2ec79c0f | 241 | $formatstring .= "| %$alignstr${cutoff}s "; |
507310df TL |
242 | $border->{m} .= "+-" . ('-' x $cutoff) . '-'; |
243 | $border->{h} .= "+=" . ('=' x $cutoff) . '='; | |
eb1c51c2 | 244 | } |
793ad69b DM |
245 | } |
246 | } else { | |
247 | # skip alignment and cutoff on last column | |
2ec79c0f | 248 | $formatstring .= ($i == ($column_count - 1)) ? "%s" : "%$alignstr${cutoff}s "; |
f53ad23a DM |
249 | } |
250 | } | |
251 | ||
507310df TL |
252 | $border->{t} = $border->{m} if !length($border->{t}); |
253 | $border->{b} = $border->{m} if !length($border->{b}); | |
eb1c51c2 | 254 | |
41d554d9 DM |
255 | my $writeln = sub { |
256 | my ($text) = @_; | |
257 | ||
258 | if ($columns) { | |
259 | print encode($encoding, substr($text, 0, $columns) . "\n"); | |
260 | } else { | |
261 | print encode($encoding, $text) . "\n"; | |
262 | } | |
263 | }; | |
264 | ||
507310df | 265 | $writeln->($border->{t}) if $show_border; |
793ad69b | 266 | |
507310df | 267 | if ($show_header) { |
352b7a14 DM |
268 | my $text = sprintf $formatstring, map { $colopts->{$_}->{title} } @$props_to_print; |
269 | $writeln->($text); | |
507310df | 270 | $border->{sep} = $border->{h}; |
9e3aaec4 | 271 | } else { |
507310df | 272 | $border->{sep} = $border->{m}; |
352b7a14 DM |
273 | } |
274 | ||
275 | for (my $i = 0; $i < scalar(@$tabledata); $i++) { | |
276 | my $coldata = $tabledata->[$i]; | |
277 | ||
507310df TL |
278 | if ($show_border && ($i != 0 || $show_header)) { |
279 | $writeln->($border->{sep}); | |
280 | $border->{sep} = $border->{m}; | |
9e3aaec4 | 281 | } |
41d554d9 DM |
282 | |
283 | for (my $i = 0; $i < $coldata->{height}; $i++) { | |
352b7a14 | 284 | my $text = sprintf $formatstring, map { |
41d554d9 DM |
285 | substr($coldata->{rowdata}->{$_}->{lines}->[$i] // '', 0, $colopts->{$_}->{cutoff}); |
286 | } @$props_to_print; | |
287 | ||
288 | $writeln->($text); | |
289 | } | |
f53ad23a | 290 | } |
41d554d9 | 291 | |
507310df | 292 | $writeln->($border->{b}) if $show_border; |
f53ad23a DM |
293 | } |
294 | ||
c5adc5f9 DM |
295 | sub extract_properties_to_print { |
296 | my ($propdef) = @_; | |
297 | ||
298 | my $required = []; | |
299 | my $optional = []; | |
300 | ||
301 | foreach my $key (keys %$propdef) { | |
302 | my $prop = $propdef->{$key}; | |
303 | if ($prop->{optional}) { | |
304 | push @$optional, $key; | |
305 | } else { | |
306 | push @$required, $key; | |
307 | } | |
308 | } | |
309 | ||
310 | return [ sort(@$required), sort(@$optional) ]; | |
311 | } | |
312 | ||
f53ad23a DM |
313 | # prints the result of an API GET call returning an array as a table. |
314 | # takes formatting information from the results property of the call | |
315 | # if $props_to_print is provided, prints only those columns. otherwise | |
316 | # takes all fields of the results property, with a fallback | |
fb3a1b29 | 317 | # to all fields occurring in items of $data. |
f53ad23a | 318 | sub print_api_list { |
b01a09e7 | 319 | my ($data, $result_schema, $props_to_print, $options, $terminal_opts) = @_; |
f53ad23a DM |
320 | |
321 | die "can only print object lists\n" | |
322 | if !($result_schema->{type} eq 'array' && $result_schema->{items}->{type} eq 'object'); | |
323 | ||
324 | my $returnprops = $result_schema->{items}->{properties}; | |
325 | ||
c5adc5f9 DM |
326 | $props_to_print = extract_properties_to_print($returnprops) |
327 | if !defined($props_to_print); | |
328 | ||
329 | if (!scalar(@$props_to_print)) { | |
330 | my $all_props = {}; | |
331 | foreach my $obj (@$data) { | |
332 | foreach my $key (keys %$obj) { | |
333 | $all_props->{$key} = 1; | |
f53ad23a | 334 | } |
f53ad23a | 335 | } |
c5adc5f9 | 336 | $props_to_print = [ sort keys %{$all_props} ]; |
f53ad23a DM |
337 | } |
338 | ||
c5adc5f9 DM |
339 | die "unable to detect list properties\n" if !scalar(@$props_to_print); |
340 | ||
b01a09e7 | 341 | print_text_table($data, $returnprops, $props_to_print, $options, $terminal_opts); |
f53ad23a DM |
342 | } |
343 | ||
b51b930d DM |
344 | my $guess_type = sub { |
345 | my $data = shift; | |
346 | ||
347 | return 'null' if !defined($data); | |
348 | ||
349 | my $class = ref($data); | |
350 | return 'string' if !$class; | |
351 | ||
352 | if ($class eq 'HASH') { | |
353 | return 'object'; | |
354 | } elsif ($class eq 'ARRAY') { | |
355 | return 'array'; | |
356 | } else { | |
357 | return 'string'; # better than nothing | |
358 | } | |
359 | }; | |
360 | ||
f53ad23a | 361 | sub print_api_result { |
b01a09e7 | 362 | my ($data, $result_schema, $props_to_print, $options, $terminal_opts) = @_; |
0c22b00c | 363 | |
352b7a14 DM |
364 | return if $options->{quiet}; |
365 | ||
b01a09e7 | 366 | $terminal_opts //= query_terminal_options({}); |
f53ad23a | 367 | |
4cbcd138 | 368 | my $format = $options->{'output-format'} // 'text'; |
f0bcf4d4 | 369 | |
fbd10e04 | 370 | if ($result_schema && defined($result_schema->{type})) { |
b51b930d | 371 | return if $result_schema->{type} eq 'null'; |
c0b8717c | 372 | return if $result_schema->{optional} && !defined($data); |
b51b930d DM |
373 | } else { |
374 | my $type = $guess_type->($data); | |
375 | $result_schema = { type => $type }; | |
376 | $result_schema->{items} = { type => $guess_type->($data->[0]) } if $type eq 'array'; | |
377 | } | |
f53ad23a | 378 | |
ac6c61bf | 379 | if ($format eq 'yaml') { |
99d02523 | 380 | print encode('UTF-8', YAML::XS::Dump($data)); |
ac6c61bf | 381 | } elsif ($format eq 'json') { |
cd6591d3 DM |
382 | # Note: we always use utf8 encoding for json format |
383 | print to_json($data, {utf8 => 1, allow_nonref => 1, canonical => 1 }) . "\n"; | |
384 | } elsif ($format eq 'json-pretty') { | |
43b104c4 | 385 | # Note: we always use utf8 encoding for json format |
f53ad23a | 386 | print to_json($data, {utf8 => 1, allow_nonref => 1, canonical => 1, pretty => 1 }); |
cd6591d3 | 387 | } elsif ($format eq 'text') { |
43b104c4 | 388 | my $encoding = $options->{encoding} // 'UTF-8'; |
f53ad23a DM |
389 | my $type = $result_schema->{type}; |
390 | if ($type eq 'object') { | |
c5adc5f9 DM |
391 | $props_to_print = extract_properties_to_print($result_schema->{properties}) |
392 | if !defined($props_to_print); | |
393 | $props_to_print = [ sort keys %$data ] if !scalar(@$props_to_print); | |
2a174d2a | 394 | my $kvstore = []; |
f53ad23a | 395 | foreach my $key (@$props_to_print) { |
a05db8b5 | 396 | next if !defined($data->{$key}); |
b01a09e7 | 397 | push @$kvstore, { key => $key, value => data_to_text($data->{$key}, $result_schema->{properties}->{$key}, $options, $terminal_opts) }; |
f53ad23a | 398 | } |
2a174d2a | 399 | my $schema = { type => 'array', items => { type => 'object' }}; |
b01a09e7 | 400 | print_api_list($kvstore, $schema, ['key', 'value'], $options, $terminal_opts); |
f53ad23a | 401 | } elsif ($type eq 'array') { |
b3d7568f TL |
402 | if (ref($data) eq 'ARRAY') { |
403 | return if !scalar(@$data); | |
404 | } elsif (ref($data) eq 'HASH') { | |
405 | return if !scalar($data->%*); | |
406 | die "got hash object, but result schema specified array!\n" | |
407 | } | |
f53ad23a DM |
408 | my $item_type = $result_schema->{items}->{type}; |
409 | if ($item_type eq 'object') { | |
b01a09e7 | 410 | print_api_list($data, $result_schema, $props_to_print, $options, $terminal_opts); |
f53ad23a | 411 | } else { |
c850b998 DM |
412 | my $kvstore = []; |
413 | foreach my $value (@$data) { | |
414 | push @$kvstore, { value => $value }; | |
f53ad23a | 415 | } |
c850b998 | 416 | my $schema = { type => 'array', items => { type => 'object', properties => { value => $result_schema->{items} }}}; |
b01a09e7 | 417 | print_api_list($kvstore, $schema, ['value'], { %$options, noheader => 1 }, $terminal_opts); |
f53ad23a DM |
418 | } |
419 | } else { | |
43b104c4 | 420 | print encode($encoding, "$data\n"); |
f53ad23a DM |
421 | } |
422 | } else { | |
423 | die "internal error: unknown output format"; # should not happen | |
424 | } | |
425 | } | |
426 | ||
6bc09493 DM |
427 | sub print_api_result_plain { |
428 | my ($data, $result_schema, $props_to_print, $options) = @_; | |
429 | ||
430 | # avoid borders and header, ignore terminal width | |
431 | $options = $options ? { %$options } : {}; # copy | |
432 | ||
433 | $options->{noheader} //= 1; | |
434 | $options->{noborder} //= 1; | |
435 | ||
436 | print_api_result($data, $result_schema, $props_to_print, $options, {}); | |
437 | } | |
438 | ||
f53ad23a | 439 | 1; |