]> git.proxmox.com Git - pve-common.git/blame - src/PVE/CLIFormatter.pm
print_api_result_plain: new helper
[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
72 my @units = qw(B KiB MiB GiB TiB PiB);
73
74 my $max_unit = 0;
75 if ($value > 1023) {
76 $max_unit = int(log($value)/log(1024));
77 $value /= 1024**($max_unit);
78 }
79
80 return sprintf "%.2f $units[$max_unit]", $value;
81}
82
83PVE::JSONSchema::register_renderer('bytes', \&render_bytes);
84
ba752c80
DM
85sub render_yaml {
86 my ($value) = @_;
87
88 my $data = CPAN::Meta::YAML::Dump($value);
89 $data =~ s/^---[\n\s]//; # remove yaml marker
90
91 return $data;
92}
93
94PVE::JSONSchema::register_renderer('yaml', \&render_yaml);
95
4ac85000
DM
96sub query_terminal_options {
97 my ($options) = @_;
98
99 $options //= {};
100
101 if (-t STDOUT) {
102 ($options->{columns}) = PVE::PTY::tcgetsize(*STDOUT);
103 }
104
105 $options->{encoding} = I18N::Langinfo::langinfo(I18N::Langinfo::CODESET());
106
107 $options->{utf8} = 1 if $options->{encoding} eq 'UTF-8';
108
109 return $options;
110}
111
f53ad23a 112sub data_to_text {
b01a09e7 113 my ($data, $propdef, $options, $terminal_opts) = @_;
f53ad23a 114
3cd6f2f3
DM
115 return '' if !defined($data);
116
b01a09e7
DM
117 $terminal_opts //= {};
118
352b7a14
DM
119 my $human_readable = $options->{'human-readable'} // 1;
120
121 if ($human_readable && defined($propdef)) {
84142f1d
DM
122 if (my $type = $propdef->{type}) {
123 if ($type eq 'boolean') {
124 return $data ? 1 : 0;
125 }
126 }
127 if (!defined($data) && defined($propdef->{default})) {
128 return "($propdef->{default})";
129 }
130 if (defined(my $renderer = $propdef->{renderer})) {
131 my $code = PVE::JSONSchema::get_renderer($renderer);
132 die "internal error: unknown renderer '$renderer'" if !$code;
b01a09e7 133 return $code->($data, $options, $terminal_opts);
84142f1d
DM
134 }
135 }
f53ad23a
DM
136
137 if (my $class = ref($data)) {
84142f1d 138 return to_json($data, { canonical => 1 });
f53ad23a
DM
139 } else {
140 return "$data";
141 }
142}
143
144# prints a formatted table with a title row.
145# $data - the data to print (array of objects)
146# $returnprops -json schema property description
147# $props_to_print - ordered list of properties to print
0c22b00c 148# $options
2e2a4502 149# - sort_key: can be used to sort after a specific column, if it isn't set we sort
f53ad23a 150# after the leftmost column (with no undef value in $data) this can be
0c22b00c 151# turned off by passing 0 as sort_key
352b7a14
DM
152# - noborder: print without asciiart border
153# - noheader: print without table header
0c22b00c
DM
154# - columns: limit output width (if > 0)
155# - utf8: use utf8 characters for table delimiters
156
f53ad23a 157sub print_text_table {
b01a09e7
DM
158 my ($data, $returnprops, $props_to_print, $options, $terminal_opts) = @_;
159
160 $terminal_opts //= query_terminal_options({});
0c22b00c
DM
161
162 my $sort_key = $options->{sort_key};
352b7a14
DM
163 my $border = !$options->{noborder};
164 my $header = !$options->{noheader};
b01a09e7
DM
165
166 my $columns = $terminal_opts->{columns};
167 my $utf8 = $terminal_opts->{utf8};
168 my $encoding = $terminal_opts->{encoding} // 'UTF-8';
f53ad23a 169
2e2a4502 170 $sort_key //= $props_to_print->[0];
41d554d9 171
2e2a4502 172 if (defined($sort_key) && $sort_key ne 0) {
41d554d9
DM
173 my $type = $returnprops->{$sort_key}->{type} // 'string';
174 if ($type eq 'integer' || $type eq 'number') {
175 @$data = sort { $a->{$sort_key} <=> $b->{$sort_key} } @$data;
176 } else {
177 @$data = sort { $a->{$sort_key} cmp $b->{$sort_key} } @$data;
178 }
f53ad23a
DM
179 }
180
181 my $colopts = {};
793ad69b 182
eb1c51c2
DM
183 my $borderstring_m = '';
184 my $borderstring_b = '';
185 my $borderstring_t = '';
f53ad23a
DM
186 my $formatstring = '';
187
188 my $column_count = scalar(@$props_to_print);
189
41d554d9
DM
190 my $tabledata = [];
191
192 foreach my $entry (@$data) {
193
194 my $height = 1;
195 my $rowdata = {};
196
197 for (my $i = 0; $i < $column_count; $i++) {
198 my $prop = $props_to_print->[$i];
199 my $propinfo = $returnprops->{$prop} // {};
200
b01a09e7 201 my $text = data_to_text($entry->{$prop}, $propinfo, $options, $terminal_opts);
41d554d9
DM
202 my $lines = [ split(/\n/, $text) ];
203 my $linecount = scalar(@$lines);
204 $height = $linecount if $linecount > $height;
205
206 my $width = 0;
207 foreach my $line (@$lines) {
208 my $len = length($line);
209 $width = $len if $len > $width;
210 }
211
b9474c96
DM
212 $width = ($width =~ m/^(\d+)$/) ? int($1) : 0; # untaint int
213
41d554d9
DM
214 $rowdata->{$prop} = {
215 lines => $lines,
216 width => $width,
217 };
218 }
219
220 push @$tabledata, {
221 height => $height,
222 rowdata => $rowdata,
223 };
224 }
225
f53ad23a
DM
226 for (my $i = 0; $i < $column_count; $i++) {
227 my $prop = $props_to_print->[$i];
228 my $propinfo = $returnprops->{$prop} // {};
2ec79c0f
DM
229 my $type = $propinfo->{type} // 'string';
230 my $alignstr = ($type eq 'integer' || $type eq 'number') ? '' : '-';
f53ad23a
DM
231
232 my $title = $propinfo->{title} // $prop;
233 my $cutoff = $propinfo->{print_width} // $propinfo->{maxLength};
234
235 # calculate maximal print width and cutoff
236 my $titlelen = length($title);
237
238 my $longest = $titlelen;
41d554d9
DM
239 foreach my $coldata (@$tabledata) {
240 my $rowdata = $coldata->{rowdata}->{$prop};
241 $longest = $rowdata->{width} if $rowdata->{width} > $longest;
f53ad23a
DM
242 }
243 $cutoff = $longest if !defined($cutoff) || $cutoff > $longest;
f53ad23a
DM
244
245 $colopts->{$prop} = {
246 title => $title,
f53ad23a
DM
247 cutoff => $cutoff,
248 };
249
793ad69b 250 if ($border) {
eb1c51c2
DM
251 if ($i == 0 && ($column_count == 1)) {
252 if ($utf8) {
2ec79c0f 253 $formatstring .= "│ %$alignstr${cutoff}s │";
41d554d9
DM
254 $borderstring_t .= "┌─" . ('─' x $cutoff) . "─┐";
255 $borderstring_m .= "├─" . ('─' x $cutoff) . "─┤";
256 $borderstring_b .= "└─" . ('─' x $cutoff) . "─┘";
eb1c51c2 257 } else {
2ec79c0f 258 $formatstring .= "| %$alignstr${cutoff}s |";
41d554d9 259 $borderstring_m .= "+-" . ('-' x $cutoff) . "-+";
eb1c51c2
DM
260 }
261 } elsif ($i == 0) {
262 if ($utf8) {
2ec79c0f 263 $formatstring .= "│ %$alignstr${cutoff}s ";
eb1c51c2
DM
264 $borderstring_t .= "┌─" . ('─' x $cutoff) . '─';
265 $borderstring_m .= "├─" . ('─' x $cutoff) . '─';
266 $borderstring_b .= "└─" . ('─' x $cutoff) . '─';
267 } else {
2ec79c0f 268 $formatstring .= "| %$alignstr${cutoff}s ";
eb1c51c2
DM
269 $borderstring_m .= "+-" . ('-' x $cutoff) . '-';
270 }
271 } elsif ($i == ($column_count - 1)) {
272 if ($utf8) {
2ec79c0f 273 $formatstring .= "│ %$alignstr${cutoff}s │";
41d554d9
DM
274 $borderstring_t .= "┬─" . ('─' x $cutoff) . "─┐";
275 $borderstring_m .= "┼─" . ('─' x $cutoff) . "─┤";
276 $borderstring_b .= "┴─" . ('─' x $cutoff) . "─┘";
eb1c51c2 277 } else {
2ec79c0f 278 $formatstring .= "| %$alignstr${cutoff}s |";
41d554d9 279 $borderstring_m .= "+-" . ('-' x $cutoff) . "-+";
eb1c51c2 280 }
793ad69b 281 } else {
eb1c51c2 282 if ($utf8) {
2ec79c0f 283 $formatstring .= "│ %$alignstr${cutoff}s ";
eb1c51c2
DM
284 $borderstring_t .= "┬─" . ('─' x $cutoff) . '─';
285 $borderstring_m .= "┼─" . ('─' x $cutoff) . '─';
286 $borderstring_b .= "┴─" . ('─' x $cutoff) . '─';
287 } else {
2ec79c0f 288 $formatstring .= "| %$alignstr${cutoff}s ";
eb1c51c2
DM
289 $borderstring_m .= "+-" . ('-' x $cutoff) . '-';
290 }
793ad69b
DM
291 }
292 } else {
293 # skip alignment and cutoff on last column
2ec79c0f 294 $formatstring .= ($i == ($column_count - 1)) ? "%s" : "%$alignstr${cutoff}s ";
f53ad23a
DM
295 }
296 }
297
eb1c51c2
DM
298 $borderstring_t = $borderstring_m if !length($borderstring_t);
299 $borderstring_b = $borderstring_m if !length($borderstring_b);
300
41d554d9
DM
301 my $writeln = sub {
302 my ($text) = @_;
303
304 if ($columns) {
305 print encode($encoding, substr($text, 0, $columns) . "\n");
306 } else {
307 print encode($encoding, $text) . "\n";
308 }
309 };
310
311 $writeln->($borderstring_t) if $border;
793ad69b 312
352b7a14
DM
313 if ($header) {
314 my $text = sprintf $formatstring, map { $colopts->{$_}->{title} } @$props_to_print;
315 $writeln->($text);
316 }
317
318 for (my $i = 0; $i < scalar(@$tabledata); $i++) {
319 my $coldata = $tabledata->[$i];
320
321 $writeln->($borderstring_m) if $border && ($i != 0 || $header);
41d554d9
DM
322
323 for (my $i = 0; $i < $coldata->{height}; $i++) {
324
352b7a14 325 my $text = sprintf $formatstring, map {
41d554d9
DM
326 substr($coldata->{rowdata}->{$_}->{lines}->[$i] // '', 0, $colopts->{$_}->{cutoff});
327 } @$props_to_print;
328
329 $writeln->($text);
330 }
f53ad23a 331 }
41d554d9
DM
332
333 $writeln->($borderstring_b) if $border;
f53ad23a
DM
334}
335
c5adc5f9
DM
336sub extract_properties_to_print {
337 my ($propdef) = @_;
338
339 my $required = [];
340 my $optional = [];
341
342 foreach my $key (keys %$propdef) {
343 my $prop = $propdef->{$key};
344 if ($prop->{optional}) {
345 push @$optional, $key;
346 } else {
347 push @$required, $key;
348 }
349 }
350
351 return [ sort(@$required), sort(@$optional) ];
352}
353
f53ad23a
DM
354# prints the result of an API GET call returning an array as a table.
355# takes formatting information from the results property of the call
356# if $props_to_print is provided, prints only those columns. otherwise
357# takes all fields of the results property, with a fallback
358# to all fields occuring in items of $data.
359sub print_api_list {
b01a09e7 360 my ($data, $result_schema, $props_to_print, $options, $terminal_opts) = @_;
f53ad23a
DM
361
362 die "can only print object lists\n"
363 if !($result_schema->{type} eq 'array' && $result_schema->{items}->{type} eq 'object');
364
365 my $returnprops = $result_schema->{items}->{properties};
366
c5adc5f9
DM
367 $props_to_print = extract_properties_to_print($returnprops)
368 if !defined($props_to_print);
369
370 if (!scalar(@$props_to_print)) {
371 my $all_props = {};
372 foreach my $obj (@$data) {
373 foreach my $key (keys %$obj) {
374 $all_props->{$key} = 1;
f53ad23a 375 }
f53ad23a 376 }
c5adc5f9 377 $props_to_print = [ sort keys %{$all_props} ];
f53ad23a
DM
378 }
379
c5adc5f9
DM
380 die "unable to detect list properties\n" if !scalar(@$props_to_print);
381
b01a09e7 382 print_text_table($data, $returnprops, $props_to_print, $options, $terminal_opts);
f53ad23a
DM
383}
384
385sub print_api_result {
b01a09e7 386 my ($data, $result_schema, $props_to_print, $options, $terminal_opts) = @_;
0c22b00c 387
352b7a14
DM
388 return if $options->{quiet};
389
b01a09e7 390 $terminal_opts //= query_terminal_options({});
f53ad23a 391
4cbcd138 392 my $format = $options->{'output-format'} // 'text';
f0bcf4d4 393
f53ad23a
DM
394 return if $result_schema->{type} eq 'null';
395
ac6c61bf
DM
396 if ($format eq 'yaml') {
397 print encode('UTF-8', CPAN::Meta::YAML::Dump($data));
398 } elsif ($format eq 'json') {
cd6591d3
DM
399 # Note: we always use utf8 encoding for json format
400 print to_json($data, {utf8 => 1, allow_nonref => 1, canonical => 1 }) . "\n";
401 } elsif ($format eq 'json-pretty') {
43b104c4 402 # Note: we always use utf8 encoding for json format
f53ad23a 403 print to_json($data, {utf8 => 1, allow_nonref => 1, canonical => 1, pretty => 1 });
cd6591d3 404 } elsif ($format eq 'text') {
43b104c4 405 my $encoding = $options->{encoding} // 'UTF-8';
f53ad23a
DM
406 my $type = $result_schema->{type};
407 if ($type eq 'object') {
c5adc5f9
DM
408 $props_to_print = extract_properties_to_print($result_schema->{properties})
409 if !defined($props_to_print);
410 $props_to_print = [ sort keys %$data ] if !scalar(@$props_to_print);
2a174d2a 411 my $kvstore = [];
f53ad23a 412 foreach my $key (@$props_to_print) {
a05db8b5 413 next if !defined($data->{$key});
b01a09e7 414 push @$kvstore, { key => $key, value => data_to_text($data->{$key}, $result_schema->{properties}->{$key}, $options, $terminal_opts) };
f53ad23a 415 }
2a174d2a 416 my $schema = { type => 'array', items => { type => 'object' }};
b01a09e7 417 print_api_list($kvstore, $schema, ['key', 'value'], $options, $terminal_opts);
f53ad23a
DM
418 } elsif ($type eq 'array') {
419 return if !scalar(@$data);
420 my $item_type = $result_schema->{items}->{type};
421 if ($item_type eq 'object') {
b01a09e7 422 print_api_list($data, $result_schema, $props_to_print, $options, $terminal_opts);
f53ad23a 423 } else {
c850b998
DM
424 my $kvstore = [];
425 foreach my $value (@$data) {
426 push @$kvstore, { value => $value };
f53ad23a 427 }
c850b998 428 my $schema = { type => 'array', items => { type => 'object', properties => { value => $result_schema->{items} }}};
b01a09e7 429 print_api_list($kvstore, $schema, ['value'], { %$options, noheader => 1 }, $terminal_opts);
f53ad23a
DM
430 }
431 } else {
43b104c4 432 print encode($encoding, "$data\n");
f53ad23a
DM
433 }
434 } else {
435 die "internal error: unknown output format"; # should not happen
436 }
437}
438
6bc09493
DM
439sub print_api_result_plain {
440 my ($data, $result_schema, $props_to_print, $options) = @_;
441
442 # avoid borders and header, ignore terminal width
443 $options = $options ? { %$options } : {}; # copy
444
445 $options->{noheader} //= 1;
446 $options->{noborder} //= 1;
447
448 print_api_result($data, $result_schema, $props_to_print, $options, {});
449}
450
f53ad23a 4511;