Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/natefinch/deputy
deputy is a go package that adds smarts on top of os/exec
https://github.com/natefinch/deputy
Last synced: 12 days ago
JSON representation
deputy is a go package that adds smarts on top of os/exec
- Host: GitHub
- URL: https://github.com/natefinch/deputy
- Owner: natefinch
- License: mit
- Created: 2015-06-17T04:19:29.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2022-03-21T04:58:51.000Z (over 2 years ago)
- Last Synced: 2024-10-12T06:43:54.154Z (27 days ago)
- Language: Go
- Size: 32.2 KB
- Stars: 232
- Watchers: 33
- Forks: 8
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# deputy [![GoDoc](https://godoc.org/npf.io/deputy?status.svg)](https://godoc.org/npf.io/deputy) [![Build Status](https://drone.io/github.com/natefinch/deputy/status.png)](https://drone.io/github.com/natefinch/deputy/latest)
deputy is a go package that adds smarts on top of os/exec![deputy-sm](https://cloud.githubusercontent.com/assets/3185864/8237448/6bc30102-15bd-11e5-9e87-6423197a73d6.jpg)
image: creative commons, © [MatsuRD](http://matsurd.deviantart.com/art/Paper53-Deputy-Stubbs-342123485)
## Example
``` go
// Make a new deputy that'll return the data written to stderr as the error
// message, log everything written to stdout to this application's log, and
// timeout after 30 seconds.
cancel := make(chan struct{})
go func() {
<-time.After(time.Second * 30)
close(cancel)
}()d := deputy.Deputy{
Errors: deputy.FromStderr,
StdoutLog: func(b []byte) { log.Print(string(b)) },
Cancel: cancel,
}
if err := d.Run(exec.Command("foo")); err != nil {
log.Print(err)
}
```## type Deputy
``` go
type Deputy struct {
// Cancel, when closed, will cause the command to close.
Cancel <-chan struct{}
// Errors describes how errors should be handled.
Errors ErrorHandling
// StdoutLog takes a function that will receive lines written to stdout from
// the command (with the newline elided).
StdoutLog func([]byte)
// StdoutLog takes a function that will receive lines written to stderr from
// the command (with the newline elided).
StderrLog func([]byte)
// contains filtered or unexported fields
}
```
Deputy is a type that runs Commands with advanced options not available from
os/exec. See the comments on field values for details.### func (Deputy) Run
``` go
func (d Deputy) Run(cmd *exec.Cmd) error
```
Run starts the specified command and waits for it to complete. Its behavior
conforms to the Options passed to it at construction time.Note that, like cmd.Run, Deputy.Run should not be used with
StdoutPipe or StderrPipe.## type ErrorHandling
``` go
type ErrorHandling int
```
ErrorHandling is a flag that tells Deputy how to handle errors running a
command. See the values below for the different modes.``` go
const (
// DefaultErrs represents the default handling of command errors - this
// simply returns the error from Cmd.Run()
DefaultErrs ErrorHandling = iota// FromStderr tells Deputy to convert the stderr output of a command into
// the text of an error, if the command exits with an error.
FromStderr// FromStdout tells Deputy to convert the stdout output of a command into
// the text of an error, if the command exits with an error.
FromStdout
)
```