https://github.com/xlab/invoker
Supercharged "exec.Cmd" helpers for asynchronous and thread-safe reading of StdOut and StdErr.
https://github.com/xlab/invoker
Last synced: over 1 year ago
JSON representation
Supercharged "exec.Cmd" helpers for asynchronous and thread-safe reading of StdOut and StdErr.
- Host: GitHub
- URL: https://github.com/xlab/invoker
- Owner: xlab
- License: mit
- Created: 2020-04-19T18:48:04.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2022-09-24T17:51:20.000Z (almost 4 years ago)
- Last Synced: 2025-01-28T04:31:18.485Z (over 1 year ago)
- Language: Go
- Size: 5.86 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## invoker [](https://goreportcard.com/report/github.com/xlab/invoker) [](https://godoc.org/github.com/xlab/invoker)
A package providing supercharged primitive for `exec.Cmd` that redirects StdErr and StdOut for asynchronous and thread-safe reading.
### Example
```go
return func(ctx *gin.Context) {
out := invoker.Run(ctx, "list", "-fmt", "json")
defer func() {
go invoker.DrainOut(out)
}()
select {
case <-ctx.Done():
ctx.AbortWithStatus(504)
return
case r := <-out:
if r.Error != nil {
ctx.String(500, "%+v", r.Error)
return
}
ctx.Data(200, "application/json", r.StdOut())
}
}
```
### Redirect to your logging
```go
ctx := context.Background()
logFn := func(data []byte) (stop bool) {
logger.Info("Output:", string(data))
return
}
stdErr := invoker.NewWatchedSafeBuffer(ctx, logFn, nil)
out := invoker.RunWithIO(ctx, nil, nil, stdErr, "list", "-fmt", "json")
go invoker.DrainOut(out)
```
### License
[MIT](/LICENSE)