Skip to content

Commit

Permalink
Make term function consistent with each other
Browse files Browse the repository at this point in the history
  • Loading branch information
creack committed Jun 25, 2013
1 parent a749fb2 commit 672d3a6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
15 changes: 7 additions & 8 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,11 @@ func (cli *DockerCli) CmdLogin(args ...string) error {
return readStringOnRawTerminal(stdin, stdout, false)
}

oldState, err := term.SetRawTerminal()
oldState, err := term.SetRawTerminal(os.Stdin.Fd())
if err != nil {
return err
}
defer term.RestoreTerminal(oldState)
defer term.RestoreTerminal(os.Stdin.Fd(), oldState)

cmd := Subcmd("login", "", "Register or Login to the docker registry server")
if err := cmd.Parse(args); err != nil {
Expand Down Expand Up @@ -319,7 +319,7 @@ func (cli *DockerCli) CmdLogin(args ...string) error {
password = cli.authConfig.Password
email = cli.authConfig.Email
}
term.RestoreTerminal(oldState)
term.RestoreTerminal(os.Stdin.Fd(), oldState)

cli.authConfig.Username = username
cli.authConfig.Password = password
Expand Down Expand Up @@ -1272,13 +1272,12 @@ func (cli *DockerCli) CmdRun(args ...string) error {
}

//start the container
_, _, err = cli.call("POST", "/containers/"+runResult.ID+"/start", nil)
if err != nil {
if _, _, err = cli.call("POST", "/containers/"+runResult.ID+"/start", nil); err != nil {
return err
}

if !config.AttachStdout && !config.AttachStderr {
fmt.Fprintf(cli.out, "%s\b", runResult.ID)
fmt.Fprintf(cli.out, "%s\n", runResult.ID)
}
if config.AttachStdin || config.AttachStdout || config.AttachStderr {
if config.Tty {
Expand Down Expand Up @@ -1457,11 +1456,11 @@ func (cli *DockerCli) hijack(method, path string, setRawTerminal bool, in *os.Fi
})

if in != nil && setRawTerminal && term.IsTerminal(in.Fd()) && os.Getenv("NORAW") == "" {
oldState, err := term.SetRawTerminal()
oldState, err := term.SetRawTerminal(os.Stdin.Fd())
if err != nil {
return err
}
defer term.RestoreTerminal(oldState)
defer term.RestoreTerminal(os.Stdin.Fd(), oldState)
}
sendStdin := utils.Go(func() error {
io.Copy(rwc, in)
Expand Down
12 changes: 4 additions & 8 deletions term/term.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,22 @@ func IsTerminal(fd uintptr) bool {

// Restore restores the terminal connected to the given file descriptor to a
// previous state.
func Restore(fd uintptr, state *State) error {
func RestoreTerminal(fd uintptr, state *State) error {
_, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(setTermios), uintptr(unsafe.Pointer(&state.termios)))
return err
}

func SetRawTerminal() (*State, error) {
oldState, err := MakeRaw(os.Stdin.Fd())
func SetRawTerminal(fd uintptr) (*State, error) {
oldState, err := MakeRaw(fd)
if err != nil {
return nil, err
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
_ = <-c
Restore(os.Stdin.Fd(), oldState)
RestoreTerminal(fd, oldState)
os.Exit(0)
}()
return oldState, err
}

func RestoreTerminal(state *State) {
Restore(os.Stdin.Fd(), state)
}

0 comments on commit 672d3a6

Please sign in to comment.