https://github.com/berquerant/execx
Provides `os/exec.Cmd` wrappers.
https://github.com/berquerant/execx
go
Last synced: about 1 year ago
JSON representation
Provides `os/exec.Cmd` wrappers.
- Host: GitHub
- URL: https://github.com/berquerant/execx
- Owner: berquerant
- License: mit
- Created: 2024-04-13T04:50:51.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2025-04-20T07:34:18.000Z (about 1 year ago)
- Last Synced: 2025-04-20T08:37:50.117Z (about 1 year ago)
- Topics: go
- Language: Go
- Homepage:
- Size: 103 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://goreportcard.com/report/github.com/berquerant/execx)
[](https://pkg.go.dev/github.com/berquerant/execx)
# execx
Provides `os/exec.Cmd` wrappers.
``` go
import (
"bytes"
"context"
"fmt"
"github.com/berquerant/execx"
)
func main() {
script := execx.NewScript(
`echo line1
echo ${line2}
cat -
echo line3 > /dev/stderr`,
"sh",
)
script.Env.Set("line2", "LINE2")
if err := script.Runner(func(cmd *execx.Cmd) error {
cmd.Stdin = bytes.NewBufferString("from stdin\n")
_, err := cmd.Run(
context.TODO(),
execx.WithStdoutConsumer(func(x execx.Token) {
fmt.Printf("1:%s\n", x)
}),
execx.WithStderrConsumer(func(x execx.Token) {
fmt.Printf("2:%s\n", x)
}),
)
return err
}); err != nil {
panic(err)
}
// Output:
// 1:line1
// 1:LINE2
// 1:from stdin
// 2:line3
}
```