github.com/containerd/containerd@v22.0.0-20200918172823-438c87b8e050+incompatible/cmd/ctr/commands/tasks/tasks_windows.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package tasks
    18  
    19  import (
    20  	gocontext "context"
    21  	"time"
    22  
    23  	"github.com/containerd/console"
    24  	"github.com/containerd/containerd"
    25  	"github.com/containerd/containerd/cio"
    26  	"github.com/containerd/containerd/log"
    27  	"github.com/pkg/errors"
    28  	"github.com/urfave/cli"
    29  )
    30  
    31  // HandleConsoleResize resizes the console
    32  func HandleConsoleResize(ctx gocontext.Context, task resizer, con console.Console) error {
    33  	// do an initial resize of the console
    34  	size, err := con.Size()
    35  	if err != nil {
    36  		return err
    37  	}
    38  	go func() {
    39  		prevSize := size
    40  		for {
    41  			time.Sleep(time.Millisecond * 250)
    42  
    43  			size, err := con.Size()
    44  			if err != nil {
    45  				log.G(ctx).WithError(err).Error("get pty size")
    46  				continue
    47  			}
    48  
    49  			if size.Width != prevSize.Width || size.Height != prevSize.Height {
    50  				if err := task.Resize(ctx, uint32(size.Width), uint32(size.Height)); err != nil {
    51  					log.G(ctx).WithError(err).Error("resize pty")
    52  				}
    53  				prevSize = size
    54  			}
    55  		}
    56  	}()
    57  	return nil
    58  }
    59  
    60  // NewTask creates a new task
    61  func NewTask(ctx gocontext.Context, client *containerd.Client, container containerd.Container, _ string, con console.Console, nullIO bool, logURI string, ioOpts []cio.Opt, opts ...containerd.NewTaskOpts) (containerd.Task, error) {
    62  	var ioCreator cio.Creator
    63  	if con != nil {
    64  		if nullIO {
    65  			return nil, errors.New("tty and null-io cannot be used together")
    66  		}
    67  		ioCreator = cio.NewCreator(append([]cio.Opt{cio.WithStreams(con, con, nil), cio.WithTerminal}, ioOpts...)...)
    68  	} else if nullIO {
    69  		ioCreator = cio.NullIO
    70  	} else {
    71  		ioCreator = cio.NewCreator(append([]cio.Opt{cio.WithStdio}, ioOpts...)...)
    72  	}
    73  	return container.NewTask(ctx, ioCreator)
    74  }
    75  
    76  func getNewTaskOpts(_ *cli.Context) []containerd.NewTaskOpts {
    77  	return nil
    78  }