2019-05-17 22:13:47 +02:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2024-11-30 17:00:58 +02:00
|
|
|
"github.com/go-task/task/v3/errors"
|
2023-03-10 20:27:30 +02:00
|
|
|
"github.com/go-task/task/v3/internal/env"
|
2020-08-16 20:48:19 +02:00
|
|
|
"github.com/go-task/task/v3/internal/execext"
|
|
|
|
"github.com/go-task/task/v3/internal/logger"
|
2023-12-29 22:32:03 +02:00
|
|
|
"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
|
|
|
|
2023-12-29 22:32:03 +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,
|
2023-03-10 20:27:30 +02:00
|
|
|
Env: env.Get(t),
|
2019-05-17 22:13:47 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
2023-10-08 00:01:57 +02:00
|
|
|
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
|
|
|
|
}
|