mirror of
https://github.com/go-task/task.git
synced 2025-04-27 12:32:25 +02:00
feat: add ability to call task with variable by reference
This commit is contained in:
parent
c655d90ab3
commit
1f3fca50b3
@ -67,6 +67,7 @@ func (c *Compiler) getVariables(t *ast.Task, call *ast.Call, evaluateShVars bool
|
|||||||
newVar.Value = value
|
newVar.Value = value
|
||||||
}
|
}
|
||||||
newVar.Sh = tr.Replace(v.Sh)
|
newVar.Sh = tr.Replace(v.Sh)
|
||||||
|
newVar.Ref = v.Ref
|
||||||
newVar.Json = tr.Replace(v.Json)
|
newVar.Json = tr.Replace(v.Json)
|
||||||
newVar.Yaml = tr.Replace(v.Yaml)
|
newVar.Yaml = tr.Replace(v.Yaml)
|
||||||
newVar.Dir = v.Dir
|
newVar.Dir = v.Dir
|
||||||
|
@ -113,6 +113,9 @@ func (r *Templater) replaceVars(vars *ast.Vars, extra map[string]any) *ast.Vars
|
|||||||
}
|
}
|
||||||
newVar.Live = v.Live
|
newVar.Live = v.Live
|
||||||
newVar.Sh = r.ReplaceWithExtra(v.Sh, extra)
|
newVar.Sh = r.ReplaceWithExtra(v.Sh, extra)
|
||||||
|
newVar.Ref = v.Ref
|
||||||
|
newVar.Json = r.ReplaceWithExtra(v.Json, extra)
|
||||||
|
newVar.Yaml = r.ReplaceWithExtra(v.Yaml, extra)
|
||||||
newVars.Set(k, newVar)
|
newVars.Set(k, newVar)
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
@ -76,6 +76,7 @@ type Var struct {
|
|||||||
Value any
|
Value any
|
||||||
Live any
|
Live any
|
||||||
Sh string
|
Sh string
|
||||||
|
Ref string
|
||||||
Json string
|
Json string
|
||||||
Yaml string
|
Yaml string
|
||||||
Dir string
|
Dir string
|
||||||
@ -107,9 +108,10 @@ func (v *Var) UnmarshalYAML(node *yaml.Node) error {
|
|||||||
case yaml.MappingNode:
|
case yaml.MappingNode:
|
||||||
key := node.Content[0].Value
|
key := node.Content[0].Value
|
||||||
switch key {
|
switch key {
|
||||||
case "sh", "map", "json", "yaml":
|
case "sh", "ref", "map", "json", "yaml":
|
||||||
var m struct {
|
var m struct {
|
||||||
Sh string
|
Sh string
|
||||||
|
Ref string
|
||||||
Map any
|
Map any
|
||||||
Json string
|
Json string
|
||||||
Yaml string
|
Yaml string
|
||||||
@ -118,12 +120,13 @@ func (v *Var) UnmarshalYAML(node *yaml.Node) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
v.Sh = m.Sh
|
v.Sh = m.Sh
|
||||||
|
v.Ref = m.Ref
|
||||||
v.Value = m.Map
|
v.Value = m.Map
|
||||||
v.Json = m.Json
|
v.Json = m.Json
|
||||||
v.Yaml = m.Yaml
|
v.Yaml = m.Yaml
|
||||||
return nil
|
return nil
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf(`yaml: line %d: %q is not a valid variable type. Try "sh", "map", "json", "yaml" or using a scalar value`, node.Line, key)
|
return fmt.Errorf(`yaml: line %d: %q is not a valid variable type. Try "sh", "ref", "map", "json", "yaml" or using a scalar value`, node.Line, key)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
var value any
|
var value any
|
||||||
|
35
testdata/vars/any2/Taskfile.yml
vendored
35
testdata/vars/any2/Taskfile.yml
vendored
@ -11,14 +11,10 @@ tasks:
|
|||||||
MAP:
|
MAP:
|
||||||
map: {"name":"Alice","age":30,"children":[{"name":"Bob","age":5},{"name":"Charlie","age":3},{"name":"Diane","age":1}]}
|
map: {"name":"Alice","age":30,"children":[{"name":"Bob","age":5},{"name":"Charlie","age":3},{"name":"Diane","age":1}]}
|
||||||
cmds:
|
cmds:
|
||||||
- >-
|
- task: print-var
|
||||||
echo "{{.MAP.name}} has {{len .MAP.children}} children called
|
vars:
|
||||||
{{- $children := .MAP.children -}}
|
VAR:
|
||||||
{{- range $i, $child := $children -}}
|
ref: MAP
|
||||||
{{- if lt $i (sub (len $children) 1)}} {{$child.name -}},
|
|
||||||
{{- else}} and {{$child.name -}}
|
|
||||||
{{- end -}}
|
|
||||||
{{- end -}}"
|
|
||||||
|
|
||||||
json:
|
json:
|
||||||
vars:
|
vars:
|
||||||
@ -27,14 +23,10 @@ tasks:
|
|||||||
JSON:
|
JSON:
|
||||||
json: "{{.JSON_STRING}}"
|
json: "{{.JSON_STRING}}"
|
||||||
cmds:
|
cmds:
|
||||||
- >-
|
- task: print-var
|
||||||
echo "{{.JSON.name}} has {{len .JSON.children}} children called
|
vars:
|
||||||
{{- $children := .JSON.children -}}
|
VAR:
|
||||||
{{- range $i, $child := $children -}}
|
ref: JSON
|
||||||
{{- if lt $i (sub (len $children) 1)}} {{$child.name -}},
|
|
||||||
{{- else}} and {{$child.name -}}
|
|
||||||
{{- end -}}
|
|
||||||
{{- end -}}"
|
|
||||||
|
|
||||||
yaml:
|
yaml:
|
||||||
vars:
|
vars:
|
||||||
@ -42,10 +34,17 @@ tasks:
|
|||||||
sh: cat example.yaml
|
sh: cat example.yaml
|
||||||
YAML:
|
YAML:
|
||||||
yaml: "{{.YAML_STRING}}"
|
yaml: "{{.YAML_STRING}}"
|
||||||
|
cmds:
|
||||||
|
- task: print-var
|
||||||
|
vars:
|
||||||
|
VAR:
|
||||||
|
ref: YAML
|
||||||
|
|
||||||
|
print-var:
|
||||||
cmds:
|
cmds:
|
||||||
- >-
|
- >-
|
||||||
echo "{{.YAML.name}} has {{len .YAML.children}} children called
|
echo "{{.VAR.name}} has {{len .VAR.children}} children called
|
||||||
{{- $children := .YAML.children -}}
|
{{- $children := .VAR.children -}}
|
||||||
{{- range $i, $child := $children -}}
|
{{- range $i, $child := $children -}}
|
||||||
{{- if lt $i (sub (len $children) 1)}} {{$child.name -}},
|
{{- if lt $i (sub (len $children) 1)}} {{$child.name -}},
|
||||||
{{- else}} and {{$child.name -}}
|
{{- else}} and {{$child.name -}}
|
||||||
|
11
variables.go
11
variables.go
@ -211,6 +211,17 @@ func (e *Executor) compiledTask(call ast.Call, evaluateShVars bool) (*ast.Task,
|
|||||||
newCmd.Cmd = r.Replace(cmd.Cmd)
|
newCmd.Cmd = r.Replace(cmd.Cmd)
|
||||||
newCmd.Task = r.Replace(cmd.Task)
|
newCmd.Task = r.Replace(cmd.Task)
|
||||||
newCmd.Vars = r.ReplaceVars(cmd.Vars)
|
newCmd.Vars = r.ReplaceVars(cmd.Vars)
|
||||||
|
// Loop over the command's variables and resolve any references to other variables
|
||||||
|
err := cmd.Vars.Range(func(k string, v ast.Var) error {
|
||||||
|
if v.Ref != "" {
|
||||||
|
refVal := vars.Get(v.Ref)
|
||||||
|
newCmd.Vars.Set(k, refVal)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
new.Cmds = append(new.Cmds, newCmd)
|
new.Cmds = append(new.Cmds, newCmd)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user