From fb74d78f072fcbddaa2876309bd9e5ea22a6ca32 Mon Sep 17 00:00:00 2001 From: Fernando Madrigal Date: Tue, 17 Sep 2024 14:58:15 -0600 Subject: [PATCH] feat: lib.go redirects stdout to Output --- cmd/lib.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/cmd/lib.go b/cmd/lib.go index 9925f31e..8cdea7db 100644 --- a/cmd/lib.go +++ b/cmd/lib.go @@ -10,7 +10,10 @@ package main import "C" import ( + "bytes" "encoding/csv" + "io" + "os" "rpc/pkg/utils" "strings" @@ -28,6 +31,11 @@ func rpcCheckAccess() int { //export rpcExec func rpcExec(Input *C.char, Output **C.char) int { + // Save the current stdout and redirect temporarly + oldStdout := os.Stdout + rd, w, _ := os.Pipe() + os.Stdout = w + if accessStatus := rpcCheckAccess(); accessStatus != int(utils.Success) { *Output = C.CString(AccessErrMsg) return accessStatus @@ -49,6 +57,14 @@ func rpcExec(Input *C.char, Output **C.char) int { *Output = C.CString("rpcExec failed: " + inputString) return handleError(err) } + + // Save captured output to Output variable and restore stdout + w.Close() + var buf bytes.Buffer + io.Copy(&buf, rd) + os.Stdout = oldStdout + *Output = C.CString(buf.String()) + return int(utils.Success) }