ctr: take console flag in account when readying IOs

Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com>
This commit is contained in:
Kenfe-Mickael Laventure 2017-01-12 10:24:22 -08:00
parent 8e603d2e48
commit 3234546ee6
3 changed files with 46 additions and 44 deletions

View file

@ -64,7 +64,7 @@ var execCommand = cli.Command{
Console: context.Bool("tty"), Console: context.Bool("tty"),
} }
fwg, err := prepareStdio(sOpts.Stdin, sOpts.Stdout, sOpts.Stderr) fwg, err := prepareStdio(sOpts.Stdin, sOpts.Stdout, sOpts.Stderr, sOpts.Console)
if err != nil { if err != nil {
return err return err
} }

View file

@ -98,7 +98,7 @@ var runCommand = cli.Command{
Stderr: filepath.Join(tmpDir, "stderr"), Stderr: filepath.Join(tmpDir, "stderr"),
} }
fwg, err := prepareStdio(crOpts.Stdin, crOpts.Stdout, crOpts.Stderr) fwg, err := prepareStdio(crOpts.Stdin, crOpts.Stdout, crOpts.Stderr, crOpts.Console)
if err != nil { if err != nil {
return err return err
} }

View file

@ -23,27 +23,11 @@ import (
var grpcConn *grpc.ClientConn var grpcConn *grpc.ClientConn
func prepareStdio(in, out, err string) (*sync.WaitGroup, error) { func prepareStdio(stdin, stdout, stderr string, console bool) (*sync.WaitGroup, error) {
var ( var wg sync.WaitGroup
wg sync.WaitGroup ctx := gocontext.Background()
dst io.Writer f, err := fifo.OpenFifo(ctx, stdin, syscall.O_WRONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0700)
src io.Reader
close func()
)
for _, f := range []struct {
name string
flags int
src bool
reader io.Reader
writer io.Writer
}{
{in, syscall.O_WRONLY | syscall.O_CREAT | syscall.O_NONBLOCK, false, os.Stdin, nil},
{out, syscall.O_RDONLY | syscall.O_CREAT | syscall.O_NONBLOCK, true, nil, os.Stdout},
{err, syscall.O_RDONLY | syscall.O_CREAT | syscall.O_NONBLOCK, true, nil, os.Stderr},
} {
ff, err := fifo.OpenFifo(gocontext.Background(), f.name, f.flags, 0700)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -51,26 +35,44 @@ func prepareStdio(in, out, err string) (*sync.WaitGroup, error) {
if err != nil { if err != nil {
c.Close() c.Close()
} }
}(ff) }(f)
go func(w io.Writer) {
io.Copy(w, os.Stdin)
f.Close()
}(f)
if f.src { f, err = fifo.OpenFifo(ctx, stdout, syscall.O_RDONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0700)
src = ff if err != nil {
dst = f.writer return nil, err
close = func() {
ff.Close()
wg.Done()
} }
defer func(c io.Closer) {
if err != nil {
c.Close()
}
}(f)
wg.Add(1) wg.Add(1)
} else { go func(r io.Reader) {
src = f.reader io.Copy(os.Stdout, r)
dst = ff f.Close()
close = func() { ff.Close() } wg.Done()
} }(f)
go func(dst io.Writer, src io.Reader, close func()) { f, err = fifo.OpenFifo(ctx, stderr, syscall.O_RDONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0700)
io.Copy(dst, src) if err != nil {
close() return nil, err
}(dst, src, close) }
defer func(c io.Closer) {
if err != nil {
c.Close()
}
}(f)
if !console {
wg.Add(1)
go func(r io.Reader) {
io.Copy(os.Stderr, r)
f.Close()
wg.Done()
}(f)
} }
return &wg, nil return &wg, nil