exec

package
v0.13.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 12, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
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

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 Output

func Output(cmd Cmd) ([]byte, error)

Output is like os/exec's cmd.Output, but over our Cmd interface

func OutputLines

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

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

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

RunWithStdinWriter runs cmd with writerFunc piped to stdin

func RunWithStdoutReader

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

RunWithStdoutReader runs cmd with stdout piped to readerFunc

func WithCmd

func WithCmd(ctx context.Context, cmdr Cmd) context.Context

WithCmd adds a Cmd to a context

func WithCmder

func WithCmder(ctx context.Context, cmdr Cmder) context.Context

WithCmder adds a Cmder to a context

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 Command

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

Command is a convenience wrapper over DefaultCmder.Command

func CommandContext

func CommandContext(ctx context.Context, command string, args ...string) Cmd

CommandContext is a convenience wrapper over DefaultCmder.CommandContext

func FromContextCmd

func FromContextCmd(ctx context.Context) (cmdr Cmd)

FromContextCmd returns a Cmd from the context

func InheritOutput

func InheritOutput(cmd Cmd) Cmd

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

func FromContextCmder(ctx context.Context) (cmdr Cmder)

FromContextCmder returns a Cmder from the context

type LocalCmd

type LocalCmd struct {
	*osexec.Cmd
}

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

func (*LocalCmd) Run

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

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

SetEnv sets env

func (*LocalCmd) SetStderr

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

SetStderr sets stderr

func (*LocalCmd) SetStdin

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

SetStdin sets stdin

func (*LocalCmd) SetStdout

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

SetStdout set stdout

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

func (c *LocalCmder) CommandContext(ctx context.Context, name string, arg ...string) Cmd

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

func (*NoOpCmd) Run

func (no *NoOpCmd) Run() error

Run writes down its command and args to the Stdout writer

func (*NoOpCmd) SetEnv

func (no *NoOpCmd) SetEnv(envs ...string) Cmd

SetEnv sets env

func (*NoOpCmd) SetStderr

func (no *NoOpCmd) SetStderr(stderr io.Writer) Cmd

SetStderr sets Stderr

func (*NoOpCmd) SetStdin

func (no *NoOpCmd) SetStdin(stdin io.Reader) Cmd

SetStdin sets Stdin reader, will not be used

func (*NoOpCmd) SetStdout

func (no *NoOpCmd) SetStdout(stdout io.Writer) Cmd

SetStdout sets Stdout

type NoOpCmder

type NoOpCmder struct{}

NoOpCmder only writes down all executed commands as output used for testing only

func (*NoOpCmder) Command

func (n *NoOpCmder) Command(cmd string, args ...string) Cmd

Command initializes a NoOpCmd with given arguments

func (*NoOpCmder) CommandContext

func (n *NoOpCmder) CommandContext(ctx context.Context, cmd string, args ...string) Cmd

CommandContext initializes a NoOpCmd with given arguments

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

func RunErrorForError(err error) *RunError

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

func (*RunError) Cause

func (e *RunError) Cause() error

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

func (*RunError) Error

func (e *RunError) Error() string

func (*RunError) PrettyCommand

func (e *RunError) PrettyCommand() string

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL