Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/perbu/executor
Wrapper around os.Exec()
https://github.com/perbu/executor
Last synced: about 2 months ago
JSON representation
Wrapper around os.Exec()
- Host: GitHub
- URL: https://github.com/perbu/executor
- Owner: perbu
- License: bsd-3-clause
- Created: 2024-06-13T12:22:54.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-06-13T12:28:45.000Z (7 months ago)
- Last Synced: 2024-07-11T14:02:53.850Z (6 months ago)
- Language: Go
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# executor
A Go package to execute shell scripts in a temporary file and return stdout, stderr and exit code.
## Installation
```
go get github.com/perbu/executor
```## Usage
```go
package mainimport (
"fmt"
"github.com/perbu/executor"
)func main() {
script := []byte("#!/bin/sh\necho 'Hello, World!'\n")
err := executor.Execute(script)
if err != nil {
fmt.Printf("Error executing script: %v\n", err)
if execErr, ok := err.(*executor.ErrExecute); ok {
fmt.Printf("Stdout: %s\n", execErr.Stdout)
fmt.Printf("Stderr: %s\n", execErr.Stderr)
fmt.Printf("Exit code: %d\n", execErr.ExitCode)
}
}
}
```## Error Handling
If the script fails to execute, `Execute` will return an `ErrExecute` error which contains:
- `SubError`: The underlying error that caused the execution to fail
- `Stdout`: The contents of stdout
- `Stderr`: The contents of stderr
- `ExitCode`: The exit code of the scriptYou can type assert the returned error to `ErrExecute` to access these fields:
```go
if execErr, ok := err.(*executor.ErrExecute); ok {
fmt.Printf("Stdout: %s\n", execErr.Stdout)
fmt.Printf("Stderr: %s\n", execErr.Stderr)
fmt.Printf("Exit code: %d\n", execErr.ExitCode)
}
```## Testing
The package includes unit tests covering success and error scenarios. To run the tests:
```
go test ./...
```## License
See LICENSE.md
## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.