Add stdin support for client and daemon
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
27
io.go
27
io.go
@@ -8,20 +8,31 @@ import (
|
||||
type ioConfig struct {
|
||||
StdoutPath string
|
||||
StderrPath string
|
||||
Stdin io.WriteCloser
|
||||
Stdout io.ReadCloser
|
||||
Stderr io.ReadCloser
|
||||
StdinPath string
|
||||
|
||||
Stdin io.WriteCloser
|
||||
Stdout io.ReadCloser
|
||||
Stderr io.ReadCloser
|
||||
}
|
||||
|
||||
func newCopier(i *ioConfig) (*copier, error) {
|
||||
l := &copier{
|
||||
config: i,
|
||||
}
|
||||
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)
|
||||
go io.Copy(i.Stdin, f)
|
||||
}
|
||||
if i.StdoutPath != "" {
|
||||
f, err := os.OpenFile(i.StdoutPath, os.O_RDWR, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
l.closers = append(l.closers, f)
|
||||
go io.Copy(f, i.Stdout)
|
||||
}
|
||||
if i.StderrPath != "" {
|
||||
@@ -29,21 +40,19 @@ func newCopier(i *ioConfig) (*copier, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
l.closers = append(l.closers, f)
|
||||
go io.Copy(f, i.Stderr)
|
||||
}
|
||||
return l, nil
|
||||
}
|
||||
|
||||
type copier struct {
|
||||
config *ioConfig
|
||||
config *ioConfig
|
||||
closers []io.Closer
|
||||
}
|
||||
|
||||
func (l *copier) Close() (err error) {
|
||||
for _, c := range []io.Closer{
|
||||
l.config.Stdin,
|
||||
l.config.Stdout,
|
||||
l.config.Stderr,
|
||||
} {
|
||||
for _, c := range append(l.closers, l.config.Stdin, l.config.Stdout, l.config.Stderr) {
|
||||
if cerr := c.Close(); err == nil {
|
||||
err = cerr
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user