exec

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2022 License: MIT Imports: 10 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(cmdStr string) (result string)

Run creates and runs a process and waits for its result (combined stdin,stderr) returned as a string value. This is equivalent to calling Proc.RunProc() followed by Proc.Result().

Types

type CommandBuilder added in v0.1.1

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

func Commands added in v0.1.1

func Commands(cmds ...string) *CommandBuilder

Commands creates a *CommandBuilder used to collect command strings to be executed.

func (*CommandBuilder) Add added in v0.1.1

func (cb *CommandBuilder) Add(cmds ...string) *CommandBuilder

Add adds a new command string to the builder

func (*CommandBuilder) Concurr added in v0.2.0

func (cb *CommandBuilder) Concurr() *CommandResult

Concurr starts all processes concurrently and does not wait for the commands to complete. It is equivalent to Commands(...).WithPolicy(ConcurrentExecPolicy).Start().

func (*CommandBuilder) Pipe added in v0.2.0

func (cb *CommandBuilder) Pipe() *PipedCommandResult

Pipe executes each command serially chaining the combinedOutput of previous command to the inputPipe of next command.

func (*CommandBuilder) Run added in v0.1.1

func (cb *CommandBuilder) Run() *CommandResult

Run executes all commands successively and waits for all of the result. The result of each individual command can be accessed from CommandResult.Procs[] after the execution completes. If policy == ExitOnErrPolicy, the execution will stop on the first error encountered, otherwise it will continue. Processes with errors can be accessed from CommandResult.ErrProcs.

func (*CommandBuilder) Start added in v0.1.1

func (cb *CommandBuilder) Start() *CommandResult

Start starts all processes sequentially by default, or concurrently if ConcurrentExecPolicy is set, and does not wait for the commands to complete. Use CommandResult.Wait to wait for the processes to complete. Then, the result of each command can be accessed from CommandResult.Procs[] or CommandResult.ErrProcs to access failed processses. If policy == ExitOnErrPolicy, the execution will halt on the first error encountered, otherwise it will continue.

func (*CommandBuilder) WithPolicy added in v0.1.1

func (cb *CommandBuilder) WithPolicy(policyMask CommandPolicy) *CommandBuilder

WithPolicy sets one or more command policy mask values, i.e. (CmdOnErrContinue | CmdExecConcurrent)

type CommandPolicy added in v0.1.1

type CommandPolicy byte
const (
	ExitOnErrPolicy CommandPolicy = 1 << iota
	ConcurrentExecPolicy
)

type CommandResult added in v0.2.0

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

CommandResult stores results of executed commands using the CommandBuilder

func (*CommandResult) ErrProcs added in v0.2.0

func (cr *CommandResult) ErrProcs() []*Proc

func (*CommandResult) Procs added in v0.2.0

func (cr *CommandResult) Procs() []*Proc

func (*CommandResult) Wait added in v0.2.0

func (cr *CommandResult) Wait() *CommandResult

type PipedCommandResult added in v0.2.0

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

func (*PipedCommandResult) ErrProcs added in v0.2.0

func (cr *PipedCommandResult) ErrProcs() []*Proc

func (*PipedCommandResult) LastProc added in v0.2.0

func (cr *PipedCommandResult) LastProc() *Proc

func (*PipedCommandResult) Procs added in v0.2.0

func (cr *PipedCommandResult) Procs() []*Proc

type Proc

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

Proc stores process info when running a process

func NewProc added in v0.1.1

func NewProc(cmdStr string) *Proc

NewProc sets up command string to be started as an OS process, however does not start the process.

func RunProc

func RunProc(cmdStr string) *Proc

RunProc starts a new process and waits for its completion. Use Proc.Out() or Proc.Result() to access the combined result from stdout and stderr.

func StartProc

func StartProc(cmdStr string) *Proc

StartProc starts an OS process (setup a combined output of stdout, stderr) and does not wait for it to complete. You must follow this with either proc.Wait() to wait for result directly. Otherwise, call proc.Out() or proc.Result() which automatically waits and gather result.

func (*Proc) Command

func (p *Proc) Command() *osexec.Cmd

Command returns the os/exec.Cmd that started the process

func (*Proc) Err

func (p *Proc) Err() error

Err returns any execution error

func (*Proc) ExitCode

func (p *Proc) ExitCode() int

ExitCode returns process exit code

func (*Proc) Exited

func (p *Proc) Exited() bool

Exited returns true if process exits ok

func (*Proc) GetErrorPipe added in v0.2.0

func (p *Proc) GetErrorPipe() io.Reader

GetErrorPipe returns a stream where the process error can be read from

func (*Proc) GetInputPipe added in v0.2.0

func (p *Proc) GetInputPipe() io.Writer

GetInputPipe returns a stream where the process input can be written to

func (*Proc) GetOutputPipe added in v0.2.0

func (p *Proc) GetOutputPipe() io.Reader

GetOutputPipe returns a stream where the process output can be read from

func (*Proc) ID

func (p *Proc) ID() int

ID returns process id

func (*Proc) IsSuccess

func (p *Proc) IsSuccess() bool

IsSuccess returns true if proc exit ok

func (*Proc) Kill

func (p *Proc) Kill() *Proc

Kill halts the process

func (*Proc) Out

func (p *Proc) Out() io.Reader

Out waits, after StartProc or Proc.Start has been called, for the cmd to complete and returns the combined result (Stdout and Stderr) as a single reader to be streamed.

func (*Proc) Peek

func (p *Proc) Peek() *Proc

Peek attempts to read process state information

func (*Proc) Result

func (p *Proc) Result() string

Result waits, after proc.Start or proc.StartProc has been called, for the cmd to complete and returns the combined stdout and stderr result as a string value.

func (*Proc) Run added in v0.2.0

func (p *Proc) Run() *Proc

Run starts and wait for a process to complete. Before calling p.Run(), setup proper access to the process' input/output (i.e. stdin,stdout, stderr)

func (*Proc) SetStderr added in v0.2.0

func (p *Proc) SetStderr(out io.Writer)

SetStderr sets a stream where the process can write its errors to

func (*Proc) SetStdin added in v0.2.0

func (p *Proc) SetStdin(in io.Reader)

SetStdin sets a stream for the process to read its input from

func (*Proc) SetStdout added in v0.2.0

func (p *Proc) SetStdout(out io.Writer)

SetStdout sets a stream where the process can write its output to

func (*Proc) Start added in v0.1.1

func (p *Proc) Start() *Proc

Start starts the associated command as an OS process and does not wait for its result. Ensure proper access to the process' input/output (stdin,stdout,stderr) has been setup prior to calling p.Start(). Use p.Err() to access any error that may have occured during execution.

func (*Proc) Stderr added in v0.2.0

func (p *Proc) Stderr() io.Writer

Stderr returns the standard error stream for the process

func (*Proc) Stdin added in v0.2.0

func (p *Proc) Stdin() io.Reader

Stdin returns the standard input stream for the process

func (*Proc) Stdout added in v0.2.0

func (p *Proc) Stdout() io.Writer

Stdout returns the standard output stream for the process

func (*Proc) SysTime

func (p *Proc) SysTime() time.Duration

SysTime returns proc system cpu time

func (*Proc) UserTime

func (p *Proc) UserTime() time.Duration

UserTime returns proc user cpu time

func (*Proc) Wait

func (p *Proc) Wait() *Proc

Wait waits for a process to complete (in a separate goroutine). Ensure p.Start() has been called prior to calling p.Wait()

Jump to

Keyboard shortcuts

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