Documentation ¶
Overview ¶
Package exec contains an interface for executing commands, along with helpers TODO(bentheelder): add standardized timeout functionality & a default timeout so that commands cannot hang indefinitely (!)
Inspired by (copied from) https://github.com/kubernetes-sigs/kind/tree/main/pkg/exec
Index ¶
- Variables
- func CombinedOutputLines(cmd Cmd) (lines []string, err error)
- func Output(cmd Cmd) ([]byte, error)
- func OutputLines(cmd Cmd) (lines []string, err error)
- func PrettyCommand(name string, args ...string) string
- func RunWithStdinWriter(cmd Cmd, writerFunc func(io.Writer) error) error
- func RunWithStdoutReader(cmd Cmd, readerFunc func(io.Reader) error) error
- func WithCmd(ctx context.Context, cmdr Cmd) context.Context
- func WithCmder(ctx context.Context, cmdr Cmder) context.Context
- type Cmd
- type Cmder
- type LocalCmd
- type LocalCmder
- type NoOpCmd
- type NoOpCmder
- type RunError
Constants ¶
This section is empty.
Variables ¶
var DefaultCmder = &LocalCmder{}
DefaultCmder is a LocalCmder instance used for convenience, packages originally using os/exec.Command can instead use pkg/kind/exec.Command which forwards to this instance TODO(bentheelder): swap this for testing TODO(bentheelder): consider not using a global for this :^)
Functions ¶
func CombinedOutputLines ¶
CombinedOutputLines is like os/exec's cmd.CombinedOutput(), but over our Cmd interface, and instead of returning the byte buffer of stderr + stdout, it scans these for lines and returns a slice of output lines
func OutputLines ¶
OutputLines is like os/exec's cmd.Output(), but over our Cmd interface, and instead of returning the byte buffer of stdout, it scans these for lines and returns a slice of output lines
func PrettyCommand ¶
PrettyCommand takes arguments identical to Cmder.Command, it returns a pretty printed command that could be pasted into a shell
func RunWithStdinWriter ¶
RunWithStdinWriter runs cmd with writerFunc piped to stdin
func RunWithStdoutReader ¶
RunWithStdoutReader runs cmd with stdout piped to readerFunc
Types ¶
type Cmd ¶
type Cmd interface { // Run executes the command (like os/exec.Cmd.Run), it should return // a *RunError if there is any error Run() error // Each entry should be of the form "key=value" SetEnv(...string) Cmd SetStdin(io.Reader) Cmd SetStdout(io.Writer) Cmd SetStderr(io.Writer) Cmd }
Cmd abstracts over running a command somewhere, this is useful for testing
func CommandContext ¶
CommandContext is a convenience wrapper over DefaultCmder.CommandContext
func FromContextCmd ¶
FromContextCmd returns a Cmd from the context
func InheritOutput ¶
InheritOutput sets cmd's output to write to the current process's stdout and stderr
type Cmder ¶
type Cmder interface { // command, args..., just like os/exec.Cmd Command(string, ...string) Cmd CommandContext(context.Context, string, ...string) Cmd }
Cmder abstracts over creating commands
func FromContextCmder ¶
FromContextCmder returns a Cmder from the context
type LocalCmd ¶
LocalCmd wraps os/exec.Cmd, implementing the kind/pkg/exec.Cmd interface
type LocalCmder ¶
type LocalCmder struct{}
LocalCmder is a factory for LocalCmd, implementing Cmder
func (*LocalCmder) Command ¶
func (c *LocalCmder) Command(name string, arg ...string) Cmd
Command returns a new exec.Cmd backed by Cmd
func (*LocalCmder) CommandContext ¶
CommandContext is like Command but includes a context
type NoOpCmd ¶
type NoOpCmd struct { Context context.Context Command string Args []string Envs []string Stdout io.Writer Stderr io.Writer Stdin io.Reader }
NoOpCmd only writes down all executed commands as output used for testing only
type NoOpCmder ¶
type NoOpCmder struct{}
NoOpCmder only writes down all executed commands as output used for testing only
type RunError ¶
type RunError struct { Command []string // [Name Args...] Output []byte // Captured Stdout / Stderr of the command Inner error // Underlying error if any }
RunError represents an error running a Cmd
func RunErrorForError ¶
RunErrorForError returns a RunError if the error contains a RunError. Otherwise it returns nil
func (*RunError) PrettyCommand ¶
PrettyCommand pretty prints the command in a way that could be pasted into a shell