Commit 9bc7baa2 authored by Addshore's avatar Addshore 🏄 Committed by Addshore
Browse files

dev: Resize terminal on exec commands

parent 8f6a7c67
......@@ -22,12 +22,14 @@ import (
"fmt"
"io"
"os"
gosignal "os/signal"
"runtime"
"time"
"gerrit.wikimedia.org/r/mediawiki/tools/cli/internal/exec"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/signal"
"golang.org/x/crypto/ssh/terminal"
)
......@@ -60,7 +62,7 @@ func (m MWDD) DockerExec(command DockerExecCommand) {
panic(err)
}
config := types.ExecConfig{
execConfig := types.ExecConfig{
AttachStderr: true,
AttachStdout: true,
AttachStdin: true,
......@@ -71,7 +73,7 @@ func (m MWDD) DockerExec(command DockerExecCommand) {
}
ctx := context.Background()
response, err := cli.ContainerExecCreate(ctx, containerID, config)
response, err := cli.ContainerExecCreate(ctx, containerID, execConfig)
if err != nil {
return
}
......@@ -92,6 +94,13 @@ func (m MWDD) DockerExec(command DockerExecCommand) {
return
}
if execConfig.Tty {
if err := monitorTtySize(ctx, cli, execID, true); err != nil {
fmt.Println("Error monitoring TTY size:")
fmt.Println(err)
}
}
// When TTY is ON, just copy stdout https://phabricator.wikimedia.org/T282340
// See: https://github.com/docker/cli/blob/70a00157f161b109be77cd4f30ce0662bfe8cc32/cli/command/container/hijack.go#L121-L130
go io.Copy(os.Stdout, waiter.Reader)
......@@ -119,3 +128,49 @@ func (m MWDD) DockerExec(command DockerExecCommand) {
}
}
}
// MonitorTtySize updates the container tty size when the terminal tty changes size
func monitorTtySize(ctx context.Context, client client.APIClient, id string, isExec bool) error {
// Source: https://github.com/skiffos/skiff-core/blob/82c430e4961453c250883c2e5ebd4bd360fa13a5/shell/tty.go
resizeTty := func() {
width, height, _ := terminal.GetSize(0)
resizeTtyTo(ctx, client, id, uint(height), uint(width), isExec)
}
resizeTty()
sigchan := make(chan os.Signal, 1)
gosignal.Notify(sigchan, signal.SIGWINCH)
go func() {
for range sigchan {
resizeTty()
}
}()
return nil
}
func resizeTtyTo(ctx context.Context, client client.ContainerAPIClient, id string, height, width uint, isExec bool) {
// Source: https://github.com/skiffos/skiff-core/blob/82c430e4961453c250883c2e5ebd4bd360fa13a5/shell/tty.go
if height == 0 && width == 0 {
return
}
options := types.ResizeOptions{
Height: height,
Width: width,
}
var err error
if isExec {
err = client.ContainerExecResize(ctx, id, options)
} else {
err = client.ContainerResize(ctx, id, options)
}
_ = err // Ignore this error for now.
/*
if err != nil {
}
*/
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment