]> git.proxmox.com Git - pve-client.git/commitdiff
add new helper print_result
authorDietmar Maurer <dietmar@proxmox.com>
Thu, 14 Jun 2018 09:12:29 +0000 (11:12 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Thu, 14 Jun 2018 09:12:29 +0000 (11:12 +0200)
PVE/APIClient/Helpers.pm
pveclient

index d8a7fba0e50ca1069919293b3d3727f51c786765..7aec16082b30814c93056abbe971e03568216c70 100644 (file)
@@ -60,6 +60,40 @@ sub get_output_format {
     return $client_output_format;
 }
 
     return $client_output_format;
 }
 
+sub print_result {
+    my ($data, $result_schema) = @_;
+
+    my $format = get_output_format();
+
+    return if $result_schema->{type} eq 'null';
+
+    # TODO: implement different output formats ($format)
+
+    if ($format eq 'json') {
+       print to_json($data, {utf8 => 1, allow_nonref => 1, canonical => 1, pretty => 1 });
+    } elsif ($format eq 'table') {
+       my $type = $result_schema->{type};
+       if ($type eq 'object') {
+           die "implement me";
+       } elsif ($type eq 'array') {
+           my $item_type = $result_schema->{items}->{type};
+           if ($item_type eq 'object') {
+               die "implement me";
+           } elsif ($item_type eq 'array') {
+               die "implement me";
+           } else {
+               foreach my $el (@$data) {
+                   print "$el\n"
+               }
+           }
+       } else {
+           print "$data\n";
+       }
+    } else {
+       die "internal error: unknown output format"; # should not happen
+    }
+}
+
 sub get_api_definition {
 
     if (!defined($pve_api_definition)) {
 sub get_api_definition {
 
     if (!defined($pve_api_definition)) {
index 404090f6722e66fb94ba3e179de23a6dbd8770af..f78770b5d4ce5f50bdf60622e8992ef057356b18 100755 (executable)
--- a/pveclient
+++ b/pveclient
@@ -98,12 +98,7 @@ $path_properties->{api_path} = {
 my $format_result = sub {
     my ($data) = @_;
 
 my $format_result = sub {
     my ($data) = @_;
 
-    my $format = PVE::APIClient::Helpers::get_output_format();
-
-    return if $path_returns->{type} eq 'null';
-
-    # TODO: implement different output formats ($format)
-    print to_json($data, {utf8 => 1, allow_nonref => 1, canonical => 1, pretty => 1 });
+    PVE::APIClient::Helpers::print_result($data, $path_returns);
 };
 
 __PACKAGE__->register_method ({
 };
 
 __PACKAGE__->register_method ({