-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqpty.go
76 lines (67 loc) · 1.42 KB
/
qpty.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
65
66
67
68
69
70
71
72
73
74
75
76
package qpty
import (
"context"
"github.com/creack/pty"
"github.com/fsouza/go-dockerclient"
"io"
"os"
)
type Qpty struct {
dock *docker.Client
container string
pty, tty *os.File
ir, or *io.PipeReader
iw, ow *io.PipeWriter
}
func New(client *docker.Client, container string, size *pty.Winsize) (*Qpty, error) {
iPty, iTty, err := pty.Open()
if err != nil {
return nil, err
}
q := &Qpty{
dock: client,
container: container,
pty: iPty, tty: iTty,
}
if err := q.SetSize(size); err != nil {
return nil, err
}
q.ir, q.iw = io.Pipe()
q.or, q.ow = io.Pipe()
return q, nil
}
func (q *Qpty) Pty() *os.File {
return q.pty
}
func (q *Qpty) SetSize(winsize *pty.Winsize) error {
return pty.Setsize(q.pty, winsize)
}
func (q *Qpty) Send(p []byte) (int, error) {
return q.iw.Write(p)
}
func (q *Qpty) Run(shell string) error {
execInst, err := q.dock.CreateExec(docker.CreateExecOptions{
Cmd: []string{shell},
Container: q.container,
User: "root",
WorkingDir: "/",
Context: context.Background(),
AttachStdin: true,
AttachStdout: true,
Tty: true,
})
if err != nil {
return err
}
go func() {
_, _ = io.Copy(q.tty, q.or)
}()
return q.dock.StartExec(execInst.ID, docker.StartExecOptions{
InputStream: q.ir,
OutputStream: q.ow,
ErrorStream: nil,
Tty: true,
RawTerminal: true,
Context: context.Background(),
})
}