Skip to content
/ cmd Public
forked from commander-cli/cmd

A simple package to execute shell commands on linux, windows and osx

License

Notifications You must be signed in to change notification settings

Phuong39/cmd

 
 

Repository files navigation

CI GoDoc Test Coverage Maintainability Go Report Card

cmd package

A simple package to execute shell commands on linux, darwin and windows.

Installation

$ go get -u github.com/commander-cli/cmd@v1.0.0

Usage

c := cmd.NewCommand("echo hello")

err := c.Execute()
if err != nil {
    panic(err.Error())    
}

fmt.Println(c.Stdout())
fmt.Println(c.Stderr())

Configure the command

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.WithCustomBaseCommand(*exec.Cmd)
  • 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)

Example

c := cmd.NewCommand("echo hello", cmd.WithStandardStreams)
c.Execute()

Set custom options

setWorkingDir := func (c *Command) {
    c.WorkingDir = "/tmp/test"
}

c := cmd.NewCommand("pwd", setWorkingDir)
c.Execute()

Testing

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)

Development

Running tests

make test

ToDo

  • os.Stdout and os.Stderr output access after execution via c.Stdout() and c.Stderr()

About

A simple package to execute shell commands on linux, windows and osx

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 94.8%
  • Shell 2.9%
  • Makefile 2.3%