Skip to content

Commit

Permalink
dont make the terminal raw until you've read data
Browse files Browse the repository at this point in the history
  • Loading branch information
csquared committed Sep 15, 2015
1 parent e509525 commit dbcc7cf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
21 changes: 18 additions & 3 deletions client/processes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package client
import (
"fmt"
"io"
"os"
"strconv"
"strings"
"time"

"github.com/docker/docker/pkg/term"
)

type Process struct {
Expand Down Expand Up @@ -36,15 +39,15 @@ 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()
defer w.Close()

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)

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
11 changes: 1 addition & 10 deletions cmd/convox/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/codegangsta/cli"
"github.com/convox/rack/cmd/convox/stdcli"
"github.com/docker/docker/pkg/term"
)

func init() {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}

Expand Down

0 comments on commit dbcc7cf

Please sign in to comment.