-
Notifications
You must be signed in to change notification settings - Fork 467
/
exec_xx.go
60 lines (54 loc) · 1.02 KB
/
exec_xx.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
//go:build !windows
// +build !windows
package shell
import (
"errors"
"os"
"os/exec"
"github.com/creack/pty"
)
// Exec execute shell command
func (link *Link) Exec() error {
var cmd *exec.Cmd
if len(link.parent.cfg.Exec) > 0 {
cmd = exec.Command(link.parent.cfg.Exec)
}
if cmd == nil {
dir, err := exec.LookPath("bash")
if err == nil {
cmd = exec.Command(dir)
}
}
if cmd == nil {
dir, err := exec.LookPath("sh")
if err == nil {
cmd = exec.Command(dir)
}
}
if cmd == nil {
return errors.New("no shell command supported")
}
cmd.Env = append(os.Environ(), link.parent.cfg.Env...)
f, err := pty.Start(cmd)
if err != nil {
return err
}
go cmd.Wait() // defunct process
link.stdin = f
link.stdout = f
link.pid = cmd.Process.Pid
return nil
}
func (link *Link) onClose() {
if link.stdin != nil {
link.stdin.Close()
}
}
func (link *Link) resize(rows, cols uint32) {
if link.stdin != nil {
pty.Setsize(link.stdin.(*os.File), &pty.Winsize{
Rows: uint16(rows),
Cols: uint16(cols),
})
}
}