exec

package
v3.4.2 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: GPL-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultLogFn is the default debug print function.
	DefaultLogFn PrintfFn
	// DefaultLogPrefix is the default prefix that is prepended to messages passed to the debugf function.
	DefaultLogPrefix = "exec: "
	// DefaultStderrColorFn is the default function that is used to colorize stderr output that is streamed to the log function.
	DefaultStderrColorFn = color.New(color.FgRed).SprintFunc()
)

Functions

This section is empty.

Types

type Cmd

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

Cmd represent a command that can be executed as new process.

func Command

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

Command creates a Cmd that executes the binary named name with the arguments args.

func (*Cmd) Directory

func (c *Cmd) Directory(dir string) *Cmd

Directory defines the directiory in which the command is executed.

func (*Cmd) Env

func (c *Cmd) Env(env []string) *Cmd

Env defines environment variables that are set during execution.

func (*Cmd) ExpectSuccess

func (c *Cmd) ExpectSuccess() *Cmd

ExpectSuccess when the command is executed and the execution of the process fails (e.g. exit status != 0 on unix) return an ExitCodeError instead of nil.

func (*Cmd) LogFn added in v3.1.0

func (c *Cmd) LogFn(fn PrintfFn) *Cmd

LogFn sets a Printf-style function as logger.

func (*Cmd) LogPrefix added in v3.1.0

func (c *Cmd) LogPrefix(prefix string) *Cmd

LogPrefix defines a string that is used as prefix for all messages written via *Cmd.LogFn.

func (*Cmd) Run

func (c *Cmd) Run(ctx context.Context) (*Result, error)

Run executes the command. If the command could not be started an error is returned. If the command was started successfully, and terminated unsuccessfully no error is returned, except if *Cmd.ExpectSuccess() was called before.

func (*Cmd) RunCombinedOut added in v3.1.0

func (c *Cmd) RunCombinedOut(ctx context.Context) (*ResultOut, error)

RunCombinedOut executes the command and stores the combined stdout and stderr output of the process in ResultOut.CombinedOutput.

func (*Cmd) Stderr added in v3.1.0

func (c *Cmd) Stderr(w io.Writer) *Cmd

Stdout streams the standard error output to w during execution.

func (*Cmd) Stdout added in v3.1.0

func (c *Cmd) Stdout(w io.Writer) *Cmd

Stdout streams the standard output to w during execution.

type ExitCodeError

type ExitCodeError struct {
	*Result
}

ExitCodeError is returned from Run() when a command exited with a code != 0.

func (*ExitCodeError) ColoredError added in v3.2.0

func (e *ExitCodeError) ColoredError(highlightFn SprintFn, errorFn SprintFn, withCmdOutput bool) string

func (*ExitCodeError) Error

func (e *ExitCodeError) Error() string

Error returns the error description.

type PrintfFn added in v3.1.0

type PrintfFn func(format string, a ...any)

type Result

type Result struct {
	Command  string
	Dir      string
	ExitCode int
	// contains filtered or unexported fields
}

Result describes the result of a run Cmd.

func (*Result) ExpectSuccess

func (r *Result) ExpectSuccess() error

ExpectSuccess the command did not execute successful (e.g. exit code != 0 on unix), a ExitCodeError is returned.

type ResultOut added in v3.1.0

type ResultOut struct {
	*Result
	CombinedOutput []byte
}

func (*ResultOut) StrOutput added in v3.1.0

func (r *ResultOut) StrOutput() string

type SprintFn added in v3.2.0

type SprintFn func(...any) string

Jump to

Keyboard shortcuts

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