]> git.proxmox.com Git - pve-common.git/blame - src/PVE/CLIFormatter.pm
render_bytes: check format, untaint before calling sprintf
[pve-common.git] / src / PVE / CLIFormatter.pm
CommitLineData
f53ad23a
DM
1package PVE::CLIFormatter;
2
3use strict;
4use warnings;
4ac85000 5use I18N::Langinfo;
5e287f60 6use POSIX qw(strftime);
ba752c80 7use CPAN::Meta::YAML; # comes with perl-modules
4ac85000 8
84142f1d 9use PVE::JSONSchema;
4ac85000 10use PVE::PTY;
f53ad23a 11use JSON;
eb1c51c2
DM
12use utf8;
13use Encode;
f53ad23a 14
5e287f60
DM
15sub render_timestamp {
16 my ($epoch) = @_;
17
18 # ISO 8601 date format
19 return strftime("%F %H:%M:%S", localtime($epoch));
20}
21
22PVE::JSONSchema::register_renderer('timestamp', \&render_timestamp);
23
e735d7b0
DM
24sub 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
31PVE::JSONSchema::register_renderer('timestamp_gmt', \&render_timestamp_gmt);
32
f495c868
DM
33sub 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
58PVE::JSONSchema::register_renderer('duration', \&render_duration);
59
60sub render_fraction_as_percentage {
61 my ($fraction) = @_;
62
63 return sprintf("%.2f%%", $fraction*100);
64}
65
66PVE::JSONSchema::register_renderer(
67 'fraction_as_percentage', \&render_fraction_as_percentage);
68
69sub render_bytes {
70 my ($value) = @_;
71
a91ee28f
DM
72 return $value if $value !~ m/^(\d+)$/;
73 $value = int($1); # untaint for sprintf
74
f495c868
DM
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
86PVE::JSONSchema::register_renderer('bytes', \&render_bytes);
87
ba752c80
DM
88sub 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
97PVE::JSONSchema::register_renderer('yaml', \&render_yaml);
98
4ac85000
DM
99sub 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
f53ad23a 115sub data_to_text {
b01a09e7 116 my ($data, $propdef, $options, $terminal_opts) = @_;
f53ad23a 117
3cd6f2f3
DM
118 return '' if !defined($data);
119
b01a09e7
DM
120 $terminal_opts //= {};
121
352b7a14
DM
122 my $human_readable = $options->{'human-readable'} // 1;
123
124 if ($human_readable && defined($propdef)) {
84142f1d
DM
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;
b01a09e7 136 return $code->($data, $options, $terminal_opts);
84142f1d
DM
137 }
138 }
f53ad23a
DM
139
140 if (my $class = ref($data)) {
b51b930d
DM
141 # JSON::PP::Boolean requires allow_nonref
142 return to_json($data, { allow_nonref => 1, canonical => 1 });
f53ad23a
DM
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
0c22b00c 152# $options
2e2a4502 153# - sort_key: can be used to sort after a specific column, if it isn't set we sort
f53ad23a 154# after the leftmost column (with no undef value in $data) this can be
0c22b00c 155# turned off by passing 0 as sort_key
352b7a14
DM
156# - noborder: print without asciiart border
157# - noheader: print without table header
0c22b00c
DM
158# - columns: limit output width (if > 0)
159# - utf8: use utf8 characters for table delimiters
160
f53ad23a 161sub print_text_table {
b01a09e7
DM
162 my ($data, $returnprops, $props_to_print, $options, $terminal_opts) = @_;
163
164 $terminal_opts //= query_terminal_options({});
0c22b00c
DM
165
166 my $sort_key = $options->{sort_key};
352b7a14
DM
167 my $border = !$options->{noborder};
168 my $header = !$options->{noheader};
b01a09e7
DM
169
170 my $columns = $terminal_opts->{columns};
171 my $utf8 = $terminal_opts->{utf8};
172 my $encoding = $terminal_opts->{encoding} // 'UTF-8';
f53ad23a 173
2e2a4502 174 $sort_key //= $props_to_print->[0];
41d554d9 175
2e2a4502 176 if (defined($sort_key) && $sort_key ne 0) {
41d554d9
DM
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 }
f53ad23a
DM
183 }
184
185 my $colopts = {};
793ad69b 186
eb1c51c2
DM
187 my $borderstring_m = '';
188 my $borderstring_b = '';
189 my $borderstring_t = '';
f53ad23a
DM
190 my $formatstring = '';
191
192 my $column_count = scalar(@$props_to_print);
193
41d554d9
DM
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
b01a09e7 205 my $text = data_to_text($entry->{$prop}, $propinfo, $options, $terminal_opts);
41d554d9
DM
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
b9474c96
DM
216 $width = ($width =~ m/^(\d+)$/) ? int($1) : 0; # untaint int
217
41d554d9
DM
218 $rowdata->{$prop} = {
219 lines => $lines,
220 width => $width,
221 };
222 }
223
224 push @$tabledata, {
225 height => $height,
226 rowdata => $rowdata,
227 };
228 }
229
f53ad23a
DM
230 for (my $i = 0; $i < $column_count; $i++) {
231 my $prop = $props_to_print->[$i];
232 my $propinfo = $returnprops->{$prop} // {};
2ec79c0f
DM
233 my $type = $propinfo->{type} // 'string';
234 my $alignstr = ($type eq 'integer' || $type eq 'number') ? '' : '-';
f53ad23a
DM
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;
41d554d9
DM
243 foreach my $coldata (@$tabledata) {
244 my $rowdata = $coldata->{rowdata}->{$prop};
245 $longest = $rowdata->{width} if $rowdata->{width} > $longest;
f53ad23a
DM
246 }
247 $cutoff = $longest if !defined($cutoff) || $cutoff > $longest;
f53ad23a
DM
248
249 $colopts->{$prop} = {
250 title => $title,
f53ad23a
DM
251 cutoff => $cutoff,
252 };
253
793ad69b 254 if ($border) {
eb1c51c2
DM
255 if ($i == 0 && ($column_count == 1)) {
256 if ($utf8) {
2ec79c0f 257 $formatstring .= "│ %$alignstr${cutoff}s │";
41d554d9
DM
258 $borderstring_t .= "┌─" . ('─' x $cutoff) . "─┐";
259 $borderstring_m .= "├─" . ('─' x $cutoff) . "─┤";
260 $borderstring_b .= "└─" . ('─' x $cutoff) . "─┘";
eb1c51c2 261 } else {
2ec79c0f 262 $formatstring .= "| %$alignstr${cutoff}s |";
41d554d9 263 $borderstring_m .= "+-" . ('-' x $cutoff) . "-+";
eb1c51c2
DM
264 }
265 } elsif ($i == 0) {
266 if ($utf8) {
2ec79c0f 267 $formatstring .= "│ %$alignstr${cutoff}s ";
eb1c51c2
DM
268 $borderstring_t .= "┌─" . ('─' x $cutoff) . '─';
269 $borderstring_m .= "├─" . ('─' x $cutoff) . '─';
270 $borderstring_b .= "└─" . ('─' x $cutoff) . '─';
271 } else {
2ec79c0f 272 $formatstring .= "| %$alignstr${cutoff}s ";
eb1c51c2
DM
273 $borderstring_m .= "+-" . ('-' x $cutoff) . '-';
274 }
275 } elsif ($i == ($column_count - 1)) {
276 if ($utf8) {
2ec79c0f 277 $formatstring .= "│ %$alignstr${cutoff}s │";
41d554d9
DM
278 $borderstring_t .= "┬─" . ('─' x $cutoff) . "─┐";
279 $borderstring_m .= "┼─" . ('─' x $cutoff) . "─┤";
280 $borderstring_b .= "┴─" . ('─' x $cutoff) . "─┘";
eb1c51c2 281 } else {
2ec79c0f 282 $formatstring .= "| %$alignstr${cutoff}s |";
41d554d9 283 $borderstring_m .= "+-" . ('-' x $cutoff) . "-+";
eb1c51c2 284 }
793ad69b 285 } else {
eb1c51c2 286 if ($utf8) {
2ec79c0f 287 $formatstring .= "│ %$alignstr${cutoff}s ";
eb1c51c2
DM
288 $borderstring_t .= "┬─" . ('─' x $cutoff) . '─';
289 $borderstring_m .= "┼─" . ('─' x $cutoff) . '─';
290 $borderstring_b .= "┴─" . ('─' x $cutoff) . '─';
291 } else {
2ec79c0f 292 $formatstring .= "| %$alignstr${cutoff}s ";
eb1c51c2
DM
293 $borderstring_m .= "+-" . ('-' x $cutoff) . '-';
294 }
793ad69b
DM
295 }
296 } else {
297 # skip alignment and cutoff on last column
2ec79c0f 298 $formatstring .= ($i == ($column_count - 1)) ? "%s" : "%$alignstr${cutoff}s ";
f53ad23a
DM
299 }
300 }
301
eb1c51c2
DM
302 $borderstring_t = $borderstring_m if !length($borderstring_t);
303 $borderstring_b = $borderstring_m if !length($borderstring_b);
304
41d554d9
DM
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;
793ad69b 316
352b7a14
DM
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);
41d554d9
DM
326
327 for (my $i = 0; $i < $coldata->{height}; $i++) {
328
352b7a14 329 my $text = sprintf $formatstring, map {
41d554d9
DM
330 substr($coldata->{rowdata}->{$_}->{lines}->[$i] // '', 0, $colopts->{$_}->{cutoff});
331 } @$props_to_print;
332
333 $writeln->($text);
334 }
f53ad23a 335 }
41d554d9
DM
336
337 $writeln->($borderstring_b) if $border;
f53ad23a
DM
338}
339
c5adc5f9
DM
340sub 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
f53ad23a
DM
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.
363sub print_api_list {
b01a09e7 364 my ($data, $result_schema, $props_to_print, $options, $terminal_opts) = @_;
f53ad23a
DM
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
c5adc5f9
DM
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;
f53ad23a 379 }
f53ad23a 380 }
c5adc5f9 381 $props_to_print = [ sort keys %{$all_props} ];
f53ad23a
DM
382 }
383
c5adc5f9
DM
384 die "unable to detect list properties\n" if !scalar(@$props_to_print);
385
b01a09e7 386 print_text_table($data, $returnprops, $props_to_print, $options, $terminal_opts);
f53ad23a
DM
387}
388
b51b930d
DM
389my $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
f53ad23a 406sub print_api_result {
b01a09e7 407 my ($data, $result_schema, $props_to_print, $options, $terminal_opts) = @_;
0c22b00c 408
352b7a14
DM
409 return if $options->{quiet};
410
b01a09e7 411 $terminal_opts //= query_terminal_options({});
f53ad23a 412
4cbcd138 413 my $format = $options->{'output-format'} // 'text';
f0bcf4d4 414
90851cec 415 if ($result_schema) {
b51b930d
DM
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 }
f53ad23a 422
ac6c61bf
DM
423 if ($format eq 'yaml') {
424 print encode('UTF-8', CPAN::Meta::YAML::Dump($data));
425 } elsif ($format eq 'json') {
cd6591d3
DM
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') {
43b104c4 429 # Note: we always use utf8 encoding for json format
f53ad23a 430 print to_json($data, {utf8 => 1, allow_nonref => 1, canonical => 1, pretty => 1 });
cd6591d3 431 } elsif ($format eq 'text') {
43b104c4 432 my $encoding = $options->{encoding} // 'UTF-8';
f53ad23a
DM
433 my $type = $result_schema->{type};
434 if ($type eq 'object') {
c5adc5f9
DM
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);
2a174d2a 438 my $kvstore = [];
f53ad23a 439 foreach my $key (@$props_to_print) {
a05db8b5 440 next if !defined($data->{$key});
b01a09e7 441 push @$kvstore, { key => $key, value => data_to_text($data->{$key}, $result_schema->{properties}->{$key}, $options, $terminal_opts) };
f53ad23a 442 }
2a174d2a 443 my $schema = { type => 'array', items => { type => 'object' }};
b01a09e7 444 print_api_list($kvstore, $schema, ['key', 'value'], $options, $terminal_opts);
f53ad23a
DM
445 } elsif ($type eq 'array') {
446 return if !scalar(@$data);
447 my $item_type = $result_schema->{items}->{type};
448 if ($item_type eq 'object') {
b01a09e7 449 print_api_list($data, $result_schema, $props_to_print, $options, $terminal_opts);
f53ad23a 450 } else {
c850b998
DM
451 my $kvstore = [];
452 foreach my $value (@$data) {
453 push @$kvstore, { value => $value };
f53ad23a 454 }
c850b998 455 my $schema = { type => 'array', items => { type => 'object', properties => { value => $result_schema->{items} }}};
b01a09e7 456 print_api_list($kvstore, $schema, ['value'], { %$options, noheader => 1 }, $terminal_opts);
f53ad23a
DM
457 }
458 } else {
43b104c4 459 print encode($encoding, "$data\n");
f53ad23a
DM
460 }
461 } else {
462 die "internal error: unknown output format"; # should not happen
463 }
464}
465
6bc09493
DM
466sub 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
f53ad23a 4781;