MCPcopy
hub / github.com/go-task/task / getVariables

Method getVariables

compiler.go:48–147  ·  view source on GitHub ↗
(t *ast.Task, call *Call, evaluateShVars bool)

Source from the content-addressed store, hash-verified

46}
47
48func (c *Compiler) getVariables(t *ast.Task, call *Call, evaluateShVars bool) (*ast.Vars, error) {
49 result := env.GetEnviron()
50 specialVars, err := c.getSpecialVars(t, call)
51 if err != nil {
52 return nil, err
53 }
54 for k, v := range specialVars {
55 result.Set(k, ast.Var{Value: v, Secret: false})
56 }
57
58 getRangeFunc := func(dir string) func(k string, v ast.Var) error {
59 return func(k string, v ast.Var) error {
60 cache := &templater.Cache{Vars: result}
61 // Replace values
62 newVar := templater.ReplaceVar(v, cache)
63 // If the variable should not be evaluated, but is nil, set it to an empty string
64 // This stops empty interface errors when using the templater to replace values later
65 // Preserve the Sh field so it can be displayed in summary
66 if !evaluateShVars && newVar.Value == nil {
67 result.Set(k, ast.Var{Value: "", Sh: newVar.Sh, Secret: v.Secret})
68 return nil
69 }
70 // If the variable should not be evaluated and it is set, we can set it and return
71 if !evaluateShVars {
72 result.Set(k, ast.Var{Value: newVar.Value, Sh: newVar.Sh, Secret: v.Secret})
73 return nil
74 }
75 // Now we can check for errors since we've handled all the cases when we don't want to evaluate
76 if err := cache.Err(); err != nil {
77 return err
78 }
79 // If the variable is already set, we can set it and return
80 if newVar.Value != nil || newVar.Sh == nil {
81 result.Set(k, ast.Var{Value: newVar.Value, Secret: v.Secret})
82 return nil
83 }
84 // If the variable is dynamic, we need to resolve it first
85 static, err := c.HandleDynamicVar(newVar, dir, env.GetFromVars(result))
86 if err != nil {
87 return err
88 }
89 result.Set(k, ast.Var{Value: static, Secret: v.Secret})
90 return nil
91 }
92 }
93 rangeFunc := getRangeFunc(c.Dir)
94
95 var taskRangeFunc func(k string, v ast.Var) error
96 if t != nil {
97 // NOTE(@andreynering): We're manually joining these paths here because
98 // this is the raw task, not the compiled one.
99 cache := &templater.Cache{Vars: result}
100 dir := templater.Replace(t.Dir, cache)
101 if err := cache.Err(); err != nil {
102 return nil, err
103 }
104 dir = filepathext.SmartJoin(c.Dir, dir)
105 taskRangeFunc = getRangeFunc(dir)

Callers 3

GetTaskfileVariablesMethod · 0.95
GetVariablesMethod · 0.95
FastGetVariablesMethod · 0.95

Calls 10

getSpecialVarsMethod · 0.95
ErrMethod · 0.95
HandleDynamicVarMethod · 0.95
GetEnvironFunction · 0.92
ReplaceVarFunction · 0.92
GetFromVarsFunction · 0.92
ReplaceFunction · 0.92
SmartJoinFunction · 0.92
SetMethod · 0.45
AllMethod · 0.45

Tested by

no test coverage detected