kind: sigs.k8s.io/kind/pkg/exec Index | Files

package exec

import "sigs.k8s.io/kind/pkg/exec"

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 (!)

Index

Package Files

default.go doc.go helpers.go local.go types.go

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 :^)

func CombinedOutputLines Uses

func CombinedOutputLines(cmd Cmd) (lines []string, err error)

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 Uses

func OutputLines(cmd Cmd) (lines []string, err error)

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 Uses

func PrettyCommand(name string, args ...string) string

PrettyCommand takes arguments identical to Cmder.Command, it returns a pretty printed command that could be pasted into a shell

func RunWithStdinWriter Uses

func RunWithStdinWriter(cmd Cmd, writerFunc func(io.Writer) error) error

RunWithStdinWriter runs cmd with writerFunc piped to stdin

func RunWithStdoutReader Uses

func RunWithStdoutReader(cmd Cmd, readerFunc func(io.Reader) error) error

RunWithStdoutReader runs cmd with stdout piped to readerFunc

type Cmd Uses

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 Command Uses

func Command(command string, args ...string) Cmd

Command is a convenience wrapper over DefaultCmder.Command

func InheritOutput Uses

func InheritOutput(cmd Cmd) Cmd

InheritOutput sets cmd's output to write to the current process's stdout and stderr

type Cmder Uses

type Cmder interface {
    // command, args..., just like os/exec.Cmd
    Command(string, ...string) Cmd
}

Cmder abstracts over creating commands

type LocalCmd Uses

type LocalCmd struct {
    *osexec.Cmd
}

LocalCmd wraps os/exec.Cmd, implementing the kind/pkg/exec.Cmd interface

func (*LocalCmd) Run Uses

func (cmd *LocalCmd) Run() error

Run runs the command If the returned error is non-nil, it should be of type *RunError

func (*LocalCmd) SetEnv Uses

func (cmd *LocalCmd) SetEnv(env ...string) Cmd

SetEnv sets env

func (*LocalCmd) SetStderr Uses

func (cmd *LocalCmd) SetStderr(w io.Writer) Cmd

SetStderr sets stderr

func (*LocalCmd) SetStdin Uses

func (cmd *LocalCmd) SetStdin(r io.Reader) Cmd

SetStdin sets stdin

func (*LocalCmd) SetStdout Uses

func (cmd *LocalCmd) SetStdout(w io.Writer) Cmd

SetStdout set stdout

type LocalCmder Uses

type LocalCmder struct{}

LocalCmder is a factory for LocalCmd, implementing Cmder

func (*LocalCmder) Command Uses

func (c *LocalCmder) Command(name string, arg ...string) Cmd

Command returns a new exec.Cmd backed by Cmd

type RunError Uses

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 Uses

func RunErrorForError(err error) *RunError

RunErrorForError returns a RunError if the error contains a RunError. Otherwise it returns nil

func (*RunError) Cause Uses

func (e *RunError) Cause() error

Cause mimics github.com/pkg/errors's Cause pattern for errors

func (*RunError) Error Uses

func (e *RunError) Error() string

func (*RunError) PrettyCommand Uses

func (e *RunError) PrettyCommand() string

PrettyCommand pretty prints the command in a way that could be pasted into a shell

Package exec imports 10 packages (graph) and is imported by 29 packages. Updated 2019-11-07. Refresh now. Tools for package owners.