-
-
Notifications
You must be signed in to change notification settings - Fork 624
/
requires.go
52 lines (44 loc) · 1.17 KB
/
requires.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package task
import (
"slices"
"github.com/go-task/task/v3/errors"
"github.com/go-task/task/v3/taskfile/ast"
)
func (e *Executor) areTaskRequiredVarsSet(t *ast.Task, call *ast.Call) error {
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
var notAllowedValuesVars []errors.NotAllowedVar
for _, requiredVar := range t.Requires.Vars {
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,
})
}
}
}
if len(missingVars) > 0 {
return &errors.TaskMissingRequiredVars{
TaskName: t.Name(),
MissingVars: missingVars,
}
}
if len(notAllowedValuesVars) > 0 {
return &errors.TaskNotAllowedVars{
TaskName: t.Name(),
NotAllowedVars: notAllowedValuesVars,
}
}
return nil
}