Rewrite library

This commit is contained in:
Melon 2024-01-14 21:07:01 +00:00
parent a8cd12d55a
commit c6685100b2
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
2 changed files with 34 additions and 48 deletions

View File

@ -25,27 +25,23 @@ func main() {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
machine := qpty.New(client, containerId) proc, err := qpty.New(client, containerId, &pty.Winsize{Rows: 14, Cols: 11})
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
exec, err := machine.Exec(&pty.Winsize{Rows: 14, Cols: 11})
if err != nil {
return
}
go func() { go func() {
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
milliType(exec, []byte(cmd+"\n"), 500*time.Millisecond) milliType(proc, []byte(cmd+"\n"), 500*time.Millisecond)
}() }()
err = exec.Run(shell) err = proc.Run(shell)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
func milliType(exec *qpty.Exec, p []byte, gap time.Duration) { func milliType(exec *qpty.Qpty, p []byte, gap time.Duration) {
for _, i := range p { for _, i := range p {
exec.Send([]byte{i}) exec.Send([]byte{i})
time.Sleep(gap) time.Sleep(gap)

82
qpty.go
View File

@ -12,57 +12,45 @@ import (
type Qpty struct { type Qpty struct {
dock *docker.Client dock *docker.Client
container string container string
} pty, tty *os.File
func New(client *docker.Client, container string) *Qpty {
return &Qpty{
dock: client,
container: container,
}
}
func (q *Qpty) Exec(size *pty.Winsize) (*Exec, error) {
iPty, _, err := pty.Open()
if err != nil {
return nil, err
}
e := &Exec{
q: q,
pty: iPty,
}
if err := e.SetSize(size); err != nil {
return nil, err
}
e.ir, e.iw = io.Pipe()
e.or, e.ow = io.Pipe()
return e, nil
}
type Exec struct {
q *Qpty
pty *os.File
ir, or *io.PipeReader ir, or *io.PipeReader
iw, ow *io.PipeWriter iw, ow *io.PipeWriter
} }
func (e *Exec) Pty() *os.File { func New(client *docker.Client, container string, size *pty.Winsize) (*Qpty, error) {
return e.pty 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 (e *Exec) SetSize(winsize *pty.Winsize) error { func (q *Qpty) Pty() *os.File {
return pty.Setsize(e.pty, winsize) return q.pty
} }
func (e *Exec) Send(p []byte) (int, error) { func (q *Qpty) SetSize(winsize *pty.Winsize) error {
return e.iw.Write(p) return pty.Setsize(q.pty, winsize)
} }
func (e *Exec) Run(shell string) error { func (q *Qpty) Send(p []byte) (int, error) {
execInst, err := e.q.dock.CreateExec(docker.CreateExecOptions{ return q.iw.Write(p)
}
func (q *Qpty) Run(shell string) error {
execInst, err := q.dock.CreateExec(docker.CreateExecOptions{
Cmd: []string{shell}, Cmd: []string{shell},
Container: e.q.container, Container: q.container,
User: "root", User: "root",
WorkingDir: "/", WorkingDir: "/",
Context: context.Background(), Context: context.Background(),
@ -75,18 +63,20 @@ func (e *Exec) Run(shell string) error {
} }
go func() { go func() {
_, _ = io.Copy(e.pty, e.or) _, _ = io.Copy(q.tty, q.or)
}() }()
go func() { go func() {
r, w := io.Pipe() r, w := io.Pipe()
go io.Copy(hex.NewEncoder(w), e.pty) go func() {
io.Copy(os.Stdout, r) _, _ = io.Copy(hex.NewEncoder(w), q.pty)
}()
_, _ = io.Copy(os.Stdout, r)
}() }()
return e.q.dock.StartExec(execInst.ID, docker.StartExecOptions{ return q.dock.StartExec(execInst.ID, docker.StartExecOptions{
InputStream: e.ir, InputStream: q.ir,
OutputStream: e.ow, OutputStream: q.ow,
ErrorStream: nil, ErrorStream: nil,
Tty: true, Tty: true,
RawTerminal: true, RawTerminal: true,