...

Source file src/gotest.tools/v3/icmd/ops.go

Documentation: gotest.tools/v3/icmd

     1  package icmd
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  	"time"
     7  )
     8  
     9  // CmdOp is an operation which modified a Cmd structure used to execute commands
    10  type CmdOp func(*Cmd)
    11  
    12  // WithTimeout sets the timeout duration of the command
    13  func WithTimeout(timeout time.Duration) CmdOp {
    14  	return func(c *Cmd) {
    15  		c.Timeout = timeout
    16  	}
    17  }
    18  
    19  // WithEnv sets the environment variable of the command.
    20  // Each arguments are in the form of KEY=VALUE
    21  func WithEnv(env ...string) CmdOp {
    22  	return func(c *Cmd) {
    23  		c.Env = env
    24  	}
    25  }
    26  
    27  // Dir sets the working directory of the command
    28  func Dir(path string) CmdOp {
    29  	return func(c *Cmd) {
    30  		c.Dir = path
    31  	}
    32  }
    33  
    34  // WithStdin sets the standard input of the command to the specified reader
    35  func WithStdin(r io.Reader) CmdOp {
    36  	return func(c *Cmd) {
    37  		c.Stdin = r
    38  	}
    39  }
    40  
    41  // WithStdout sets the standard output of the command to the specified writer
    42  func WithStdout(w io.Writer) CmdOp {
    43  	return func(c *Cmd) {
    44  		c.Stdout = w
    45  	}
    46  }
    47  
    48  // WithStderr sets the standard error of the command to the specified writer
    49  func WithStderr(w io.Writer) CmdOp {
    50  	return func(c *Cmd) {
    51  		c.Stderr = w
    52  	}
    53  }
    54  
    55  // WithExtraFile adds a file descriptor to the command
    56  func WithExtraFile(f *os.File) CmdOp {
    57  	return func(c *Cmd) {
    58  		c.ExtraFiles = append(c.ExtraFiles, f)
    59  	}
    60  }
    61  

View as plain text