executil

package
v1.11.4 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2022 License: MIT Imports: 22 Imported by: 15

Documentation

Overview

Utilities that make executing commands on the local system a little bit easier.

Index

Constants

This section is empty.

Variables

View Source
var PasswordPrompt = `Enter password: `
View Source
var PasswordVerifyPrompt = `Verify password: `
View Source
var PromptWriter io.Writer = os.Stdout

Functions

func Env added in v1.7.36

func Env(name string, fallback ...interface{}) string

func EnvBool added in v1.7.36

func EnvBool(name string, fallback ...interface{}) bool

func EnvDuration added in v1.7.36

func EnvDuration(name string, fallback ...interface{}) time.Duration

func EnvFloat added in v1.7.36

func EnvFloat(name string, fallback ...interface{}) float64

func EnvInt added in v1.7.36

func EnvInt(name string, fallback ...interface{}) int64

func EnvTime added in v1.7.36

func EnvTime(name string, fallback ...interface{}) time.Time

func FindShell added in v1.7.30

func FindShell() string

Uses environment variables and other configurations to attempt to locate the path to the user's shell.

func IsRoot added in v1.7.31

func IsRoot() bool

Returns whether the current user is root or not.

func Join added in v1.7.24

func Join(in interface{}) string

Take an *exec.Cmd or []string and return a shell-executable command line string.

func LogOutputFunc added in v1.8.40

func LogOutputFunc(line string, err bool)

func MustShellOut added in v1.8.39

func MustShellOut(cmdOrLine string, args ...interface{}) []byte

A panicky version of ShellOut.

func MustSplit added in v1.8.68

func MustSplit(cmd string) []string

Same as Split, but panics if there is an error.

func PromptPassword added in v1.8.29

func PromptPassword(writer io.Writer, fd int, verify bool) (string, bool)

Generic password prompt that takes the input file descriptor and verify flag as options.

func ReadAndVerifyPassword added in v1.8.29

func ReadAndVerifyPassword() (string, bool)

Read a password from standard input, disabling echo and prompting twice. The second return argument is falseif the two passwords do not match.

func ReadPassword added in v1.8.29

func ReadPassword() string

Read a password from standard input, disabling echo.

func RootOr added in v1.7.31

func RootOr(ifRoot interface{}, notRoot interface{}) interface{}

Returns the first argument if the current user is root, and the second if not.

func RootOrString added in v1.7.31

func RootOrString(ifRoot interface{}, notRoot interface{}) string

The same as RootOr, but returns a string.

func ShellOut added in v1.8.39

func ShellOut(cmdOrLine string, args ...interface{}) ([]byte, error)

Run a command and return the standard output. If the first argument contains a command and its arguments, it will be executed in the user's shell using FindShell. Otherwise, the first argument will be treated as a command and the remaining arguments will be passed in parameterized.

func ShouldShellOut added in v1.8.39

func ShouldShellOut(cmdOrLine string, args ...interface{}) []byte

Attempts to call ShellOut, but will return nil if there is an error. Does not panic.

func ShouldSplit added in v1.8.68

func ShouldSplit(cmd string) []string

Same as Split, but silently discards any errors, returning an empty slice in this case.

func Split added in v1.8.68

func Split(cmd string) ([]string, error)

Splits the given string into words, honoring quoting and escaping conventions of common command line shells.

func TrapSignals added in v1.7.33

func TrapSignals(after func(sig os.Signal) bool, signals ...os.Signal)

Registers a list of OS signals to intercept and provides an opportunity to run a function before the program exits.

func Which added in v1.7.10

func Which(cmdname string, path ...string) string

Locates the first path containing the given command. The directories listed in the environment variable "PATH" will be checked, in order. If additional directories are specified in the path variadic argument, they will be checked first. If the command is not in any path, an empty string will be returned.

func WhichAll added in v1.7.10

func WhichAll(cmdname string, path ...string) []string

Locates the all paths containing the given command. The directories listed in the environment variable "PATH" will be checked, in order. If additional directories are specified in the path variadic argument, they will be checked first. If the command is not in any path, an empty slice will be returned.

Types

type Cmd

type Cmd struct {
	*exec.Cmd

	// An interval of time on which the command should be actively checked for run and exit status.
	MonitorInterval time.Duration

	// How long the command may run for before being killed.
	Timeout time.Duration

	// Whether the command invocation should inherit the environment variables of the calling process.
	InheritEnv bool

	// Called when immediately before the command is executed.
	OnStart CommandStatusFunc

	// Called whenever the monitor check is performed.
	OnMonitor CommandStatusFunc

	// Called when the command exits, regardless of success or failure.
	OnComplete CommandStatusFunc

	// Called when the command exits with a non-error status (code 0)
	OnSuccess CommandStatusFunc

	// Called when the command exits with an error status (non-zero exit code, security, invocation, or resource error)
	OnError CommandStatusFunc

	// Called when a line of standard output is written.
	OnStdout OutputLineFunc

	// Called when a line of standard error is written.
	OnStderr OutputLineFunc

	// If specified, this function will determine how to tokenize the stdout stream and when to call OnStdout.  Defaults to bufio.ScanLines.
	StdoutSplitFunc bufio.SplitFunc

	// If specified, this function will determine how to tokenize the stderr stream and when to call OnStderr.  Defaults to bufio.ScanLines.
	StderrSplitFunc bufio.SplitFunc

	// Specifies that the spawned process should inherit the same Process Group ID (PGID) as the parent.
	InheritParent bool
	// contains filtered or unexported fields
}

A wrapper for exec.Cmd that provides helpful callbacks and monitoring details that are challenging to implement.

func Command

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

func CommandContext

func CommandContext(ctx context.Context, name string, arg ...string) *Cmd

func ShellCommand added in v1.7.30

func ShellCommand(cmdline string) *Cmd

func Wrap

func Wrap(cmd *exec.Cmd) *Cmd

func (*Cmd) Close added in v1.10.6

func (self *Cmd) Close() error

Implements io.Closer, killing the underlying process, waiting for it to exit, then returning.

func (*Cmd) CloseInput added in v1.10.6

func (self *Cmd) CloseInput() error

Notify the command that no further standard input will be written.

func (*Cmd) CombinedOutput

func (self *Cmd) CombinedOutput() ([]byte, error)

func (*Cmd) Kill added in v1.7.30

func (self *Cmd) Kill() error

Kill the running command.

func (*Cmd) Output

func (self *Cmd) Output() ([]byte, error)

func (*Cmd) Read added in v1.10.6

func (self *Cmd) Read(p []byte) (int, error)

Implements io.Reader, sourcing data from the command's standard output. If the command is not already running, it will be started.

func (*Cmd) Run

func (self *Cmd) Run() error

func (*Cmd) SetEnv added in v1.7.30

func (self *Cmd) SetEnv(key string, value interface{})

func (*Cmd) Start

func (self *Cmd) Start() error

func (*Cmd) Status

func (self *Cmd) Status() Status

Return the current status of the process.

func (*Cmd) String added in v1.8.39

func (self *Cmd) String() string

func (*Cmd) WaitStatus

func (self *Cmd) WaitStatus() Status

Wait for the process to complete, then return the last status. Process must have been started using the Start() function.

func (*Cmd) Write added in v1.10.6

func (self *Cmd) Write(p []byte) (int, error)

Implements io.Writer, writing data to the commands standard input. If the command is not already running, it will be started.

type CommandStatusFunc

type CommandStatusFunc func(Status)

type OutputLineFunc added in v1.8.7

type OutputLineFunc func(string, bool)

func LogOutput added in v1.8.40

func LogOutput(outlevel log.Level, errlevel log.Level) OutputLineFunc

type Status

type Status struct {
	StartedAt  time.Time
	StoppedAt  time.Time
	Running    bool
	Successful bool
	ExitCode   int
	Error      error
	PID        int
	Cmd        *Cmd
}

func (Status) String

func (self Status) String() string

func (Status) Took

func (self Status) Took() time.Duration

Jump to

Keyboard shortcuts

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