mirror of
https://github.com/go-task/task.git
synced 2025-03-19 21:17:46 +02:00
A benchmark was added. The performance improvement is considerable: BenchmarkNoPool-4 30000 43405 ns/op BenchmarkPool-4 20000 71219 ns/op
73 lines
1.2 KiB
Go
73 lines
1.2 KiB
Go
package execext
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"strings"
|
|
"sync"
|
|
|
|
"mvdan.cc/sh/interp"
|
|
"mvdan.cc/sh/syntax"
|
|
)
|
|
|
|
var (
|
|
parserPool = sync.Pool{
|
|
New: func() interface{} {
|
|
return syntax.NewParser()
|
|
},
|
|
}
|
|
|
|
runnerPool = sync.Pool{
|
|
New: func() interface{} {
|
|
return &interp.Runner{}
|
|
},
|
|
}
|
|
)
|
|
|
|
// RunCommandOptions is the options for the RunCommand func
|
|
type RunCommandOptions struct {
|
|
Context context.Context
|
|
Command string
|
|
Dir string
|
|
Env []string
|
|
Stdin io.Reader
|
|
Stdout io.Writer
|
|
Stderr io.Writer
|
|
}
|
|
|
|
var (
|
|
// ErrNilOptions is returned when a nil options is given
|
|
ErrNilOptions = errors.New("execext: nil options given")
|
|
)
|
|
|
|
// RunCommand runs a shell command
|
|
func RunCommand(opts *RunCommandOptions) error {
|
|
if opts == nil {
|
|
return ErrNilOptions
|
|
}
|
|
|
|
parser := parserPool.Get().(*syntax.Parser)
|
|
defer parserPool.Put(parser)
|
|
|
|
p, err := parser.Parse(strings.NewReader(opts.Command), "")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
r := runnerPool.Get().(*interp.Runner)
|
|
defer runnerPool.Put(r)
|
|
|
|
r.Context = opts.Context
|
|
r.Dir = opts.Dir
|
|
r.Env = opts.Env
|
|
r.Stdin = opts.Stdin
|
|
r.Stdout = opts.Stdout
|
|
r.Stderr = opts.Stderr
|
|
|
|
if err = r.Reset(); err != nil {
|
|
return err
|
|
}
|
|
return r.Run(p)
|
|
}
|