{"modulePath":"github.com/AlaudaDevops/pkg","version":"v0.14.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package exec\n\nPackage exec contains an interface for executing commands, along with helpers TODO(bentheelder): add standardized timeout functionality \u0026 a default timeout so that commands cannot hang indefinitely (!)\n\nInspired by (copied from) [https://github.com/kubernetes-sigs/kind/tree/main/pkg/exec](https://github.com/kubernetes-sigs/kind/tree/main/pkg/exec)\n\n## Variables\n\n```go\nvar DefaultCmder = \u0026LocalCmder{}\n```\nDefaultCmder 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 :^)\n\n## Functions\n\n```go\nfunc CombinedOutputLines(cmd Cmd) (lines []string, err error)\n```\nCombinedOutputLines 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\n\n```go\nfunc Output(cmd Cmd) ([]byte, error)\n```\nOutput is like os/exec's cmd.Output, but over our Cmd interface\n\n```go\nfunc OutputLines(cmd Cmd) (lines []string, err error)\n```\nOutputLines 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\n\n```go\nfunc PrettyCommand(name string, args ...string) string\n```\nPrettyCommand takes arguments identical to Cmder.Command, it returns a pretty printed command that could be pasted into a shell\n\n```go\nfunc RunWithStdinWriter(cmd Cmd, writerFunc func(io.Writer) error) error\n```\nRunWithStdinWriter runs cmd with writerFunc piped to stdin\n\n```go\nfunc RunWithStdoutReader(cmd Cmd, readerFunc func(io.Reader) error) error\n```\nRunWithStdoutReader runs cmd with stdout piped to readerFunc\n\n```go\nfunc WithCmd(ctx context.Context, cmdr Cmd) context.Context\n```\nWithCmd adds a Cmd to a context\n\n```go\nfunc WithCmder(ctx context.Context, cmdr Cmder) context.Context\n```\nWithCmder adds a Cmder to a context\n\n## Types\n\n```go\ntype Cmd interface {\n\t// Run executes the command (like os/exec.Cmd.Run), it should return\n\t// a *RunError if there is any error\n\tRun() error\n\t// Each entry should be of the form \"key=value\"\n\tSetEnv(...string) Cmd\n\tSetStdin(io.Reader) Cmd\n\tSetStdout(io.Writer) Cmd\n\tSetStderr(io.Writer) Cmd\n}\n```\nCmd abstracts over running a command somewhere, this is useful for testing\n\n```go\nfunc Command(command string, args ...string) Cmd\n```\nCommand is a convenience wrapper over DefaultCmder.Command\n\n```go\nfunc CommandContext(ctx context.Context, command string, args ...string) Cmd\n```\nCommandContext is a convenience wrapper over DefaultCmder.CommandContext\n\n```go\nfunc FromContextCmd(ctx context.Context) (cmdr Cmd)\n```\nFromContextCmd returns a Cmd from the context\n\n```go\nfunc InheritOutput(cmd Cmd) Cmd\n```\nInheritOutput sets cmd's output to write to the current process's stdout and stderr\n\n```go\ntype Cmder interface {\n\t// command, args..., just like os/exec.Cmd\n\tCommand(string, ...string) Cmd\n\tCommandContext(context.Context, string, ...string) Cmd\n}\n```\nCmder abstracts over creating commands\n\n```go\nfunc FromContextCmder(ctx context.Context) (cmdr Cmder)\n```\nFromContextCmder returns a Cmder from the context\n\n```go\ntype LocalCmd struct {\n\t*osexec.Cmd\n}\n```\nLocalCmd wraps os/exec.Cmd, implementing the kind/pkg/exec.Cmd interface\n\n```go\nfunc (cmd *LocalCmd) Run() error\n```\nRun runs the command If the returned error is non-nil, it should be of type \\*RunError\n\n```go\nfunc (cmd *LocalCmd) SetEnv(env ...string) Cmd\n```\nSetEnv sets env\n\n```go\nfunc (cmd *LocalCmd) SetStderr(w io.Writer) Cmd\n```\nSetStderr sets stderr\n\n```go\nfunc (cmd *LocalCmd) SetStdin(r io.Reader) Cmd\n```\nSetStdin sets stdin\n\n```go\nfunc (cmd *LocalCmd) SetStdout(w io.Writer) Cmd\n```\nSetStdout set stdout\n\n```go\ntype LocalCmder struct{}\n```\nLocalCmder is a factory for LocalCmd, implementing Cmder\n\n```go\nfunc (c *LocalCmder) Command(name string, arg ...string) Cmd\n```\nCommand returns a new exec.Cmd backed by Cmd\n\n```go\nfunc (c *LocalCmder) CommandContext(ctx context.Context, name string, arg ...string) Cmd\n```\nCommandContext is like Command but includes a context\n\n```go\ntype NoOpCmd struct {\n\tContext context.Context\n\tCommand string\n\tArgs    []string\n\tEnvs    []string\n\n\tStdout io.Writer\n\tStderr io.Writer\n\tStdin  io.Reader\n}\n```\nNoOpCmd only writes down all executed commands as output used for testing only\n\n```go\nfunc (no *NoOpCmd) Run() error\n```\nRun writes down its command and args to the Stdout writer\n\n```go\nfunc (no *NoOpCmd) SetEnv(envs ...string) Cmd\n```\nSetEnv sets env\n\n```go\nfunc (no *NoOpCmd) SetStderr(stderr io.Writer) Cmd\n```\nSetStderr sets Stderr\n\n```go\nfunc (no *NoOpCmd) SetStdin(stdin io.Reader) Cmd\n```\nSetStdin sets Stdin reader, will not be used\n\n```go\nfunc (no *NoOpCmd) SetStdout(stdout io.Writer) Cmd\n```\nSetStdout sets Stdout\n\n```go\ntype NoOpCmder struct{}\n```\nNoOpCmder only writes down all executed commands as output used for testing only\n\n```go\nfunc (n *NoOpCmder) Command(cmd string, args ...string) Cmd\n```\nCommand initializes a NoOpCmd with given arguments\n\n```go\nfunc (n *NoOpCmder) CommandContext(ctx context.Context, cmd string, args ...string) Cmd\n```\nCommandContext initializes a NoOpCmd with given arguments\n\n```go\ntype RunError struct {\n\tCommand []string // [Name Args...]\n\tOutput  []byte   // Captured Stdout / Stderr of the command\n\tInner   error    // Underlying error if any\n}\n```\nRunError represents an error running a Cmd\n\n```go\nfunc RunErrorForError(err error) *RunError\n```\nRunErrorForError returns a RunError if the error contains a RunError. Otherwise it returns nil\n\n```go\nfunc (e *RunError) Cause() error\n```\nCause mimics github.com/pkg/errors's Cause pattern for errors\n\n```go\nfunc (e *RunError) Error() string\n```\n\n```go\nfunc (e *RunError) PrettyCommand() string\n```\nPrettyCommand pretty prints the command in a way that could be pasted into a shell\n\n","path":"github.com/AlaudaDevops/pkg/command/exec","name":"exec","synopsis":"Package exec contains an interface for executing commands, along with helpers TODO(bentheelder): add standardized timeout functionality \u0026 a default timeout so that commands cannot hang indefinitely (!)","isRedistributable":true}
