1
0
mirror of https://github.com/go-task/task.git synced 2025-01-18 04:59:01 +02:00
task/execext/exec.go

51 lines
910 B
Go
Raw Normal View History

2017-03-12 17:18:59 -03:00
package execext
import (
"context"
"errors"
"io"
"strings"
"github.com/mvdan/sh/interp"
"github.com/mvdan/sh/syntax"
2017-03-12 17:18:59 -03:00
)
2017-04-24 10:25:38 -03:00
// 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
}
2017-03-12 17:18:59 -03:00
var (
// ErrNilOptions is returned when a nil options is given
ErrNilOptions = errors.New("execext: nil options given")
2017-03-12 17:18:59 -03:00
)
// RunCommand runs a shell command
func RunCommand(opts *RunCommandOptions) error {
if opts == nil {
return ErrNilOptions
}
p, err := syntax.NewParser().Parse(strings.NewReader(opts.Command), "")
if err != nil {
return err
}
2017-03-12 17:18:59 -03:00
r := interp.Runner{
Context: opts.Context,
File: p,
Dir: opts.Dir,
Env: opts.Env,
Stdin: opts.Stdin,
Stdout: opts.Stdout,
Stderr: opts.Stderr,
}
2017-07-08 15:08:44 -03:00
return r.Run()
2017-03-12 17:18:59 -03:00
}