github.com/hustcat/docker@v1.3.3-0.20160314103604-901c67a8eeab/daemon/execdriver/termconsole.go (about) 1 package execdriver 2 3 import ( 4 "io" 5 "os/exec" 6 ) 7 8 // StdConsole defines standard console operations for execdriver 9 type StdConsole struct { 10 // Closers holds io.Closer references for closing at terminal close time 11 Closers []io.Closer 12 } 13 14 // NewStdConsole returns a new StdConsole struct 15 func NewStdConsole(processConfig *ProcessConfig, pipes *Pipes) (*StdConsole, error) { 16 std := &StdConsole{} 17 18 if err := std.AttachPipes(&processConfig.Cmd, pipes); err != nil { 19 return nil, err 20 } 21 return std, nil 22 } 23 24 // AttachPipes attaches given pipes to exec.Cmd 25 func (s *StdConsole) AttachPipes(command *exec.Cmd, pipes *Pipes) error { 26 command.Stdout = pipes.Stdout 27 command.Stderr = pipes.Stderr 28 29 if pipes.Stdin != nil { 30 stdin, err := command.StdinPipe() 31 if err != nil { 32 return err 33 } 34 35 go func() { 36 defer stdin.Close() 37 io.Copy(stdin, pipes.Stdin) 38 }() 39 } 40 return nil 41 } 42 43 // Resize implements Resize method of Terminal interface 44 func (s *StdConsole) Resize(h, w int) error { 45 // we do not need to resize a non tty 46 return nil 47 } 48 49 // Close implements Close method of Terminal interface 50 func (s *StdConsole) Close() error { 51 for _, c := range s.Closers { 52 c.Close() 53 } 54 return nil 55 }