Skip to content

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

License

Notifications You must be signed in to change notification settings

commander-cli/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

Install the latest version with:

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

or an exact version:

$ go get -u github.com/commander-cli/[email protected]

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 an option function can 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)

See godocs for details.

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()

Contributing

If you would like to contribute please submit a pull request. For bug fixes/minor changes a simple pull request will suffice. If the change is large or you would like to have a feature discussion before implementation feel free to open an issue.

If you have a feature request or bug report please open an issue.

Development

Please fork the project and do your development there. Please use a meaningful branch name as well as adhere to commitlint rules.

If you would like the precommit hooks run:

make init

To run the test suite:

make test

Reminder: The goal of this project is to ensure we abstract the OS specific command execution as mush as possible. Ensure your change is compatible with linux, windows and osx. If unable to test on every operating system (help needed for windows (:) the CI will take care of that for you.