exec

package
v4.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2024 License: LGPL-3.0 Imports: 13 Imported by: 10

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCancelled = errors.New("command cancelled")

ErrCancelled is returned by WaitWithCancel in case it successfully manages to kill the running process.

Functions

func KillProcess

func KillProcess(proc *os.Process) error

KillProcess tries to kill the process being ran by RunParams We need this convoluted implementation because everything ran under the bash script is spawned as a different process and doesn't get killed by a regular process.Kill() For details see https://groups.google.com/forum/#!topic/golang-nuts/XoQ3RhFBJl8

Types

type ExecResponse

type ExecResponse struct {
	Code   int
	Stdout []byte
	Stderr []byte
}

ExecResponse contains the return code and output generated by executing a command.

func RunCommands

func RunCommands(run RunParams) (*ExecResponse, error)

RunCommands executes the Commands specified in the RunParams using powershell on windows, and '/bin/bash -s' on everything else, passing the commands through as stdin, and collecting stdout and stderr. If a non-zero return code is returned, this is collected as the code for the response and this does not classify as an error.

type RunParams

type RunParams struct {
	Commands    string
	WorkingDir  string
	Environment []string
	Clock       clock.Clock
	KillProcess func(*os.Process) error
	User        string
	// contains filtered or unexported fields
}

Parameters for RunCommands. Commands contains one or more commands to be executed using bash or PowerShell. If WorkingDir is set, this is passed through. Similarly if the Environment is specified, this is used for executing the command. TODO: refactor this to use a config struct and a constructor. Remove todo and extra code from WaitWithCancel once this is done.

func (*RunParams) Process

func (r *RunParams) Process() *os.Process

Process returns the *os.Process instance of the current running process This will allow us to kill the process if needed, or get more information on the process

func (*RunParams) Run

func (r *RunParams) Run() error

Run sets up the command environment (environment variables, working dir) and starts the process. The commands are passed into bash on Linux machines and to powershell on Windows machines.

func (*RunParams) Wait

func (r *RunParams) Wait() (*ExecResponse, error)

Wait blocks until the process exits, and returns an ExecResponse type containing stdout, stderr and the return code of the process. If a non-zero return code is returned, this is collected as the code for the response and this does not classify as an error.

func (*RunParams) WaitWithCancel

func (r *RunParams) WaitWithCancel(cancel <-chan struct{}) (*ExecResponse, error)

WaitWithCancel waits until the process exits or until a signal is sent on the cancel channel. In case a signal is sent it first tries to kill the process and return ErrCancelled. If it fails at killing the process it will return anyway and report the problematic PID.

Jump to

Keyboard shortcuts

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