[pve-devel] [PATCH manager] fix #4333: redirect API handler output to STDERR

Fabian Grünbichler f.gruenbichler at proxmox.com
Wed Mar 8 12:47:09 CET 2023


otherwise, print/warn statements by the API endpoint are included in the
output, which breaks JSON parsing in case of output-format == json*.

Signed-off-by: Fabian Grünbichler <f.gruenbichler at proxmox.com>
---

Notes:
    technically a change that might require adapting some scripted calls, although
    we don't give any guarantees about pvesh output (or task log contents/stdout
    output of endpoints) I think ;)
    
    Suggested-by: reitermarkus (Proxmox Community Forum user)

 PVE/CLI/pvesh.pm | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/PVE/CLI/pvesh.pm b/PVE/CLI/pvesh.pm
index 9acf292a..31dce26d 100755
--- a/PVE/CLI/pvesh.pm
+++ b/PVE/CLI/pvesh.pm
@@ -309,7 +309,13 @@ sub call_api_method {
 	    $param->{$p} = $uri_param->{$p};
 	}
 
-	$data = $handler->handle($info, $param);
+	do {
+	    # redirect stray output to STDERR
+	    # only the returned $data should go to STDOUT
+	    local *STDOUT;
+	    open(STDOUT, '>&', STDERR);
+	    $data = $handler->handle($info, $param);
+	};
     }
 
     return if $opt_nooutput || $stdopts->{quiet};
-- 
2.30.2






More information about the pve-devel mailing list