utils: k8s.io/utils/exec Index | Examples | Files | Directories

package exec

import "k8s.io/utils/exec"

Package exec provides an injectable interface and implementations for running commands.

Index

Examples

Package Files

doc.go exec.go

Variables

var ErrExecutableNotFound = osexec.ErrNotFound

ErrExecutableNotFound is returned if the executable is not found.

type Cmd Uses

type Cmd interface {
    // Run runs the command to the completion.
    Run() error
    // CombinedOutput runs the command and returns its combined standard output
    // and standard error. This follows the pattern of package os/exec.
    CombinedOutput() ([]byte, error)
    // Output runs the command and returns standard output, but not standard err
    Output() ([]byte, error)
    SetDir(dir string)
    SetStdin(in io.Reader)
    SetStdout(out io.Writer)
    SetStderr(out io.Writer)
    SetEnv(env []string)

    // StdoutPipe and StderrPipe for getting the process' Stdout and Stderr as
    // Readers
    StdoutPipe() (io.ReadCloser, error)
    StderrPipe() (io.ReadCloser, error)

    // Start and Wait are for running a process non-blocking
    Start() error
    Wait() error

    // Stops the command by sending SIGTERM. It is not guaranteed the
    // process will stop before this function returns. If the process is not
    // responding, an internal timer function will send a SIGKILL to force
    // terminate after 10 seconds.
    Stop()
}

Cmd is an interface that presents an API that is very similar to Cmd from os/exec. As more functionality is needed, this can grow. Since Cmd is a struct, we will have to replace fields with get/set method pairs.

type CodeExitError Uses

type CodeExitError struct {
    Err  error
    Code int
}

CodeExitError is an implementation of ExitError consisting of an error object and an exit code (the upper bits of os.exec.ExitStatus).

func (CodeExitError) Error Uses

func (e CodeExitError) Error() string

func (CodeExitError) ExitStatus Uses

func (e CodeExitError) ExitStatus() int

ExitStatus is for checking the error code

func (CodeExitError) Exited Uses

func (e CodeExitError) Exited() bool

Exited is to check if the process has finished

func (CodeExitError) String Uses

func (e CodeExitError) String() string

type ExitError Uses

type ExitError interface {
    String() string
    Error() string
    Exited() bool
    ExitStatus() int
}

ExitError is an interface that presents an API similar to os.ProcessState, which is what ExitError from os/exec is. This is designed to make testing a bit easier and probably loses some of the cross-platform properties of the underlying library.

type ExitErrorWrapper Uses

type ExitErrorWrapper struct {
    *osexec.ExitError
}

ExitErrorWrapper is an implementation of ExitError in terms of os/exec ExitError. Note: standard exec.ExitError is type *os.ProcessState, which already implements Exited().

func (ExitErrorWrapper) ExitStatus Uses

func (eew ExitErrorWrapper) ExitStatus() int

ExitStatus is part of the ExitError interface.

type Interface Uses

type Interface interface {
    // Command returns a Cmd instance which can be used to run a single command.
    // This follows the pattern of package os/exec.
    Command(cmd string, args ...string) Cmd

    // CommandContext returns a Cmd instance which can be used to run a single command.
    //
    // The provided context is used to kill the process if the context becomes done
    // before the command completes on its own. For example, a timeout can be set in
    // the context.
    CommandContext(ctx context.Context, cmd string, args ...string) Cmd

    // LookPath wraps os/exec.LookPath
    LookPath(file string) (string, error)
}

Interface is an interface that presents a subset of the os/exec API. Use this when you want to inject fakeable/mockable exec behavior.

func New Uses

func New() Interface

New returns a new Interface which will os/exec to run commands.

Code:

exec := exec.New()

cmd := exec.Command("echo", "Bonjour!")
buff := bytes.Buffer{}
cmd.SetStdout(&buff)
if err := cmd.Run(); err != nil {
    panic(err)
}
fmt.Println(buff.String())

Output:

Bonjour!

Code:

cmd := exec.New().Command("/bin/sh", "-c", "echo 'We can read from stderr via pipe!' >&2")

stderrPipe, err := cmd.StderrPipe()
if err != nil {
    panic(err)
}

stderr := make(chan []byte)
go func() {
    b, err := ioutil.ReadAll(stderrPipe)
    if err != nil {
        panic(err)
    }
    stderr <- b
}()

if err := cmd.Start(); err != nil {
    panic(err)
}

received := <-stderr

if err := cmd.Wait(); err != nil {
    panic(err)
}

fmt.Println(string(received))

Output:

We can read from stderr via pipe!

Directories

PathSynopsis
testing

Package exec imports 5 packages (graph) and is imported by 758 packages. Updated 2019-10-31. Refresh now. Tools for package owners.