-
Notifications
You must be signed in to change notification settings - Fork 14
/
m_sh.go
64 lines (55 loc) · 1.01 KB
/
m_sh.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"bytes"
"os/exec"
"strings"
"time"
)
type mShCmd struct {
Name string
Args []string
Input string
And *mShCmd
Or *mShCmd
}
type mSh struct {
Env map[string]string
Cmd mShCmd
Cid string
Cwd string
}
// todo: send the client output as it comes
// handle And, Or
func (m *mSh) Call() (interface{}, string) {
env := envSlice(m.Env)
if m.Cid == "" {
m.Cid = "sh.auto." + numbers.nextString()
} else {
killCmd(m.Cid)
}
start := time.Now()
stdErr := bytes.NewBuffer(nil)
stdOut := bytes.NewBuffer(nil)
c := exec.Command(m.Cmd.Name, m.Cmd.Args...)
c.Stdout = stdOut
c.Stderr = stdErr
if m.Cmd.Input != "" {
c.Stdin = strings.NewReader(m.Cmd.Input)
}
c.Dir = m.Cwd
c.Env = env
watchCmd(m.Cid, c)
err := c.Run()
unwatchCmd(m.Cid)
res := M{
"out": jData(stdOut.Bytes()),
"err": jData(stdErr.Bytes()),
"dur": time.Now().Sub(start).String(),
}
return res, errStr(err)
}
func init() {
registry.Register("sh", func(b *Broker) Caller {
return &mSh{}
})
}