]> git.proxmox.com Git - pve-client.git/blobdiff - PVE/APIClient/Helpers.pm
Tools.pm: copied lock_file_full from pve-common
[pve-client.git] / PVE / APIClient / Helpers.pm
index d8a7fba0e50ca1069919293b3d3727f51c786765..cc1951401d3247b5682a35857a164471f71cf8d0 100644 (file)
@@ -43,7 +43,7 @@ $build_pve_api_path_hash = sub {
     }
 };
 
-my $default_output_format = 'table';
+my $default_output_format = 'text';
 my $client_output_format =  $default_output_format;
 
 sub set_output_format {
@@ -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 'text') {
+       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)) {