2015-12-18 00:07:04 +00:00
|
|
|
package supervisor
|
2015-12-14 22:15:26 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ioConfig struct {
|
|
|
|
StdoutPath string
|
|
|
|
StderrPath string
|
2015-12-14 22:40:50 +00:00
|
|
|
StdinPath string
|
|
|
|
|
|
|
|
Stdin io.WriteCloser
|
|
|
|
Stdout io.ReadCloser
|
|
|
|
Stderr io.ReadCloser
|
2015-12-14 22:15:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newCopier(i *ioConfig) (*copier, error) {
|
|
|
|
l := &copier{
|
|
|
|
config: i,
|
|
|
|
}
|
2015-12-14 22:40:50 +00:00
|
|
|
if i.StdinPath != "" {
|
|
|
|
f, err := os.OpenFile(i.StdinPath, os.O_RDONLY, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
l.closers = append(l.closers, f)
|
2015-12-17 21:42:02 +00:00
|
|
|
go func() {
|
|
|
|
io.Copy(i.Stdin, f)
|
|
|
|
i.Stdin.Close()
|
|
|
|
}()
|
2015-12-14 22:40:50 +00:00
|
|
|
}
|
2015-12-14 22:15:26 +00:00
|
|
|
if i.StdoutPath != "" {
|
|
|
|
f, err := os.OpenFile(i.StdoutPath, os.O_RDWR, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-12-14 22:40:50 +00:00
|
|
|
l.closers = append(l.closers, f)
|
2015-12-14 22:15:26 +00:00
|
|
|
go io.Copy(f, i.Stdout)
|
|
|
|
}
|
|
|
|
if i.StderrPath != "" {
|
|
|
|
f, err := os.OpenFile(i.StderrPath, os.O_RDWR, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-12-14 22:40:50 +00:00
|
|
|
l.closers = append(l.closers, f)
|
2015-12-14 22:15:26 +00:00
|
|
|
go io.Copy(f, i.Stderr)
|
|
|
|
}
|
|
|
|
return l, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type copier struct {
|
2015-12-14 22:40:50 +00:00
|
|
|
config *ioConfig
|
|
|
|
closers []io.Closer
|
2015-12-14 22:15:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *copier) Close() (err error) {
|
2015-12-14 22:40:50 +00:00
|
|
|
for _, c := range append(l.closers, l.config.Stdin, l.config.Stdout, l.config.Stderr) {
|
2015-12-15 00:47:42 +00:00
|
|
|
if c != nil {
|
|
|
|
if cerr := c.Close(); err == nil {
|
|
|
|
err = cerr
|
|
|
|
}
|
2015-12-14 22:15:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|