1
0
mirror of https://github.com/go-task/task.git synced 2024-12-04 10:24:45 +02:00
task/precondition.go

33 lines
831 B
Go
Raw Normal View History

2019-05-17 22:13:47 +02:00
package task
import (
"context"
"github.com/go-task/task/v3/errors"
"github.com/go-task/task/v3/internal/env"
"github.com/go-task/task/v3/internal/execext"
"github.com/go-task/task/v3/internal/logger"
"github.com/go-task/task/v3/taskfile/ast"
2019-05-17 22:13:47 +02:00
)
2023-03-31 21:13:29 +02:00
// ErrPreconditionFailed is returned when a precondition fails
var ErrPreconditionFailed = errors.New("task: precondition not met")
2019-05-17 22:13:47 +02:00
func (e *Executor) areTaskPreconditionsMet(ctx context.Context, t *ast.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: env.Get(t),
2019-05-17 22:13:47 +02:00
})
if err != nil {
if !errors.Is(err, context.Canceled) {
e.Logger.Errf(logger.Magenta, "task: %s\n", p.Msg)
}
2019-05-28 22:02:59 +02:00
return false, ErrPreconditionFailed
2019-05-17 22:13:47 +02:00
}
}
return true, nil
}