executils

package
v0.35.3 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2024 License: GPL-3.0 Imports: 10 Imported by: 8

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NullReader = &nullReader{}

NullReader is an io.Reader that will always return EOF

View Source
var NullWriter = &nullWriter{}

NullWriter is an io.Writer that discards any output

View Source
var PrintToStderr = &printToStderr{}

PrintToStderr is an OutputListener that outputs messages to standard error

View Source
var PrintToStdout = &printToStdout{}

PrintToStdout is an OutputListener that outputs messages to standard output

Functions

func AttachStderrListener

func AttachStderrListener(cmd *exec.Cmd, listener OutputListener) error

AttachStderrListener adds an OutputListener to the stderr of the process

func AttachStdoutListener

func AttachStdoutListener(cmd *exec.Cmd, listener OutputListener) error

AttachStdoutListener adds an OutputListener to the stdout of the process

func PipeCommands

func PipeCommands(commands ...*exec.Cmd) ([]byte, error)

PipeCommands executes the commands received as input by feeding the output of one to the input of the other, exactly like Unix Pipe (|). Returns the output of the final command and the eventual error.

code inspired by https://gist.github.com/tyndyll/89fbb2c2273f83a074dc

func TellCommandNotToSpawnShell

func TellCommandNotToSpawnShell(cmd *exec.Cmd)

TellCommandNotToSpawnShell avoids that the specified Cmd display a small command prompt while runnning on Windows. It has no effects on other OS.

Types

type OutputListener

type OutputListener interface {
	Output(msg string)
}

OutputListener is a callback interface to receive output messages from process

type Process

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

Process is representation of an external process run

func NewProcess

func NewProcess(extraEnv []string, args ...string) (*Process, error)

NewProcess creates a command with the provided command line arguments and environment variables (that will be added to the parent os.Environ). The argument args[0] is the path to the executable, the remainder are the arguments to the command.

func NewProcessFromPath

func NewProcessFromPath(extraEnv []string, executable *paths.Path, args ...string) (*Process, error)

NewProcessFromPath creates a command from the provided executable path, additional environment vars (in addition to the system default ones) and command line arguments.

func (*Process) GetArgs

func (p *Process) GetArgs() []string

GetArgs returns the command arguments

func (*Process) GetDir

func (p *Process) GetDir() string

GetDir gets the working directory of the command.

func (*Process) Kill

func (p *Process) Kill() error

Kill causes the Process to exit immediately. Kill does not wait until the Process has actually exited. This only kills the Process itself, not any other processes it may have started.

func (*Process) RedirectStderrTo

func (p *Process) RedirectStderrTo(out io.Writer)

RedirectStderrTo will redirect the process' stdout to the specified writer. Any previous redirection will be overwritten.

func (*Process) RedirectStdoutTo

func (p *Process) RedirectStdoutTo(out io.Writer)

RedirectStdoutTo will redirect the process' stdout to the specified writer. Any previous redirection will be overwritten.

func (*Process) Run

func (p *Process) Run() error

Run starts the specified command and waits for it to complete.

func (*Process) RunAndCaptureOutput

func (p *Process) RunAndCaptureOutput(ctx context.Context) ([]byte, []byte, error)

RunAndCaptureOutput starts the specified command and waits for it to complete. If the given context is canceled before the normal process termination, the process is killed. The standard output and standard error of the process are captured and returned at process termination.

func (*Process) RunWithinContext

func (p *Process) RunWithinContext(ctx context.Context) error

RunWithinContext starts the specified command and waits for it to complete. If the given context is canceled before the normal process termination, the process is killed.

func (*Process) SetDir

func (p *Process) SetDir(dir string)

SetDir sets the working directory of the command. If Dir is the empty string, Run runs the command in the calling process's current directory.

func (*Process) SetDirFromPath

func (p *Process) SetDirFromPath(path *paths.Path)

SetDirFromPath sets the working directory of the command. If path is nil, Run runs the command in the calling process's current directory.

func (*Process) SetEnvironment

func (p *Process) SetEnvironment(values []string)

SetEnvironment set the environment for the running process. Each entry is of the form "key=value". System default environments will be wiped out.

func (*Process) Signal

func (p *Process) Signal(sig os.Signal) error

Signal sends a signal to the Process. Sending Interrupt on Windows is not implemented.

func (*Process) Start

func (p *Process) Start() error

Start will start the underliyng process.

func (*Process) StderrPipe

func (p *Process) StderrPipe() (io.ReadCloser, error)

StderrPipe returns a pipe that will be connected to the command's standard error when the command starts.

Wait will close the pipe after seeing the command exit, so most callers don't need to close the pipe themselves. It is thus incorrect to call Wait before all reads from the pipe have completed. For the same reason, it is incorrect to use Run when using StderrPipe.

func (*Process) StdinPipe

func (p *Process) StdinPipe() (io.WriteCloser, error)

StdinPipe returns a pipe that will be connected to the command's standard input when the command starts. The pipe will be closed automatically after Wait sees the command exit. A caller need only call Close to force the pipe to close sooner. For example, if the command being run will not exit until standard input is closed, the caller must close the pipe.

func (*Process) StdoutPipe

func (p *Process) StdoutPipe() (io.ReadCloser, error)

StdoutPipe returns a pipe that will be connected to the command's standard output when the command starts.

Wait will close the pipe after seeing the command exit, so most callers don't need to close the pipe themselves. It is thus incorrect to call Wait before all reads from the pipe have completed. For the same reason, it is incorrect to call Run when using StdoutPipe.

func (*Process) Wait

func (p *Process) Wait() error

Wait waits for the command to exit and waits for any copying to stdin or copying from stdout or stderr to complete.

Jump to

Keyboard shortcuts

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