A simple package to execute shell commands on linux, darwin and windows.
$ go get -u github.com/commander-cli/[email protected]
c := cmd.NewCommand("echo hello")
err := c.Execute()
if err != nil {
panic(err.Error())
}
fmt.Println(c.Stdout())
fmt.Println(c.Stderr())
To configure the command a option function will be passed which receives the command object as an argument passed by reference.
Default option functions:
cmd.WithStandardStreams
cmd.WithCustomStdout(...io.Writers)
cmd.WithCustomStderr(...io.Writers)
cmd.WithTimeout(time.Duration)
cmd.WithoutTimeout
cmd.WithWorkingDir(string)
cmd.WithEnvironmentVariables(cmd.EnvVars)
cmd.WithInheritedEnvironment(cmd.EnvVars)
c := cmd.NewCommand("echo hello", cmd.WithStandardStreams)
c.Execute()
setWorkingDir := func (c *Command) {
c.WorkingDir = "/tmp/test"
}
c := cmd.NewCommand("pwd", setWorkingDir)
c.Execute()
You can catch output streams to stdout
and stderr
with cmd.CaptureStandardOut
.
// caputred is the captured output from all executed source code
// fnResult contains the result of the executed function
captured, fnResult := cmd.CaptureStandardOut(func() interface{} {
c := NewCommand("echo hello", cmd.WithStandardStream)
err := c.Execute()
return err
})
// prints "hello"
fmt.Println(captured)
make test
- os.Stdout and os.Stderr output access after execution via
c.Stdout()
andc.Stderr()