...

Package command

import "sigs.k8s.io/release-utils/command"
Overview
Index

Overview ▾

Index ▾

func Available(commands ...string) (ok bool)
func Execute(cmd string, args ...string) error
func GetGlobalVerbose() bool
func SetGlobalVerbose(to bool)
type Command
    func New(cmd string, args ...string) *Command
    func NewWithWorkDir(workDir, cmd string, args ...string) *Command
    func (c *Command) Add(cmd string, args ...string) Commands
    func (c *Command) AddErrorWriter(writer io.Writer) *Command
    func (c *Command) AddOutputWriter(writer io.Writer) *Command
    func (c *Command) AddWriter(writer io.Writer) *Command
    func (c *Command) Env(env ...string) *Command
    func (c *Command) Filter(regex, replaceAll string) (*Command, error)
    func (c *Command) Pipe(cmd string, args ...string) *Command
    func (c *Command) Run() (res *Status, err error)
    func (c *Command) RunSilent() (res *Status, err error)
    func (c *Command) RunSilentSuccess() error
    func (c *Command) RunSilentSuccessOutput() (output *Stream, err error)
    func (c *Command) RunSuccess() error
    func (c *Command) RunSuccessOutput() (output *Stream, err error)
    func (c *Command) String() string
    func (c *Command) Verbose() *Command
type Commands
    func (c Commands) Add(cmd string, args ...string) Commands
    func (c Commands) Run() (*Status, error)
type Status
    func (s *Status) ExitCode() int
    func (s *Status) Success() bool
type Stream
    func (s *Stream) Error() string
    func (s *Stream) Output() string
    func (s *Stream) OutputTrimNL() string

Package files

command.go global.go

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

type Command

A generic command abstraction

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

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

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

Commands is an abstraction over multiple Command structures

type Commands []*Command

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

A generic command exit status

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

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

Stream combines standard output and error

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

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.