]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/CLIFormatter.pm
cli: remove all output formatter magic from CLIHandler
[pve-common.git] / src / PVE / CLIFormatter.pm
index 653a99f0129d8822d83d29803b1eb348452c2557..22cd8f2378c803bbc977646f1ff8468b62dc89fc 100644 (file)
@@ -364,7 +364,7 @@ sub print_api_list {
 }
 
 sub print_api_result {
-    my ($format, $data, $result_schema, $props_to_print, $options) = @_;
+    my ($data, $result_schema, $props_to_print, $options) = @_;
 
     return if $options->{quiet};
 
@@ -374,12 +374,17 @@ sub print_api_result {
        $options = { %$options }; # copy
     }
 
+    my $format = $options->{'output-format'} // 'text';
+
     return if $result_schema->{type} eq 'null';
 
     if ($format eq 'json') {
+       # Note: we always use utf8 encoding for json format
+       print to_json($data, {utf8 => 1, allow_nonref => 1, canonical => 1 }) . "\n";
+    } elsif ($format eq 'json-pretty') {
        # Note: we always use utf8 encoding for json format
        print to_json($data, {utf8 => 1, allow_nonref => 1, canonical => 1, pretty => 1 });
-    } elsif ($format eq 'text' || $format eq 'plain') {
+    } elsif ($format eq 'text') {
        my $encoding = $options->{encoding} // 'UTF-8';
        my $type = $result_schema->{type};
        if ($type eq 'object') {
@@ -388,6 +393,7 @@ sub print_api_result {
            $props_to_print = [ sort keys %$data ] if !scalar(@$props_to_print);
            my $kvstore = [];
            foreach my $key (@$props_to_print) {
+               next if !defined($data->{$key});
                push @$kvstore, { key => $key, value => data_to_text($data->{$key}, $result_schema->{properties}->{$key}, $options) };
            }
            my $schema = { type => 'array', items => { type => 'object' }};