Skip to content

Commit

Permalink
feat: lib.go redirects stdout to Output
Browse files Browse the repository at this point in the history
  • Loading branch information
sfmadrig committed Sep 17, 2024
1 parent a99ce90 commit fb74d78
Showing 1 changed file with 16 additions and 0 deletions.
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

0 comments on commit fb74d78

Please sign in to comment.