Skip to content

Commit

Permalink
promptui directly to tty
Browse files Browse the repository at this point in the history
  • Loading branch information
gabeduke committed Jan 18, 2025
1 parent 3a42525 commit 4422be2
Showing 1 changed file with 49 additions and 5 deletions.
54 changes: 49 additions & 5 deletions pkg/iexec/iexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,17 @@ func selectPod(pods []corev1.Pod, config Config) (corev1.Pod, error) {
return pods[0], nil
}

templates := podTemplate
// Open the terminal (tty) explicitly for rendering the menu
tty, err := os.OpenFile("/dev/tty", os.O_RDWR, 0)
if err != nil {
return corev1.Pod{}, errors.Wrap(err, "failed to open /dev/tty for prompt")

Check warning on line 66 in pkg/iexec/iexec.go

View check run for this annotation

Codecov / codecov/patch

pkg/iexec/iexec.go#L64-L66

Added lines #L64 - L66 were not covered by tests
}
defer func() {
log.Trace("Closing TTY...")
tty.Close()
}()

Check warning on line 71 in pkg/iexec/iexec.go

View check run for this annotation

Codecov / codecov/patch

pkg/iexec/iexec.go#L68-L71

Added lines #L68 - L71 were not covered by tests

templates := podTemplate

Check warning on line 73 in pkg/iexec/iexec.go

View check run for this annotation

Codecov / codecov/patch

pkg/iexec/iexec.go#L73

Added line #L73 was not covered by tests
if config.Naked {
templates = podTemplateNaked
}
Expand All @@ -73,9 +82,11 @@ func selectPod(pods []corev1.Pod, config Config) (corev1.Pod, error) {
IsVimMode: config.VimMode,
}

// Run the prompt
i, _, err := podsPrompt.Run()

if err != nil {
return pods[i], errors.Wrap(err, "unable to run prompt")
return corev1.Pod{}, errors.Wrap(err, "unable to run prompt")

Check warning on line 89 in pkg/iexec/iexec.go

View check run for this annotation

Codecov / codecov/patch

pkg/iexec/iexec.go#L89

Added line #L89 was not covered by tests
}

return pods[i], nil
Expand All @@ -86,8 +97,17 @@ func containerPrompt(containers []corev1.Container, config Config) (corev1.Conta
return containers[0], nil
}

templates := containerTemplates
// Open the terminal (tty) explicitly for rendering the menu
tty, err := os.OpenFile("/dev/tty", os.O_RDWR, 0)
if err != nil {
return corev1.Container{}, errors.Wrap(err, "failed to open /dev/tty for prompt")

Check warning on line 103 in pkg/iexec/iexec.go

View check run for this annotation

Codecov / codecov/patch

pkg/iexec/iexec.go#L101-L103

Added lines #L101 - L103 were not covered by tests
}
defer func() {
log.Trace("Closing TTY...")
tty.Close()
}()

Check warning on line 108 in pkg/iexec/iexec.go

View check run for this annotation

Codecov / codecov/patch

pkg/iexec/iexec.go#L105-L108

Added lines #L105 - L108 were not covered by tests

templates := containerTemplates

Check warning on line 110 in pkg/iexec/iexec.go

View check run for this annotation

Codecov / codecov/patch

pkg/iexec/iexec.go#L110

Added line #L110 was not covered by tests
if config.Naked {
templates = containerTemplatesNaked
}
Expand All @@ -97,17 +117,35 @@ func containerPrompt(containers []corev1.Container, config Config) (corev1.Conta
Items: containers,
Templates: templates,
IsVimMode: config.VimMode,
Stdout: tty, // Render menu to the PTY

Check warning on line 120 in pkg/iexec/iexec.go

View check run for this annotation

Codecov / codecov/patch

pkg/iexec/iexec.go#L120

Added line #L120 was not covered by tests
}

i, _, err := containersPrompt.Run()
if err != nil {
return containers[i], errors.Wrap(err, "unable to get prompt")
return corev1.Container{}, errors.Wrap(err, "unable to get prompt")

Check warning on line 125 in pkg/iexec/iexec.go

View check run for this annotation

Codecov / codecov/patch

pkg/iexec/iexec.go#L125

Added line #L125 was not covered by tests
}

log.Trace("hello")

Check warning on line 128 in pkg/iexec/iexec.go

View check run for this annotation

Codecov / codecov/patch

pkg/iexec/iexec.go#L128

Added line #L128 was not covered by tests
return containers[i], nil
}

func testTTY() error {
tty, err := os.OpenFile("/dev/tty", os.O_RDWR, 0)
if err != nil {
return errors.Wrap(err, "failed to open /dev/tty")

Check warning on line 135 in pkg/iexec/iexec.go

View check run for this annotation

Codecov / codecov/patch

pkg/iexec/iexec.go#L132-L135

Added lines #L132 - L135 were not covered by tests
}
defer tty.Close()

Check warning on line 137 in pkg/iexec/iexec.go

View check run for this annotation

Codecov / codecov/patch

pkg/iexec/iexec.go#L137

Added line #L137 was not covered by tests

_, err = tty.WriteString("Testing /dev/tty output...\n")
return err

Check warning on line 140 in pkg/iexec/iexec.go

View check run for this annotation

Codecov / codecov/patch

pkg/iexec/iexec.go#L139-L140

Added lines #L139 - L140 were not covered by tests
}

func (r *Iexec) Do() error {
err := testTTY()
if err != nil {
return err

Check warning on line 146 in pkg/iexec/iexec.go

View check run for this annotation

Codecov / codecov/patch

pkg/iexec/iexec.go#L144-L146

Added lines #L144 - L146 were not covered by tests
}

client, err := kubernetes.NewForConfig(r.restConfig)
if err != nil {
return errors.Wrap(err, "unable to get kubernetes for config")
Expand Down Expand Up @@ -189,7 +227,11 @@ func exec(restCfg *rest.Config, pod corev1.Pod, container corev1.Container, cmd
return errors.Wrap(err, "unable to init terminal")
}

termWidth, termHeight, _ := term.GetSize(fd)
termWidth, termHeight, err := term.GetSize(fd)
if err != nil {
log.Errorf("Error getting terminal size: %v", err)

Check warning on line 232 in pkg/iexec/iexec.go

View check run for this annotation

Codecov / codecov/patch

pkg/iexec/iexec.go#L230-L232

Added lines #L230 - L232 were not covered by tests
}

termSize := remotecommand.TerminalSize{Width: uint16(termWidth), Height: uint16(termHeight)}
s := make(sizeQueue, 1)
s <- termSize
Expand All @@ -202,13 +244,15 @@ func exec(restCfg *rest.Config, pod corev1.Pod, container corev1.Container, cmd
}()

// Connect this process' std{in,out,err} to the remote shell process.
log.Trace("Starting exec.StreamWithContext...")

Check warning on line 247 in pkg/iexec/iexec.go

View check run for this annotation

Codecov / codecov/patch

pkg/iexec/iexec.go#L247

Added line #L247 was not covered by tests
err = exec.StreamWithContext(context.Background(), remotecommand.StreamOptions{
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
Tty: true,
TerminalSizeQueue: s,
})
log.Trace("Finished exec.StreamWithContext.")

Check warning on line 255 in pkg/iexec/iexec.go

View check run for this annotation

Codecov / codecov/patch

pkg/iexec/iexec.go#L255

Added line #L255 was not covered by tests
if err != nil {
return errors.Wrap(err, "unable to stream shell process")
}
Expand Down

0 comments on commit 4422be2

Please # to comment.