run

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultRunTimeout = time.Minute

DefaultRunTimeout is how long we'll wait for commands to run in the case where the context doesn't already have a timeout. This was chosen arbitrarily.

Variables

This section is empty.

Functions

func Many

func Many(ctx context.Context, args ...[]string) (stdouts, stderrs []string, _ error)

Many calls Simple for each command in args. If any command returns error, then no further commands will be run, and that error will be returned. For any commands that were actually executed (not aborted by a previous error), their stdout and stderr will be returned. It's guaranteed that len(stdouts)==len(stderrs).

func Run

func Run(ctx context.Context, opts []*Option, args ...string) (exitCode int, _ error)

Run runs the command provided by args, using the options in opts. By default, if the command returns a nonzero exit code, an error is returned, but this behavior may be overridden by the AllowNonzeroExit option.

If the incoming context doesn't already have a timeout, then a default timeout will be added (see DefaultRunTimeout).

If the command fails, the error message will include the contents of stdout and stderr. This saves boilerplate in the caller.

The input args must have len>=1. opts may be nil if no special options are needed.

This doesn't execute a shell (unless of course args[0] is the name of a shell binary).

func RunDiff

func RunDiff(ctx context.Context, color bool, file1, file1RelTo, file2, file2RelTo string) (_ string, outErr error)

RunDiff execs the diff binary. Returns len > 0 if there's a diff. Returns unified diff format. Returns empty string if neither file exists (this differs from the normal behavior of `/bin/diff -N`, which returns an error if both files are absent).

For the purposes of determining the paths in the returned diff, we use the relative path of file1 relative to file1RelTo, and the same for file1. So if file1 is "/x/y/z.tzt" and file1RelTo is "/x", then the filename label in the returned diff will be "y/z.txt".

func Simple

func Simple(ctx context.Context, args ...string) (stdout, stderr string, _ error)

Simple is a wrapper around Run that captures stdout and stderr as strings. This is intended to be used for commands that run non-interactively then exit.

If the command exits with a nonzero status code, an *os.ExitError will be returned.

If the command fails, the error message will include the contents of stdout and stderr. This saves boilerplate in the caller.

Types

type Option

type Option struct {
	// contains filtered or unexported fields
}

Option implements the functional options pattern for Run.

func AllowNonzeroExit

func AllowNonzeroExit() *Option

AllowNonzeroExit is an option that will NOT treat a nonzero exit code from the command as an error (so Run won't return error). The default behavior of Run is that if a command exits with a nonzero status code, then that becomes a Go error.

func WithCwd

func WithCwd(cwd string) *Option

WithCwd runs the command in the given working directory.

func WithStderr

func WithStderr(stderr io.Writer) *Option

WithStderr writes the command's standard error to the given writer.

func WithStdin

func WithStdin(stdin io.Reader) *Option

WithStdin passes the given reader as the command's standard input.

func WithStdinStr

func WithStdinStr(stdin string) *Option

WithStdinStr is a convenient wrapper around WithStdin that passes the given string as the command's standard input.

func WithStdout

func WithStdout(stdout io.Writer) *Option

WithStdout writes the command's standard output to the given writer.

Jump to

Keyboard shortcuts

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