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

31 lines
755 B
Go
Raw Normal View History

2019-05-17 22:13:47 +02:00
package task
import (
"context"
"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"
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 *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: env.Get(t),
2019-05-17 22:13:47 +02:00
})
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
}