2014-02-24 15:28:45 -05:00
|
|
|
package execdriver
|
2014-02-21 15:32:14 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2014-02-22 04:21:26 -05:00
|
|
|
"os/exec"
|
2014-02-21 15:32:14 -05:00
|
|
|
)
|
|
|
|
|
2015-07-27 20:43:22 -04:00
|
|
|
// StdConsole defines standard console operations for execdriver
|
2014-02-21 15:32:14 -05:00
|
|
|
type StdConsole struct {
|
2015-10-08 11:51:41 -04:00
|
|
|
// Closers holds io.Closer references for closing at terminal close time
|
|
|
|
Closers []io.Closer
|
2014-02-21 15:32:14 -05:00
|
|
|
}
|
|
|
|
|
2015-07-27 20:43:22 -04:00
|
|
|
// NewStdConsole returns a new StdConsole struct
|
2014-08-26 18:05:37 -04:00
|
|
|
func NewStdConsole(processConfig *ProcessConfig, pipes *Pipes) (*StdConsole, error) {
|
2014-02-21 15:42:37 -05:00
|
|
|
std := &StdConsole{}
|
|
|
|
|
2014-08-26 18:05:37 -04:00
|
|
|
if err := std.AttachPipes(&processConfig.Cmd, pipes); err != nil {
|
2014-02-21 15:42:37 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return std, nil
|
2014-02-21 15:32:14 -05:00
|
|
|
}
|
|
|
|
|
2015-07-27 20:43:22 -04:00
|
|
|
// AttachPipes attaches given pipes to exec.Cmd
|
2014-02-26 15:55:24 -05:00
|
|
|
func (s *StdConsole) AttachPipes(command *exec.Cmd, pipes *Pipes) error {
|
2014-02-21 15:42:37 -05:00
|
|
|
command.Stdout = pipes.Stdout
|
|
|
|
command.Stderr = pipes.Stderr
|
2014-02-21 15:32:14 -05:00
|
|
|
|
|
|
|
if pipes.Stdin != nil {
|
2014-02-21 15:42:37 -05:00
|
|
|
stdin, err := command.StdinPipe()
|
2014-02-21 15:32:14 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer stdin.Close()
|
|
|
|
io.Copy(stdin, pipes.Stdin)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-27 20:43:22 -04:00
|
|
|
// Resize implements Resize method of Terminal interface
|
2014-02-21 15:32:14 -05:00
|
|
|
func (s *StdConsole) Resize(h, w int) error {
|
2015-08-10 04:20:35 -04:00
|
|
|
// we do not need to resize a non tty
|
2014-02-21 15:32:14 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-27 20:43:22 -04:00
|
|
|
// Close implements Close method of Terminal interface
|
2014-02-21 15:32:14 -05:00
|
|
|
func (s *StdConsole) Close() error {
|
2015-10-08 11:51:41 -04:00
|
|
|
for _, c := range s.Closers {
|
|
|
|
c.Close()
|
|
|
|
}
|
2014-02-21 15:32:14 -05:00
|
|
|
return nil
|
|
|
|
}
|