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"),
}
fwg, err := prepareStdio(sOpts.Stdin, sOpts.Stdout, sOpts.Stderr)
fwg, err := prepareStdio(sOpts.Stdin, sOpts.Stdout, sOpts.Stderr, sOpts.Console)
if err != nil {
return err
}

View file

@ -98,7 +98,7 @@ var runCommand = cli.Command{
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 {
return err
}

View file

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