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