cmd package - github.com/commander-cli/cmd - Go Packages
- func CaptureStandardOutput(f func() interface{}) (string, interface{})
- func WithCustomBaseCommand(baseCommand *exec.Cmd) func(c *Command)
- func WithCustomStderr(writers ...io.Writer) func(c *Command)
- func WithCustomStdout(writers ...io.Writer) func(c *Command)
- func WithEnvironmentVariables(env EnvVars) func(c *Command)
- func WithInheritedEnvironment(env EnvVars) func(c *Command)
- func WithStandardStreams(c *Command)
- func WithTimeout(t time.Duration) func(c *Command)
- func WithUser(credential syscall.Credential) func(c *Command)
- func WithWorkingDir(dir string) func(c *Command)
- func WithoutTimeout(c *Command)
- type Command
- func (c *Command) AddEnv(key, value string)
- func (c *Command) Combined() string
- func (c *Command) Execute() error
- func (c *Command) ExecuteContext(ctx context.Context) error
- func (c *Command) Executed() bool
- func (c *Command) ExitCode() int
- func (c *Command) Stderr() string
- func (c *Command) Stdout() string
- type CommandInterface
- type EnvVars
This section is empty.
This section is empty.
func CaptureStandardOutput ¶
func CaptureStandardOutput(f func() interface{}) (string, interface{})
CaptureStandardOutput allows to capture the output which will be written to os.Stdout and os.Stderr. It returns the captured output and the return value of the called function
func WithCustomBaseCommand ¶ added in v1.5.0
WithCustomBaseCommand allows the OS specific generated baseCommand to be overridden by an *os/exec.Cmd.
Example:
c := cmd.NewCommand(
"echo hello",
cmd.WithCustomBaseCommand(exec.Command("/bin/bash", "-c")),
)
c.Execute()
WithCustomStderr allows to add custom writers to stderr
WithCustomStdout allows to add custom writers to stdout
WithEnvironmentVariables sets environment variables for the executed command
WithInheritedEnvironment uses the env from the current process and allow to add more variables.
func WithStandardStreams ¶
func WithStandardStreams(c *Command)
WithStandardStreams is used as an option by the NewCommand constructor function and writes the output streams to stderr and stdout of the operating system
Example:
c := cmd.NewCommand("echo hello", cmd.WithStandardStreams)
c.Execute()
WithTimeout sets the timeout of the command
Example:
cmd.NewCommand("sleep 10;", cmd.WithTimeout(500))
WithUser allows the command to be run as a different user.
Example:
cred := syscall.Credential{Uid: 1000, Gid: 1000}
c := NewCommand("echo hello", cred)
c.Execute()
func WithoutTimeout(c *Command)
WithoutTimeout disables the timeout for the command
type Command ¶
Command represents a single command which can be executed
func NewCommand ¶
NewCommand creates a new command You can add option with variadic option argument Default timeout is set to 30 minutes
Example:
c := cmd.NewCommand("echo hello", function (c *Command) {
c.WorkingDir = "/tmp"
})
c.Execute()
or you can use existing options functions
c := cmd.NewCommand("echo hello", cmd.WithStandardStreams)
c.Execute()
func (*Command) AddEnv ¶
AddEnv adds an environment variable to the command If a variable gets passed like ${VAR_NAME} the env variable will be read out by the current shell
func (*Command) Combined ¶
Combined returns the combined output of stderr and stdout according to their timeline
func (*Command) Execute ¶
Execute executes the command and writes the results into it's own instance The results can be received with the Stdout(), Stderr() and ExitCode() methods
func (*Command) ExecuteContext ¶ added in v1.4.0
ExecuteContext runs Execute but with Context
EnvVars represents a map where the key is the name of the env variable and the value is the value of the variable
Example:
env := map[string]string{"ENV": "VALUE"}