2023-06-30 03:13:41 +02:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
2024-10-18 18:16:57 +02:00
|
|
|
"slices"
|
|
|
|
|
2023-06-30 03:13:41 +02:00
|
|
|
"github.com/go-task/task/v3/errors"
|
2023-12-29 22:32:03 +02:00
|
|
|
"github.com/go-task/task/v3/taskfile/ast"
|
2023-06-30 03:13:41 +02:00
|
|
|
)
|
|
|
|
|
2024-08-14 15:37:05 +02:00
|
|
|
func (e *Executor) areTaskRequiredVarsSet(t *ast.Task, call *ast.Call) error {
|
2023-06-30 03:13:41 +02:00
|
|
|
if t.Requires == nil || len(t.Requires.Vars) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
vars, err := e.Compiler.GetVariables(t, call)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var missingVars []string
|
2024-10-18 18:16:57 +02:00
|
|
|
var notAllowedValuesVars []errors.NotAllowedVar
|
2023-06-30 03:13:41 +02:00
|
|
|
for _, requiredVar := range t.Requires.Vars {
|
2024-10-18 18:16:57 +02:00
|
|
|
value, isString := vars.Get(requiredVar.Name).Value.(string)
|
|
|
|
if !vars.Exists(requiredVar.Name) {
|
|
|
|
missingVars = append(missingVars, requiredVar.Name)
|
|
|
|
} else {
|
|
|
|
if isString && requiredVar.Enum != nil && !slices.Contains(requiredVar.Enum, value) {
|
|
|
|
notAllowedValuesVars = append(notAllowedValuesVars, errors.NotAllowedVar{
|
|
|
|
Value: value,
|
|
|
|
Enum: requiredVar.Enum,
|
|
|
|
Name: requiredVar.Name,
|
|
|
|
})
|
|
|
|
}
|
2023-06-30 03:13:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(missingVars) > 0 {
|
|
|
|
return &errors.TaskMissingRequiredVars{
|
|
|
|
TaskName: t.Name(),
|
|
|
|
MissingVars: missingVars,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-18 18:16:57 +02:00
|
|
|
if len(notAllowedValuesVars) > 0 {
|
|
|
|
return &errors.TaskNotAllowedVars{
|
|
|
|
TaskName: t.Name(),
|
|
|
|
NotAllowedVars: notAllowedValuesVars,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-30 03:13:41 +02:00
|
|
|
return nil
|
|
|
|
}
|