Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: lib.go redirects stdout to Output #640

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions cmd/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ package main
import "C"

import (
"bytes"
"encoding/csv"
"io"
"os"
"rpc/pkg/utils"
"strings"

Expand All @@ -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
Expand All @@ -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)
}

Expand Down
Loading