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(cmd string, args ...string) error
Execute is a convenience function which creates a new Command, executes it and evaluates its status.
func GetGlobalVerbose() bool
GetGlobalVerbose returns the globally set command verbosity
func SetGlobalVerbose(to bool)
SetGlobalVerbose sets the global command verbosity to the specified value
A generic command abstraction
type Command struct {
// contains filtered or unexported fields
}
func New(cmd string, args ...string) *Command
New creates a new command from the provided arguments.
func NewWithWorkDir(workDir, cmd string, args ...string) *Command
NewWithWorkDir creates a new command from the provided workDir and the command arguments.
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 (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 (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 (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 (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 (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 (c *Command) Pipe(cmd string, args ...string) *Command
Pipe creates a new command where the previous should be piped to
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 (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 (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 (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 (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 (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 (c *Command) String() string
String returns a string representation of the full command
func (c *Command) Verbose() *Command
Verbose enables verbose output aka printing the command before executing it.
Commands is an abstraction over multiple Command structures
type Commands []*Command
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 (c Commands) Run() (*Status, error)
Run executes all commands sequentially and abort if any of those fails.
A generic command exit status
type Status struct { *Stream // contains filtered or unexported fields }
func (s *Status) ExitCode() int
ExitCode returns the exit status of the command status
func (s *Status) Success() bool
Success returns if a Status was successful
Stream combines standard output and error
type Stream struct {
// contains filtered or unexported fields
}
func (s *Stream) Error() string
Error returns the stderr of the command status
func (s *Stream) Output() string
Output returns stdout of the command status
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.