From 0dea724fc0a8ac6599bb0d33aa3792ff1bd3a0aa Mon Sep 17 00:00:00 2001 From: Guanjun Gong Date: Thu, 10 Sep 2020 15:12:44 +0800 Subject: [PATCH] NewTask avoid unnecessary cio.NewCreator calls Signed-off-by: Guanjun Gong --- cmd/ctr/commands/tasks/tasks_unix.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/cmd/ctr/commands/tasks/tasks_unix.go b/cmd/ctr/commands/tasks/tasks_unix.go index 12917f201..0ec6dc010 100644 --- a/cmd/ctr/commands/tasks/tasks_unix.go +++ b/cmd/ctr/commands/tasks/tasks_unix.go @@ -72,7 +72,6 @@ func NewTask(ctx gocontext.Context, client *containerd.Client, container contain stdinC := &stdinCloser{ stdin: os.Stdin, } - stdio := cio.NewCreator(append([]cio.Opt{cio.WithStreams(stdinC, os.Stdout, os.Stderr)}, ioOpts...)...) if checkpoint != "" { im, err := client.GetImage(ctx, checkpoint) if err != nil { @@ -80,22 +79,22 @@ func NewTask(ctx gocontext.Context, client *containerd.Client, container contain } opts = append(opts, containerd.WithTaskCheckpoint(im)) } - ioCreator := stdio + var ioCreator cio.Creator if con != nil { - ioCreator = cio.NewCreator(append([]cio.Opt{cio.WithStreams(con, con, nil), cio.WithTerminal}, ioOpts...)...) - } - if nullIO { - if con != nil { + if nullIO { return nil, errors.New("tty and null-io cannot be used together") } + ioCreator = cio.NewCreator(append([]cio.Opt{cio.WithStreams(con, con, nil), cio.WithTerminal}, ioOpts...)...) + } else if nullIO { ioCreator = cio.NullIO - } - if logURI != "" { + } else if logURI != "" { u, err := url.Parse(logURI) if err != nil { return nil, err } ioCreator = cio.LogURI(u) + } else { + ioCreator = cio.NewCreator(append([]cio.Opt{cio.WithStreams(stdinC, os.Stdout, os.Stderr)}, ioOpts...)...) } t, err := container.NewTask(ctx, ioCreator, opts...) if err != nil {