diff options
author | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2017-05-22 23:58:31 +0200 |
---|---|---|
committer | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2017-05-25 00:17:51 +0200 |
commit | 66496d63e16635d72f15abe48dc3dadb473f0b6b (patch) | |
tree | f451ec49e07886168ac843fa564e55e97ade182c /lib/common/execPipeWs.go | |
parent | 7f1db509a2076311c280964715962df71a1631ce (diff) |
Rework development page: Pre-build, Build, Populate.
Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
Diffstat (limited to 'lib/common/execPipeWs.go')
-rw-r--r-- | lib/common/execPipeWs.go | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/lib/common/execPipeWs.go b/lib/common/execPipeWs.go index 3b63cdc..4994d9d 100644 --- a/lib/common/execPipeWs.go +++ b/lib/common/execPipeWs.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "os" + "strings" "time" "syscall" @@ -14,7 +15,7 @@ import ( ) // EmitOutputCB is the function callback used to emit data -type EmitOutputCB func(sid string, cmdID int, stdout, stderr string) +type EmitOutputCB func(sid string, cmdID int, stdout, stderr string, data *map[string]interface{}) // EmitExitCB is the function callback used to emit exit proc code type EmitExitCB func(sid string, cmdID int, code int, err error) @@ -23,8 +24,8 @@ type EmitExitCB func(sid string, cmdID int, code int, err error) // https://github.com/gorilla/websocket/blob/master/examples/command/main.go // ExecPipeWs executes a command and redirect stdout/stderr into a WebSocket -func ExecPipeWs(cmd string, so *socketio.Socket, sid string, cmdID int, - cmdExecTimeout int, log *logrus.Logger, eoCB EmitOutputCB, eeCB EmitExitCB) error { +func ExecPipeWs(cmd []string, env []string, so *socketio.Socket, sid string, cmdID int, + cmdExecTimeout int, log *logrus.Logger, eoCB EmitOutputCB, eeCB EmitExitCB, data *map[string]interface{}) error { outr, outw, err := os.Pipe() if err != nil { @@ -39,9 +40,10 @@ func ExecPipeWs(cmd string, so *socketio.Socket, sid string, cmdID int, return fmt.Errorf("Pipe stdin error: " + err.Error()) } - bashArgs := []string{"/bin/bash", "-c", cmd} + bashArgs := []string{"/bin/bash", "-c", strings.Join(cmd, " ")} proc, err := os.StartProcess("/bin/bash", bashArgs, &os.ProcAttr{ Files: []*os.File{inr, outw, outw}, + Env: append(os.Environ(), env...), }) if err != nil { outr.Close() @@ -58,7 +60,7 @@ func ExecPipeWs(cmd string, so *socketio.Socket, sid string, cmdID int, defer inw.Close() stdoutDone := make(chan struct{}) - go cmdPumpStdout(so, outr, stdoutDone, sid, cmdID, log, eoCB) + go cmdPumpStdout(so, outr, stdoutDone, sid, cmdID, log, eoCB, data) // Blocking function that poll input or wait for end of process cmdPumpStdin(so, inw, proc, sid, cmdID, cmdExecTimeout, log, eeCB) @@ -133,13 +135,13 @@ func cmdPumpStdin(so *socketio.Socket, w io.Writer, proc *os.Process, } func cmdPumpStdout(so *socketio.Socket, r io.Reader, done chan struct{}, - sid string, cmdID int, log *logrus.Logger, emitFuncCB EmitOutputCB) { + sid string, cmdID int, log *logrus.Logger, emitFuncCB EmitOutputCB, data *map[string]interface{}) { defer func() { }() sc := bufio.NewScanner(r) for sc.Scan() { - emitFuncCB(sid, cmdID, string(sc.Bytes()), "") + emitFuncCB(sid, cmdID, string(sc.Bytes()), "", data) } if sc.Err() != nil { log.Errorln("scan:", sc.Err()) |