requires.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package task
  2. import (
  3. "slices"
  4. "github.com/go-task/task/v3/errors"
  5. "github.com/go-task/task/v3/taskfile/ast"
  6. )
  7. func (e *Executor) areTaskRequiredVarsSet(t *ast.Task, call *ast.Call) error {
  8. if t.Requires == nil || len(t.Requires.Vars) == 0 {
  9. return nil
  10. }
  11. vars, err := e.Compiler.GetVariables(t, call)
  12. if err != nil {
  13. return err
  14. }
  15. var missingVars []string
  16. var notAllowedValuesVars []errors.NotAllowedVar
  17. for _, requiredVar := range t.Requires.Vars {
  18. value, isString := vars.Get(requiredVar.Name).Value.(string)
  19. if !vars.Exists(requiredVar.Name) {
  20. missingVars = append(missingVars, requiredVar.Name)
  21. } else {
  22. if isString && requiredVar.Enum != nil && !slices.Contains(requiredVar.Enum, value) {
  23. notAllowedValuesVars = append(notAllowedValuesVars, errors.NotAllowedVar{
  24. Value: value,
  25. Enum: requiredVar.Enum,
  26. Name: requiredVar.Name,
  27. })
  28. }
  29. }
  30. }
  31. if len(missingVars) > 0 {
  32. return &errors.TaskMissingRequiredVars{
  33. TaskName: t.Name(),
  34. MissingVars: missingVars,
  35. }
  36. }
  37. if len(notAllowedValuesVars) > 0 {
  38. return &errors.TaskNotAllowedVars{
  39. TaskName: t.Name(),
  40. NotAllowedVars: notAllowedValuesVars,
  41. }
  42. }
  43. return nil
  44. }