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