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 {
log.Fatal(err)
}
machine := qpty.New(client, containerId)
proc, err := qpty.New(client, containerId, &pty.Winsize{Rows: 14, Cols: 11})
if err != nil {
log.Fatal(err)
}
exec, err := machine.Exec(&pty.Winsize{Rows: 14, Cols: 11})
if err != nil {
return
}
go func() {
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 {
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 {
exec.Send([]byte{i})
time.Sleep(gap)

82
qpty.go
View File

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