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