exec

package
v0.0.0-...-6cd82ae Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewExitError

func NewExitError(
	exitErr exec.ExitError,
	cmd string,
	stdOut string,
	stdErr string,
	outputAvailable bool) error

func RedactSensitiveArgs

func RedactSensitiveArgs(args []string, sensitiveDataMatch []string) []string

func RedactSensitiveData

func RedactSensitiveData(msg string) string

func TestAbortSignalRetainsOutputError

func TestAbortSignalRetainsOutputError(t *testing.T)

Types

type CmdTree

type CmdTree struct {
	CmdTreeOptions
	*exec.Cmd
}

CmdTree represents an `exec.Cmd` run inside a process group. When `Kill` is called, SIGKILL is sent to the process group, which will kill any lingering child processes launched by the root process.

func (*CmdTree) Kill

func (o *CmdTree) Kill()

func (*CmdTree) Start

func (o *CmdTree) Start() error

type CmdTreeOptions

type CmdTreeOptions struct {
	Interactive bool
}

Settings to modify the way CmdTree is executed

type CommandRunner

type CommandRunner interface {
	Run(ctx context.Context, args RunArgs) (RunResult, error)
	RunList(ctx context.Context, commands []string, args RunArgs) (RunResult, error)
}

CommandRunner exposes the contract for executing console/shell commands for the specified runArgs

func NewCommandRunner

func NewCommandRunner(opt *RunnerOptions) CommandRunner

Creates a new default instance of the CommandRunner. Passing nil will use the default values for RunnerOptions.

These options will be used by default during interactive commands unless specifically overridden within the command run arguments.

type ExitError

type ExitError struct {
	// The path or name of the command being invoked.
	Cmd string
	// The exit code of the command.
	ExitCode int
	// contains filtered or unexported fields
}

ExitError is the error returned when a command unsuccessfully exits.

func (*ExitError) Error

func (e *ExitError) Error() string

Error augments the underlying exec.ExitError's Error with the stdout and stderr output of the command, if available.

type RunArgs

type RunArgs struct {
	Cmd  string
	Args []string
	// Any string from SensitiveData will be redacted as *** if found in Args
	SensitiveData []string
	Cwd           string
	Env           []string

	// Stderr will receive a copy of the text written to Stderr by
	// the command.
	// NOTE: RunResult.Stderr will still contain stderr output.
	Stderr io.Writer

	// Enables debug logging.
	DebugLogging *bool

	// When set will run the command within a shell
	UseShell bool

	// When set will attach commands to std input/output
	Interactive bool

	// When set will call the command with the specified StdIn
	StdIn io.Reader

	// When set will call the command with the specified StdOut
	StdOut io.Writer
}

RunArgs exposes the command, arguments and other options when running console/shell commands

func NewRunArgs

func NewRunArgs(cmd string, args ...string) RunArgs

NewRunArgs creates a new instance with the specified cmd and args

func NewRunArgsWithSensitiveData

func NewRunArgsWithSensitiveData(cmd string, args, sensitiveData []string) RunArgs

NewRunArgs creates a new instance with the specified cmd and args and a list of SensitiveData Use this constructor to protect known sensitive data from going to logs

func (RunArgs) AppendParams

func (b RunArgs) AppendParams(params ...string) RunArgs

Appends additional command params

func (RunArgs) WithCwd

func (b RunArgs) WithCwd(cwd string) RunArgs

Updates the current working directory (cwd) for the command

func (RunArgs) WithDebugLogging

func (b RunArgs) WithDebugLogging(debug bool) RunArgs

Updates whether or not debug output will be written to default logger

func (RunArgs) WithEnv

func (b RunArgs) WithEnv(env []string) RunArgs

Updates the environment variables to used for the command

func (RunArgs) WithInteractive

func (b RunArgs) WithInteractive(interactive bool) RunArgs

Updates whether or not this will be an interactive commands Interactive command sets stdin, stdout & stderr to the OS console/terminal

func (RunArgs) WithShell

func (b RunArgs) WithShell(useShell bool) RunArgs

Updates whether or not this will be run in a shell

func (RunArgs) WithStdErr

func (b RunArgs) WithStdErr(stdErr io.Writer) RunArgs

Updates the stderr writer that will be used while invoking the command

func (RunArgs) WithStdIn

func (b RunArgs) WithStdIn(stdIn io.Reader) RunArgs

Updates the stdin reader that will be used while invoking the command

func (RunArgs) WithStdOut

func (b RunArgs) WithStdOut(stdOut io.Writer) RunArgs

Updates the stdout writer that will be used while invoking the command

type RunResult

type RunResult struct {
	// The exit code of the command.
	ExitCode int
	// The stdout output captured from running the command.
	Stdout string
	// The stderr output captured from running the command.
	Stderr string
}

RunResult is the result of running a command.

func NewRunResult

func NewRunResult(code int, stdout, stderr string) RunResult

type RunnerOptions

type RunnerOptions struct {
	// Stdin is the input stream. If nil, os.Stdin is used.
	Stdin io.Reader
	// Stdout is the output stream. If nil, os.Stdout is used.
	Stdout io.Writer
	// Stderr is the error stream. If nil, os.Stderr is used.
	Stderr io.Writer
	// Whether debug logging is enabled. False by default.
	DebugLogging bool
}

Jump to

Keyboard shortcuts

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