Skip to content

Commit

Permalink
Add terminal check for Stdin
Browse files Browse the repository at this point in the history
Fixes #37

Add check to only set RAW mode in case Stdin is a terminal.
  • Loading branch information
HeavyWombat committed Oct 12, 2022
1 parent 45708b2 commit fb86a37
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/gonvenience/neat v1.3.11
github.com/gonvenience/term v1.0.2
github.com/gonvenience/wrap v1.1.2
github.com/mattn/go-isatty v0.0.16
github.com/onsi/ginkgo/v2 v2.3.0
github.com/onsi/gomega v1.22.1
github.com/spf13/cobra v1.6.0
Expand All @@ -23,7 +24,6 @@ require (
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-ciede2000 v0.0.0-20170301095244-782e8c62fec3 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mitchellh/go-ps v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
Expand Down
59 changes: 35 additions & 24 deletions internal/ptexec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import (

"github.com/creack/pty"
"github.com/gonvenience/wrap"
terminal "golang.org/x/term"
"github.com/mattn/go-isatty"
"golang.org/x/term"
)

// RunCommandInPseudoTerminal runs the provided program with the given
Expand All @@ -54,40 +55,45 @@ func RunCommandInPseudoTerminal(name string, args ...string) ([]byte, error) {
name = "/bin/sh"
}

// Set RAW mode for Stdin
if isTerminal(os.Stdin) {
oldState, rawErr := term.MakeRaw(int(os.Stdin.Fd()))
if rawErr != nil {
return nil, wrap.Errorf(rawErr, "failed to enable RAW mode for Stdin")
}

// And make sure to restore the original mode eventually
defer func() { _ = term.Restore(int(os.Stdin.Fd()), oldState) }()
}

pt, err := pty.Start(exec.Command(name, args...))
if err != nil {
return nil, err
}

defer func() {
_ = pt.Close()
}()

// Support terminal resizing
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGWINCH)
go func() {
for range ch {
if ptyErr := pty.InheritSize(os.Stdin, pt); ptyErr != nil {
errors = append(errors, wrap.Error(ptyErr, "error resizing PTY"))
if isTerminal(os.Stdin) {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGWINCH)
go func() {
for range ch {
if ptyErr := pty.InheritSize(os.Stdin, pt); ptyErr != nil {
errors = append(errors, wrap.Error(ptyErr, "error resizing PTY"))
}
}
}
}()
ch <- syscall.SIGWINCH
}()

// Set RAW mode for stdin
oldState, err := terminal.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
return nil, wrap.Errorf(err, "failed to enable RAW mode for stdin")
ch <- syscall.SIGWINCH
defer func() {
signal.Stop(ch)
close(ch)
}()
}

// Make sure to restore the original mode
defer func() {
_ = terminal.Restore(int(os.Stdin.Fd()), oldState)
}()

go func() {
if _, copyErr := io.Copy(pt, os.Stdin); copyErr != nil {
defer pt.Close()
_, copyErr := io.Copy(pt, os.Stdin)
if copyErr != nil {
errors = append(errors, copyErr)
}
}()
Expand Down Expand Up @@ -125,3 +131,8 @@ func copy(dst io.Writer, src io.Reader) error {

return err
}

func isTerminal(f *os.File) bool {
return isatty.IsTerminal(f.Fd()) ||
isatty.IsCygwinTerminal(f.Fd())
}

0 comments on commit fb86a37

Please sign in to comment.