2019-05-17 22:13:47 +02:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/go-task/task/v2/internal/execext"
|
2019-06-16 03:47:15 +02:00
|
|
|
"github.com/go-task/task/v2/internal/logger"
|
2019-05-17 22:13:47 +02:00
|
|
|
"github.com/go-task/task/v2/internal/taskfile"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-05-28 22:02:59 +02:00
|
|
|
// ErrPreconditionFailed is returned when a precondition fails
|
|
|
|
ErrPreconditionFailed = errors.New("task: precondition not met")
|
2019-05-17 22:13:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func (e *Executor) areTaskPreconditionsMet(ctx context.Context, t *taskfile.Task) (bool, error) {
|
2019-05-28 22:02:59 +02:00
|
|
|
for _, p := range t.Preconditions {
|
2019-05-17 22:13:47 +02:00
|
|
|
err := execext.RunCommand(ctx, &execext.RunCommandOptions{
|
|
|
|
Command: p.Sh,
|
|
|
|
Dir: t.Dir,
|
|
|
|
Env: getEnviron(t),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2019-06-16 03:47:15 +02:00
|
|
|
e.Logger.Errf(logger.Magenta, "task: %s", p.Msg)
|
2019-05-28 22:02:59 +02:00
|
|
|
return false, ErrPreconditionFailed
|
2019-05-17 22:13:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|