command

package
v0.8.2 Latest Latest
Warning

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

Go to latest
Published: May 8, 2024 License: Apache-2.0 Imports: 11 Imported by: 32

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Available

func Available(commands ...string) (ok bool)

Available verifies that the specified `commands` are available within the current `$PATH` environment and returns true if so. The function does not check for duplicates nor if the provided slice is empty.

func Execute

func Execute(cmd string, args ...string) error

Execute is a convenience function which creates a new Command, executes it and evaluates its status.

func GetGlobalVerbose

func GetGlobalVerbose() bool

GetGlobalVerbose returns the globally set command verbosity

func SetGlobalVerbose

func SetGlobalVerbose(to bool)

SetGlobalVerbose sets the global command verbosity to the specified value

Types

type Command

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

A generic command abstraction

func New

func New(cmd string, args ...string) *Command

New creates a new command from the provided arguments.

func NewWithWorkDir

func NewWithWorkDir(workDir, cmd string, args ...string) *Command

NewWithWorkDir creates a new command from the provided workDir and the command arguments.

func (*Command) Add

func (c *Command) Add(cmd string, args ...string) Commands

Add a command with the same working directory as well as verbosity mode. Returns a new Commands instance.

func (*Command) AddErrorWriter

func (c *Command) AddErrorWriter(writer io.Writer) *Command

AddErrorWriter can be used to add an additional error (stderr) writer to the command, for example when having the need to log to files.

func (*Command) AddOutputWriter

func (c *Command) AddOutputWriter(writer io.Writer) *Command

AddOutputWriter can be used to add an additional output (stdout) writer to the command, for example when having the need to log to files.

func (*Command) AddWriter

func (c *Command) AddWriter(writer io.Writer) *Command

AddWriter can be used to add an additional output (stdout) and error (stderr) writer to the command, for example when having the need to log to files.

func (*Command) Env

func (c *Command) Env(env ...string) *Command

Env specifies the environment added to the command. Each entry is of the form "key=value". The environment of the current process is being preserved, while it is possible to overwrite already existing environment variables.

func (*Command) Filter added in v0.3.0

func (c *Command) Filter(regex, replaceAll string) (*Command, error)

Filter adds an output filter regular expression to the command. Every output will then be replaced with the string provided by replaceAll.

func (*Command) Pipe

func (c *Command) Pipe(cmd string, args ...string) *Command

Pipe creates a new command where the previous should be piped to

func (*Command) Run

func (c *Command) Run() (res *Status, err error)

Run starts the command and waits for it to finish. It returns an error if the command execution was not possible at all, otherwise the Status. This method prints the commands output during execution

func (*Command) RunSilent

func (c *Command) RunSilent() (res *Status, err error)

Run starts the command and waits for it to finish. It returns an error if the command execution was not possible at all, otherwise the Status. This method does not print the output of the command during its execution.

func (*Command) RunSilentSuccess

func (c *Command) RunSilentSuccess() error

RunSilentSuccess starts the command and waits for it to finish. It returns an error if the command execution was not successful. This method does not print the output of the command during its execution.

func (*Command) RunSilentSuccessOutput

func (c *Command) RunSilentSuccessOutput() (output *Stream, err error)

RunSilentSuccessOutput starts the command and waits for it to finish. It returns an error if the command execution was not successful, otherwise its output. This method does not print the output of the command during its execution.

func (*Command) RunSuccess

func (c *Command) RunSuccess() error

RunSuccess starts the command and waits for it to finish. It returns an error if the command execution was not successful.

func (*Command) RunSuccessOutput

func (c *Command) RunSuccessOutput() (output *Stream, err error)

RunSuccessOutput starts the command and waits for it to finish. It returns an error if the command execution was not successful, otherwise its output.

func (*Command) String

func (c *Command) String() string

String returns a string representation of the full command

func (*Command) Verbose

func (c *Command) Verbose() *Command

Verbose enables verbose output aka printing the command before executing it.

type Commands

type Commands []*Command

Commands is an abstraction over multiple Command structures

func (Commands) Add

func (c Commands) Add(cmd string, args ...string) Commands

Add adds another command with the same working directory as well as verbosity mode to the Commands.

func (Commands) Run

func (c Commands) Run() (*Status, error)

Run executes all commands sequentially and abort if any of those fails.

type Status

type Status struct {
	*Stream
	// contains filtered or unexported fields
}

A generic command exit status

func (*Status) ExitCode

func (s *Status) ExitCode() int

ExitCode returns the exit status of the command status

func (*Status) Success

func (s *Status) Success() bool

Success returns if a Status was successful

type Stream

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

Stream combines standard output and error

func (*Stream) Error

func (s *Stream) Error() string

Error returns the stderr of the command status

func (*Stream) Output

func (s *Stream) Output() string

Output returns stdout of the command status

func (*Stream) OutputTrimNL

func (s *Stream) OutputTrimNL() string

OutputTrimNL returns stdout of the command status with newlines trimmed Use only when output is expected to be a single "word", like a version string.

Jump to

Keyboard shortcuts

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