mirror of
https://git.proxmox.com/git/pve-client
synced 2025-10-05 06:52:26 +00:00
update files from pve-common
This commit is contained in:
parent
633508aa89
commit
bb27fea14c
@ -3,6 +3,7 @@ package PVE::APIClient::CLIFormatter;
|
|||||||
use strict;
|
use strict;
|
||||||
use warnings;
|
use warnings;
|
||||||
use I18N::Langinfo;
|
use I18N::Langinfo;
|
||||||
|
use POSIX qw(strftime);
|
||||||
|
|
||||||
use PVE::APIClient::JSONSchema;
|
use PVE::APIClient::JSONSchema;
|
||||||
use PVE::APIClient::PTY;
|
use PVE::APIClient::PTY;
|
||||||
@ -10,6 +11,24 @@ use JSON;
|
|||||||
use utf8;
|
use utf8;
|
||||||
use Encode;
|
use Encode;
|
||||||
|
|
||||||
|
sub render_timestamp {
|
||||||
|
my ($epoch) = @_;
|
||||||
|
|
||||||
|
# ISO 8601 date format
|
||||||
|
return strftime("%F %H:%M:%S", localtime($epoch));
|
||||||
|
}
|
||||||
|
|
||||||
|
PVE::APIClient::JSONSchema::register_renderer('timestamp', \&render_timestamp);
|
||||||
|
|
||||||
|
sub render_timestamp_gmt {
|
||||||
|
my ($epoch) = @_;
|
||||||
|
|
||||||
|
# ISO 8601 date format, standard Greenwich time zone
|
||||||
|
return strftime("%F %H:%M:%S", gmtime($epoch));
|
||||||
|
}
|
||||||
|
|
||||||
|
PVE::APIClient::JSONSchema::register_renderer('timestamp_gmt', \&render_timestamp_gmt);
|
||||||
|
|
||||||
sub render_duration {
|
sub render_duration {
|
||||||
my ($duration_in_seconds) = @_;
|
my ($duration_in_seconds) = @_;
|
||||||
|
|
||||||
@ -79,7 +98,7 @@ sub query_terminal_options {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sub data_to_text {
|
sub data_to_text {
|
||||||
my ($data, $propdef) = @_;
|
my ($data, $propdef, $options) = @_;
|
||||||
|
|
||||||
return '' if !defined($data);
|
return '' if !defined($data);
|
||||||
|
|
||||||
@ -95,7 +114,7 @@ sub data_to_text {
|
|||||||
if (defined(my $renderer = $propdef->{renderer})) {
|
if (defined(my $renderer = $propdef->{renderer})) {
|
||||||
my $code = PVE::APIClient::JSONSchema::get_renderer($renderer);
|
my $code = PVE::APIClient::JSONSchema::get_renderer($renderer);
|
||||||
die "internal error: unknown renderer '$renderer'" if !$code;
|
die "internal error: unknown renderer '$renderer'" if !$code;
|
||||||
return $code->($data);
|
return $code->($data, $options);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,7 +179,7 @@ sub print_text_table {
|
|||||||
my $prop = $props_to_print->[$i];
|
my $prop = $props_to_print->[$i];
|
||||||
my $propinfo = $returnprops->{$prop} // {};
|
my $propinfo = $returnprops->{$prop} // {};
|
||||||
|
|
||||||
my $text = data_to_text($entry->{$prop}, $propinfo);
|
my $text = data_to_text($entry->{$prop}, $propinfo, $options);
|
||||||
my $lines = [ split(/\n/, $text) ];
|
my $lines = [ split(/\n/, $text) ];
|
||||||
my $linecount = scalar(@$lines);
|
my $linecount = scalar(@$lines);
|
||||||
$height = $linecount if $linecount > $height;
|
$height = $linecount if $linecount > $height;
|
||||||
@ -358,21 +377,24 @@ sub print_api_result {
|
|||||||
$props_to_print = [ sort keys %$data ] if !scalar(@$props_to_print);
|
$props_to_print = [ sort keys %$data ] if !scalar(@$props_to_print);
|
||||||
my $kvstore = [];
|
my $kvstore = [];
|
||||||
foreach my $key (@$props_to_print) {
|
foreach my $key (@$props_to_print) {
|
||||||
push @$kvstore, { key => $key, value => data_to_text($data->{$key}, $result_schema->{properties}->{$key}) };
|
push @$kvstore, { key => $key, value => data_to_text($data->{$key}, $result_schema->{properties}->{$key}, $options) };
|
||||||
}
|
}
|
||||||
my $schema = { type => 'array', items => { type => 'object' }};
|
my $schema = { type => 'array', items => { type => 'object' }};
|
||||||
$options->{border} = $format eq 'text';
|
$options->{border} = $format eq 'text';
|
||||||
print_api_list($kvstore, $schema, ['key', 'value'], $options);
|
print_api_list($kvstore, $schema, ['key', 'value'], $options);
|
||||||
} elsif ($type eq 'array') {
|
} elsif ($type eq 'array') {
|
||||||
return if !scalar(@$data);
|
return if !scalar(@$data);
|
||||||
|
$options->{border} = $format eq 'text';
|
||||||
my $item_type = $result_schema->{items}->{type};
|
my $item_type = $result_schema->{items}->{type};
|
||||||
if ($item_type eq 'object') {
|
if ($item_type eq 'object') {
|
||||||
$options->{border} = $format eq 'text';
|
|
||||||
print_api_list($data, $result_schema, $props_to_print, $options);
|
print_api_list($data, $result_schema, $props_to_print, $options);
|
||||||
} else {
|
} else {
|
||||||
foreach my $entry (@$data) {
|
my $kvstore = [];
|
||||||
print encode($encoding, data_to_text($entry) . "\n");
|
foreach my $value (@$data) {
|
||||||
|
push @$kvstore, { value => $value };
|
||||||
}
|
}
|
||||||
|
my $schema = { type => 'array', items => { type => 'object', properties => { value => $result_schema->{items} }}};
|
||||||
|
print_api_list($kvstore, $schema, ['value'], $options);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
print encode($encoding, "$data\n");
|
print encode($encoding, "$data\n");
|
||||||
|
@ -616,7 +616,8 @@ sub run_cli_handler {
|
|||||||
|
|
||||||
$exename = &$get_exe_name($class);
|
$exename = &$get_exe_name($class);
|
||||||
|
|
||||||
initlog($exename);
|
my $logid = $ENV{PVE_LOG_ID} || $exename;
|
||||||
|
initlog($logid);
|
||||||
|
|
||||||
no strict 'refs';
|
no strict 'refs';
|
||||||
$cmddef = ${"${class}::cmddef"};
|
$cmddef = ${"${class}::cmddef"};
|
||||||
|
Loading…
Reference in New Issue
Block a user