[pve-devel] [PATCH pve-client 1/2] add new helper print_result

Dietmar Maurer dietmar at proxmox.com
Thu Jun 14 12:00:36 CEST 2018


Signed-off-by: Dietmar Maurer <dietmar at proxmox.com>
---
 PVE/APIClient/Helpers.pm | 34 ++++++++++++++++++++++++++++++++++
 pveclient                |  7 +------
 2 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/PVE/APIClient/Helpers.pm b/PVE/APIClient/Helpers.pm
index d8a7fba..7aec160 100644
--- a/PVE/APIClient/Helpers.pm
+++ b/PVE/APIClient/Helpers.pm
@@ -60,6 +60,40 @@ sub get_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)) {
diff --git a/pveclient b/pveclient
index 404090f..f78770b 100755
--- a/pveclient
+++ b/pveclient
@@ -98,12 +98,7 @@ $path_properties->{api_path} = {
 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 ({
-- 
2.11.0



More information about the pve-devel mailing list