Skip to content

Commit

Permalink
feat: TTY mode detection
Browse files Browse the repository at this point in the history
  • Loading branch information
jpts committed Jun 22, 2023
1 parent 806d623 commit 6711e61
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
32 changes: 19 additions & 13 deletions cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
)

type Options struct {
Expand Down Expand Up @@ -53,6 +54,7 @@ type cliSession struct {
clientConf *rest.Config
k8sClient *kubernetes.Clientset
namespace string
RawMode bool
}

func NewCliSession(o *Options) (*cliSession, error) {
Expand Down Expand Up @@ -154,7 +156,12 @@ func (c *cliSession) prepExec() (*http.Request, error) {
}

if c.opts.TTY {
query.Add("tty", "true")
stdIn, _, _ := term.StdStreams()
_, c.RawMode = term.GetFdInfo(stdIn)
if !c.RawMode {
klog.V(2).Infof("Unable to use a TTY - input is not a terminal or the right kind of file")
}
query.Add("tty", fmt.Sprintf("%t", c.RawMode))
}

if c.opts.Stdin {
Expand Down Expand Up @@ -184,26 +191,25 @@ func (c *cliSession) doExec(req *http.Request) error {
Subprotocols: protocols,
}

initState := &TerminalState{}
if c.opts.TTY {
initState := &TerminalState{
IsRaw: c.RawMode,
}
if c.RawMode {
stdIn, stdOut, _ := term.StdStreams()
stdInFd, isTerm := term.GetFdInfo(stdIn)
stdInFd, _ := term.GetFdInfo(stdIn)
stdOutFd, _ := term.GetFdInfo(stdOut)
if isTerm {
initState.StdInFd = stdInFd
initState.StdOutFd = stdOutFd
initState.StateBlob, err = term.SetRawTerminal(stdInFd)
if err != nil {
return err
}
defer term.RestoreTerminal(stdInFd, initState.StateBlob)
initState.StdInFd = stdInFd
initState.StdOutFd = stdOutFd
initState.StateBlob, err = term.SetRawTerminal(stdInFd)
if err != nil {
return err
}
defer term.RestoreTerminal(stdInFd, initState.StateBlob)
}

rt := &WebsocketRoundTripper{
Dialer: dialer,
TermState: initState,
opts: c.opts,
}

rter, err := rest.HTTPWrappersForConfig(c.clientConf, rt)
Expand Down
1 change: 1 addition & 0 deletions cmd/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type TerminalState struct {
StdOutFd uintptr
StateBlob *term.State
Initialised bool
IsRaw bool
}

type TerminalSize struct {
Expand Down
3 changes: 1 addition & 2 deletions cmd/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
type WebsocketRoundTripper struct {
Dialer *websocket.Dialer
TermState *TerminalState
opts Options
SendBuffer bytes.Buffer
}

Expand Down Expand Up @@ -157,7 +156,7 @@ func (d *WebsocketRoundTripper) concurrentRecv(wg *sync.WaitGroup, ws *websocket

func (d *WebsocketRoundTripper) concurrentResize(wg *sync.WaitGroup, ws *websocket.Conn, errChan chan error) {
defer wg.Done()
if d.opts.TTY {
if d.TermState.IsRaw {
resizeNotify := registerResizeSignal()

d.TermState.Initialised = false
Expand Down

0 comments on commit 6711e61

Please sign in to comment.