1 package PVE
::CLIFormatter
;
7 use POSIX
qw(strftime);
8 use YAML
::XS
; # supports Dumping JSON::PP::Boolean
9 $YAML::XS
::Boolean
= "JSON::PP";
18 sub render_timestamp
{
21 # ISO 8601 date format
22 return strftime
("%F %H:%M:%S", localtime($epoch));
25 PVE
::JSONSchema
::register_renderer
('timestamp', \
&render_timestamp
);
27 sub render_timestamp_gmt
{
30 # ISO 8601 date format, standard Greenwich time zone
31 return strftime
("%F %H:%M:%S", gmtime($epoch));
34 PVE
::JSONSchema
::register_renderer
('timestamp_gmt', \
&render_timestamp_gmt
);
37 my ($duration_in_seconds) = @_;
40 my $rest = $duration_in_seconds;
43 my ($unit, $unitlength) = @_;
45 if ((my $v = int($rest/$unitlength)) > 0) {
46 $text .= " " if length($text);
47 $text .= "${v}${unit}";
48 $rest -= $v * $unitlength;
52 $step->('w', 7*24*3600);
53 $step->('d', 24*3600);
61 PVE
::JSONSchema
::register_renderer
('duration', \
&render_duration
);
63 sub render_fraction_as_percentage
{
66 return sprintf("%.2f%%", $fraction*100);
69 PVE
::JSONSchema
::register_renderer
(
70 'fraction_as_percentage', \
&render_fraction_as_percentage
);
75 my @units = qw(B KiB MiB GiB TiB PiB);
79 $max_unit = int(log($value)/log(1024));
80 $value /= 1024**($max_unit);
82 my $unit = $units[$max_unit];
83 return sprintf "%.2f $unit", $value;
86 PVE
::JSONSchema
::register_renderer
('bytes', \
&render_bytes
);
91 my $data = YAML
::XS
::Dump
($value);
92 $data =~ s/^---[\n\s]//; # remove yaml marker
97 PVE
::JSONSchema
::register_renderer
('yaml', \
&render_yaml
);
99 sub query_terminal_options
{
105 ($options->{columns
}) = PVE
::PTY
::tcgetsize
(*STDOUT
);
108 $options->{encoding
} = I18N
::Langinfo
::langinfo
(I18N
::Langinfo
::CODESET
());
110 $options->{utf8
} = 1 if $options->{encoding
} eq 'UTF-8';
116 my ($data, $propdef, $options, $terminal_opts) = @_;
118 return '' if !defined($data);
120 $terminal_opts //= {};
122 my $human_readable = $options->{'human-readable'} // 1;
124 if ($human_readable && defined($propdef)) {
125 if (my $type = $propdef->{type
}) {
126 if ($type eq 'boolean') {
127 return $data ?
1 : 0;
130 if (!defined($data) && defined($propdef->{default})) {
131 return "($propdef->{default})";
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);
140 if (my $class = ref($data)) {
141 # JSON::PP::Boolean requires allow_nonref
142 return to_json
($data, { allow_nonref
=> 1, canonical
=> 1 });
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
153 # - sort_key: can be used to sort after a specific column, if it isn't set we sort
154 # after the leftmost column. This can be turned off by passing 0 as sort_key
155 # - noborder: print without asciiart border
156 # - noheader: print without table header
157 # - columns: limit output width (if > 0)
158 # - utf8: use utf8 characters for table delimiters
160 sub print_text_table
{
161 my ($data, $returnprops, $props_to_print, $options, $terminal_opts) = @_;
163 $terminal_opts //= query_terminal_options
({});
165 my $sort_key = $options->{sort_key
};
166 my $border = !$options->{noborder
};
167 my $header = !$options->{noheader
};
169 my $columns = $terminal_opts->{columns
};
170 my $utf8 = $terminal_opts->{utf8
};
171 my $encoding = $terminal_opts->{encoding
} // 'UTF-8';
173 $sort_key //= $props_to_print->[0];
175 if (defined($sort_key) && $sort_key ne 0) {
176 my $type = $returnprops->{$sort_key}->{type
} // 'string';
178 if ($type eq 'integer' || $type eq 'number') {
179 $cmpfn = sub { $_[0] <=> $_[1] };
181 $cmpfn = sub { $_[0] cmp $_[1] };
184 PVE
::Tools
::safe_compare
($a->{$sort_key}, $b->{$sort_key}, $cmpfn)
190 my $borderstring_m = '';
191 my $borderstring_b = '';
192 my $borderstring_t = '';
193 my $borderstring_h = '';
194 my $formatstring = '';
196 my $column_count = scalar(@$props_to_print);
200 foreach my $entry (@$data) {
205 for (my $i = 0; $i < $column_count; $i++) {
206 my $prop = $props_to_print->[$i];
207 my $propinfo = $returnprops->{$prop} // {};
209 my $text = data_to_text
($entry->{$prop}, $propinfo, $options, $terminal_opts);
210 my $lines = [ split(/\n/, $text) ];
211 my $linecount = scalar(@$lines);
212 $height = $linecount if $linecount > $height;
215 foreach my $line (@$lines) {
216 my $len = length($line);
217 $width = $len if $len > $width;
220 $width = ($width =~ m/^(\d+)$/) ?
int($1) : 0; # untaint int
222 $rowdata->{$prop} = {
234 for (my $i = 0; $i < $column_count; $i++) {
235 my $prop = $props_to_print->[$i];
236 my $propinfo = $returnprops->{$prop} // {};
237 my $type = $propinfo->{type
} // 'string';
238 my $alignstr = ($type eq 'integer' || $type eq 'number') ?
'' : '-';
240 my $title = $propinfo->{title
} // $prop;
241 my $cutoff = $propinfo->{print_width
} // $propinfo->{maxLength
};
243 # calculate maximal print width and cutoff
244 my $titlelen = length($title);
246 my $longest = $titlelen;
247 foreach my $coldata (@$tabledata) {
248 my $rowdata = $coldata->{rowdata
}->{$prop};
249 $longest = $rowdata->{width
} if $rowdata->{width
} > $longest;
251 $cutoff = $longest if !defined($cutoff) || $cutoff > $longest;
253 $colopts->{$prop} = {
259 if ($i == 0 && ($column_count == 1)) {
261 $formatstring .= "│ %$alignstr${cutoff}s │";
262 $borderstring_t .= "┌─" . ('─' x
$cutoff) . "─┐";
263 $borderstring_h .= "╞═" . ('═' x
$cutoff) . '═╡';
264 $borderstring_m .= "├─" . ('─' x
$cutoff) . "─┤";
265 $borderstring_b .= "└─" . ('─' x
$cutoff) . "─┘";
267 $formatstring .= "| %$alignstr${cutoff}s |";
268 $borderstring_m .= "+-" . ('-' x
$cutoff) . "-+";
269 $borderstring_h .= "+=" . ('=' x
$cutoff) . '=';
273 $formatstring .= "│ %$alignstr${cutoff}s ";
274 $borderstring_t .= "┌─" . ('─' x
$cutoff) . '─';
275 $borderstring_h .= "╞═" . ('═' x
$cutoff) . '═';
276 $borderstring_m .= "├─" . ('─' x
$cutoff) . '─';
277 $borderstring_b .= "└─" . ('─' x
$cutoff) . '─';
279 $formatstring .= "| %$alignstr${cutoff}s ";
280 $borderstring_m .= "+-" . ('-' x
$cutoff) . '-';
281 $borderstring_h .= "+=" . ('=' x
$cutoff) . '=';
283 } elsif ($i == ($column_count - 1)) {
285 $formatstring .= "│ %$alignstr${cutoff}s │";
286 $borderstring_t .= "┬─" . ('─' x
$cutoff) . "─┐";
287 $borderstring_h .= "╪═" . ('═' x
$cutoff) . '═╡';
288 $borderstring_m .= "┼─" . ('─' x
$cutoff) . "─┤";
289 $borderstring_b .= "┴─" . ('─' x
$cutoff) . "─┘";
291 $formatstring .= "| %$alignstr${cutoff}s |";
292 $borderstring_m .= "+-" . ('-' x
$cutoff) . "-+";
293 $borderstring_h .= "+=" . ('=' x
$cutoff) . "=+";
297 $formatstring .= "│ %$alignstr${cutoff}s ";
298 $borderstring_t .= "┬─" . ('─' x
$cutoff) . '─';
299 $borderstring_h .= "╪═" . ('═' x
$cutoff) . '═';
300 $borderstring_m .= "┼─" . ('─' x
$cutoff) . '─';
301 $borderstring_b .= "┴─" . ('─' x
$cutoff) . '─';
303 $formatstring .= "| %$alignstr${cutoff}s ";
304 $borderstring_m .= "+-" . ('-' x
$cutoff) . '-';
305 $borderstring_h .= "+=" . ('=' x
$cutoff) . '=';
309 # skip alignment and cutoff on last column
310 $formatstring .= ($i == ($column_count - 1)) ?
"%s" : "%$alignstr${cutoff}s ";
314 $borderstring_t = $borderstring_m if !length($borderstring_t);
315 $borderstring_b = $borderstring_m if !length($borderstring_b);
321 print encode
($encoding, substr($text, 0, $columns) . "\n");
323 print encode
($encoding, $text) . "\n";
327 $writeln->($borderstring_t) if $border;
329 my $borderstring_sep;
331 my $text = sprintf $formatstring, map { $colopts->{$_}->{title
} } @$props_to_print;
333 $borderstring_sep = $borderstring_h;
335 $borderstring_sep = $borderstring_m;
338 for (my $i = 0; $i < scalar(@$tabledata); $i++) {
339 my $coldata = $tabledata->[$i];
341 if ($border && ($i != 0 || $header)) {
342 $writeln->($borderstring_sep);
343 $borderstring_sep = $borderstring_m;
346 for (my $i = 0; $i < $coldata->{height
}; $i++) {
348 my $text = sprintf $formatstring, map {
349 substr($coldata->{rowdata
}->{$_}->{lines
}->[$i] // '', 0, $colopts->{$_}->{cutoff
});
356 $writeln->($borderstring_b) if $border;
359 sub extract_properties_to_print
{
365 foreach my $key (keys %$propdef) {
366 my $prop = $propdef->{$key};
367 if ($prop->{optional
}) {
368 push @$optional, $key;
370 push @$required, $key;
374 return [ sort(@$required), sort(@$optional) ];
377 # prints the result of an API GET call returning an array as a table.
378 # takes formatting information from the results property of the call
379 # if $props_to_print is provided, prints only those columns. otherwise
380 # takes all fields of the results property, with a fallback
381 # to all fields occurring in items of $data.
383 my ($data, $result_schema, $props_to_print, $options, $terminal_opts) = @_;
385 die "can only print object lists\n"
386 if !($result_schema->{type
} eq 'array' && $result_schema->{items
}->{type
} eq 'object');
388 my $returnprops = $result_schema->{items
}->{properties
};
390 $props_to_print = extract_properties_to_print
($returnprops)
391 if !defined($props_to_print);
393 if (!scalar(@$props_to_print)) {
395 foreach my $obj (@$data) {
396 foreach my $key (keys %$obj) {
397 $all_props->{$key} = 1;
400 $props_to_print = [ sort keys %{$all_props} ];
403 die "unable to detect list properties\n" if !scalar(@$props_to_print);
405 print_text_table
($data, $returnprops, $props_to_print, $options, $terminal_opts);
408 my $guess_type = sub {
411 return 'null' if !defined($data);
413 my $class = ref($data);
414 return 'string' if !$class;
416 if ($class eq 'HASH') {
418 } elsif ($class eq 'ARRAY') {
421 return 'string'; # better than nothing
425 sub print_api_result
{
426 my ($data, $result_schema, $props_to_print, $options, $terminal_opts) = @_;
428 return if $options->{quiet
};
430 $terminal_opts //= query_terminal_options
({});
432 my $format = $options->{'output-format'} // 'text';
434 if ($result_schema && defined($result_schema->{type
})) {
435 return if $result_schema->{type
} eq 'null';
436 return if $result_schema->{optional
} && !defined($data);
438 my $type = $guess_type->($data);
439 $result_schema = { type
=> $type };
440 $result_schema->{items
} = { type
=> $guess_type->($data->[0]) } if $type eq 'array';
443 if ($format eq 'yaml') {
444 print encode
('UTF-8', YAML
::XS
::Dump
($data));
445 } elsif ($format eq 'json') {
446 # Note: we always use utf8 encoding for json format
447 print to_json
($data, {utf8
=> 1, allow_nonref
=> 1, canonical
=> 1 }) . "\n";
448 } elsif ($format eq 'json-pretty') {
449 # Note: we always use utf8 encoding for json format
450 print to_json
($data, {utf8
=> 1, allow_nonref
=> 1, canonical
=> 1, pretty
=> 1 });
451 } elsif ($format eq 'text') {
452 my $encoding = $options->{encoding
} // 'UTF-8';
453 my $type = $result_schema->{type
};
454 if ($type eq 'object') {
455 $props_to_print = extract_properties_to_print
($result_schema->{properties
})
456 if !defined($props_to_print);
457 $props_to_print = [ sort keys %$data ] if !scalar(@$props_to_print);
459 foreach my $key (@$props_to_print) {
460 next if !defined($data->{$key});
461 push @$kvstore, { key
=> $key, value
=> data_to_text
($data->{$key}, $result_schema->{properties
}->{$key}, $options, $terminal_opts) };
463 my $schema = { type
=> 'array', items
=> { type
=> 'object' }};
464 print_api_list
($kvstore, $schema, ['key', 'value'], $options, $terminal_opts);
465 } elsif ($type eq 'array') {
466 return if !scalar(@$data);
467 my $item_type = $result_schema->{items
}->{type
};
468 if ($item_type eq 'object') {
469 print_api_list
($data, $result_schema, $props_to_print, $options, $terminal_opts);
472 foreach my $value (@$data) {
473 push @$kvstore, { value
=> $value };
475 my $schema = { type
=> 'array', items
=> { type
=> 'object', properties
=> { value
=> $result_schema->{items
} }}};
476 print_api_list
($kvstore, $schema, ['value'], { %$options, noheader
=> 1 }, $terminal_opts);
479 print encode
($encoding, "$data\n");
482 die "internal error: unknown output format"; # should not happen
486 sub print_api_result_plain
{
487 my ($data, $result_schema, $props_to_print, $options) = @_;
489 # avoid borders and header, ignore terminal width
490 $options = $options ?
{ %$options } : {}; # copy
492 $options->{noheader
} //= 1;
493 $options->{noborder
} //= 1;
495 print_api_result
($data, $result_schema, $props_to_print, $options, {});