From dbcc7cf4b3b04ba4c00e8e4183f4c2d0aaab612c Mon Sep 17 00:00:00 2001 From: csquared Date: Tue, 15 Sep 2015 14:46:29 -0700 Subject: [PATCH] dont make the terminal raw until you've read data --- client/processes.go | 21 ++++++++++++++++++--- cmd/convox/run.go | 11 +---------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/client/processes.go b/client/processes.go index 585c2c697b..be5f10fe43 100644 --- a/client/processes.go +++ b/client/processes.go @@ -3,9 +3,12 @@ package client import ( "fmt" "io" + "os" "strconv" "strings" "time" + + "github.com/docker/docker/pkg/term" ) type Process struct { @@ -36,7 +39,7 @@ func (c *Client) GetProcesses(app string) (Processes, error) { return processes, nil } -func (c *Client) RunProcessAttached(app, process, command string, in io.Reader, out io.Writer) (int, error) { +func (c *Client) RunProcessAttached(app, process, command string, in io.Reader, out io.Writer, raw bool) (int, error) { r, w := io.Pipe() defer r.Close() @@ -44,7 +47,7 @@ func (c *Client) RunProcessAttached(app, process, command string, in io.Reader, ch := make(chan int) - go copyWithExit(out, r, ch) + go copyWithExit(out, r, ch, raw) err := c.Stream(fmt.Sprintf("/apps/%s/processes/%s/run", app, process), map[string]string{"Command": command}, in, w) @@ -84,8 +87,9 @@ func (c *Client) StopProcess(app, id string) (*Process, error) { return &process, nil } -func copyWithExit(w io.Writer, r io.Reader, ch chan int) { +func copyWithExit(w io.Writer, r io.Reader, ch chan int, raw bool) { buf := make([]byte, 1024) + isTerminalRaw := false for { n, err := r.Read(buf) @@ -94,6 +98,17 @@ func copyWithExit(w io.Writer, r io.Reader, ch chan int) { break } + // don't make the terminal raw until we've read some data + if raw && !isTerminalRaw { + fd := os.Stdin.Fd() + oldState, err := term.SetRawTerminal(fd) + if err != nil { + break + } + defer term.RestoreTerminal(fd, oldState) + isTerminalRaw = true + } + if s := string(buf[0:n]); strings.HasPrefix(s, "F1E49A85-0AD7-4AEF-A618-C249C6E6568D:") { code, _ := strconv.Atoi(s[37:]) ch <- code diff --git a/cmd/convox/run.go b/cmd/convox/run.go index 202012e8c4..ec80143749 100644 --- a/cmd/convox/run.go +++ b/cmd/convox/run.go @@ -9,7 +9,6 @@ import ( "github.com/codegangsta/cli" "github.com/convox/rack/cmd/convox/stdcli" - "github.com/docker/docker/pkg/term" ) func init() { @@ -37,12 +36,6 @@ func cmdRun(c *cli.Context) { return } - fd := os.Stdin.Fd() - - oldState, err := term.SetRawTerminal(fd) - - defer term.RestoreTerminal(fd, oldState) - _, app, err := stdcli.DirApp(c, ".") if err != nil { @@ -57,15 +50,13 @@ func cmdRun(c *cli.Context) { ps := c.Args()[0] - code, err := rackClient(c).RunProcessAttached(app, ps, strings.Join(c.Args()[1:], " "), os.Stdin, os.Stdout) + code, err := rackClient(c).RunProcessAttached(app, ps, strings.Join(c.Args()[1:], " "), os.Stdin, os.Stdout, true) if err != nil { stdcli.Error(err) return } - term.RestoreTerminal(fd, oldState) - os.Exit(code) }