1
0

precondition.go 804 B

1234567891011121314151617181920212223242526272829303132
  1. package task
  2. import (
  3. "context"
  4. "errors"
  5. "github.com/go-task/task/v3/internal/env"
  6. "github.com/go-task/task/v3/internal/execext"
  7. "github.com/go-task/task/v3/internal/logger"
  8. "github.com/go-task/task/v3/taskfile/ast"
  9. )
  10. // ErrPreconditionFailed is returned when a precondition fails
  11. var ErrPreconditionFailed = errors.New("task: precondition not met")
  12. func (e *Executor) areTaskPreconditionsMet(ctx context.Context, t *ast.Task) (bool, error) {
  13. for _, p := range t.Preconditions {
  14. err := execext.RunCommand(ctx, &execext.RunCommandOptions{
  15. Command: p.Sh,
  16. Dir: t.Dir,
  17. Env: env.Get(t),
  18. })
  19. if err != nil {
  20. if !errors.Is(err, context.Canceled) {
  21. e.Logger.Errf(logger.Magenta, "task: %s\n", p.Msg)
  22. }
  23. return false, ErrPreconditionFailed
  24. }
  25. }
  26. return true, nil
  27. }