forked from go-task/task
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvar_extractor.go
83 lines (73 loc) · 1.95 KB
/
var_extractor.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package task
import (
"github.com/go-task/task/v3/taskfile"
)
// extractAllVarsForTask extracts all vars and envs from the task, it's taskfile,
// and any dependencies, including taskvars.
// only the user-specified vars/envs will be extracted, i.e. not the whole OS environment.
func extractAllVarsForTask(
call taskfile.Call,
executor *Executor,
task *taskfile.Task,
taskEvaluatedVars *taskfile.Vars,
additionalVars ...*taskfile.Vars,
) *map[string]string {
// we want to export only the user-specified vars and envs
excludeVars := []string{
"CLI_ARGS",
}
result := taskfile.Vars{}
// for some reason, task doesn't contain its vars, but it can be retrieved from the taskfile spec
for _, taskfileTask := range executor.Taskfile.Tasks {
if taskfileTask.Name() == task.Name() {
if taskfileTask.Env != nil {
result.Merge(taskfileTask.Env)
}
if taskfileTask.Vars != nil {
result.Merge(taskfileTask.Vars)
}
}
}
if call.Vars != nil {
result.Merge(call.Vars)
}
if task.Vars != nil {
result.Merge(task.Vars)
}
if task.Env != nil {
result.Merge(task.Env)
}
if task.IncludeVars != nil {
result.Merge(task.IncludeVars)
}
if task.IncludedTaskfileVars != nil {
result.Merge(task.IncludedTaskfileVars)
}
if len(additionalVars) > 0 {
for _, additionalVar := range additionalVars {
result.Merge(additionalVar)
}
}
if executor.Taskfile.Vars != nil {
result.Merge(executor.Taskfile.Vars)
}
// taskEvaluatedVars contains the whole env, we don't need it
// but we need the evaluated dynamic vars values
for varName := range result.Mapping {
if evaluatedVar, ok := taskEvaluatedVars.Mapping[varName]; ok {
result.Mapping[varName] = evaluatedVar
}
}
for _, excludeVar := range excludeVars {
delete(result.Mapping, excludeVar)
}
resultMapping := make(map[string]string, 0)
for k, v := range result.Mapping {
resultMapping[k] = v.Static
}
if result.Len() > 0 {
return &resultMapping
} else {
return nil
}
}