tractor.dev/toolkit-go@v0.0.0-20241010005851-214d91207d07/engine/cli/context.go (about)

     1  package cli
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  )
     7  
     8  type Context struct {
     9  	context.Context
    10  	*iocontext
    11  }
    12  
    13  // ContextWithIO returns a child context with a ContextIO
    14  // value added using the given Stdio equivalents.
    15  func ContextWithIO(parent context.Context, in io.Reader, out io.Writer, err io.Writer) *Context {
    16  	return &Context{
    17  		Context: parent,
    18  		iocontext: &iocontext{
    19  			in:  in,
    20  			out: out,
    21  			err: err,
    22  		},
    23  	}
    24  }
    25  
    26  type iocontext struct {
    27  	out, err io.Writer
    28  	in       io.Reader
    29  }
    30  
    31  func (c *iocontext) Write(p []byte) (n int, err error) {
    32  	return c.out.Write(p)
    33  }
    34  
    35  func (c *iocontext) Read(p []byte) (n int, err error) {
    36  	return c.in.Read(p)
    37  }
    38  
    39  func (c *iocontext) Errout() io.Writer {
    40  	return c.err
    41  }