mirror of
https://github.com/go-task/task.git
synced 2025-07-17 01:43:07 +02:00
feat: add sh and map (value) support
This commit is contained in:
@ -32,7 +32,7 @@ func init() {
|
|||||||
readDotEnv()
|
readDotEnv()
|
||||||
GentleForce = New("GENTLE_FORCE")
|
GentleForce = New("GENTLE_FORCE")
|
||||||
RemoteTaskfiles = New("REMOTE_TASKFILES")
|
RemoteTaskfiles = New("REMOTE_TASKFILES")
|
||||||
AnyVariables = New("ANY_VARIABLES")
|
AnyVariables = New("ANY_VARIABLES", "1", "2")
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(xName string, enabledValues ...string) Experiment {
|
func New(xName string, enabledValues ...string) Experiment {
|
||||||
|
@ -81,19 +81,53 @@ type Var struct {
|
|||||||
|
|
||||||
func (v *Var) UnmarshalYAML(node *yaml.Node) error {
|
func (v *Var) UnmarshalYAML(node *yaml.Node) error {
|
||||||
if experiments.AnyVariables.Enabled {
|
if experiments.AnyVariables.Enabled {
|
||||||
var value any
|
|
||||||
if err := node.Decode(&value); err != nil {
|
// This implementation is not backwards-compatible and replaces the 'sh' key with map variables
|
||||||
return err
|
if experiments.AnyVariables.Value == "1" {
|
||||||
|
var value any
|
||||||
|
if err := node.Decode(&value); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// If the value is a string and it starts with $, then it's a shell command
|
||||||
|
if str, ok := value.(string); ok {
|
||||||
|
if str, ok = strings.CutPrefix(str, "$"); ok {
|
||||||
|
v.Sh = str
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
v.Value = value
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
// If the value is a string and it starts with $, then it's a shell command
|
|
||||||
if str, ok := value.(string); ok {
|
// This implementation IS backwards-compatible and keeps the 'sh' key and allows map variables to be added under the `map` key
|
||||||
if str, ok = strings.CutPrefix(str, "$"); ok {
|
if experiments.AnyVariables.Value == "2" {
|
||||||
v.Sh = str
|
switch node.Kind {
|
||||||
|
case yaml.MappingNode:
|
||||||
|
key := node.Content[0].Value
|
||||||
|
switch key {
|
||||||
|
case "sh", "map":
|
||||||
|
var m struct {
|
||||||
|
Sh string
|
||||||
|
Map any
|
||||||
|
}
|
||||||
|
if err := node.Decode(&m); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
v.Sh = m.Sh
|
||||||
|
v.Value = m.Map
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
return fmt.Errorf(`yaml: line %d: %q is not a valid variable type. Try "sh", "map" or using a scalar value`, node.Line, key)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
var value any
|
||||||
|
if err := node.Decode(&value); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
v.Value = value
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
v.Value = value
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch node.Kind {
|
switch node.Kind {
|
||||||
|
21
testdata/vars/any2/Taskfile.yml
vendored
Normal file
21
testdata/vars/any2/Taskfile.yml
vendored
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
version: '3'
|
||||||
|
|
||||||
|
tasks:
|
||||||
|
default:
|
||||||
|
- task: map
|
||||||
|
- task: json
|
||||||
|
- task: yaml
|
||||||
|
|
||||||
|
map:
|
||||||
|
vars:
|
||||||
|
MAP:
|
||||||
|
map: {"name":"Alice","age":30,"children":[{"name":"Bob","age":5},{"name":"Charlie","age":3},{"name":"Diane","age":1}]}
|
||||||
|
cmds:
|
||||||
|
- >-
|
||||||
|
echo "{{.MAP.name}} has {{len .MAP.children}} children called
|
||||||
|
{{- $children := .MAP.children -}}
|
||||||
|
{{- range $i, $child := $children -}}
|
||||||
|
{{- if lt $i (sub (len $children) 1)}} {{$child.name -}},
|
||||||
|
{{- else}} and {{$child.name -}}
|
||||||
|
{{- end -}}
|
||||||
|
{{- end -}}"
|
Reference in New Issue
Block a user