mirror of
https://github.com/go-task/task.git
synced 2025-01-26 05:27:15 +02:00
feat: add json and yaml variable constructors
This commit is contained in:
parent
2ccf80713d
commit
c655d90ab3
@ -3,10 +3,13 @@ package compiler
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
|
||||||
"github.com/go-task/task/v3/internal/execext"
|
"github.com/go-task/task/v3/internal/execext"
|
||||||
"github.com/go-task/task/v3/internal/filepathext"
|
"github.com/go-task/task/v3/internal/filepathext"
|
||||||
"github.com/go-task/task/v3/internal/logger"
|
"github.com/go-task/task/v3/internal/logger"
|
||||||
@ -64,6 +67,8 @@ 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.Json = tr.Replace(v.Json)
|
||||||
|
newVar.Yaml = tr.Replace(v.Yaml)
|
||||||
newVar.Dir = v.Dir
|
newVar.Dir = v.Dir
|
||||||
// If the variable should not be evaluated, but is nil, set it to an empty string
|
// If the variable should not be evaluated, but is nil, set it to an empty string
|
||||||
// This stops empty interface errors when using the templater to replace values later
|
// This stops empty interface errors when using the templater to replace values later
|
||||||
@ -80,6 +85,18 @@ func (c *Compiler) getVariables(t *ast.Task, call *ast.Call, evaluateShVars bool
|
|||||||
if err := tr.Err(); err != nil {
|
if err := tr.Err(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
// Evaluate JSON
|
||||||
|
if newVar.Json != "" {
|
||||||
|
if err := json.Unmarshal([]byte(newVar.Json), &newVar.Value); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Evaluate YAML
|
||||||
|
if newVar.Yaml != "" {
|
||||||
|
if err := yaml.Unmarshal([]byte(newVar.Yaml), &newVar.Value); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
// If the variable is not dynamic, we can set it and return
|
// If the variable is not dynamic, we can set it and return
|
||||||
if newVar.Value != nil || newVar.Sh == "" {
|
if newVar.Value != nil || newVar.Sh == "" {
|
||||||
result.Set(k, ast.Var{Value: newVar.Value})
|
result.Set(k, ast.Var{Value: newVar.Value})
|
||||||
|
@ -76,6 +76,8 @@ type Var struct {
|
|||||||
Value any
|
Value any
|
||||||
Live any
|
Live any
|
||||||
Sh string
|
Sh string
|
||||||
|
Json string
|
||||||
|
Yaml string
|
||||||
Dir string
|
Dir string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,19 +107,23 @@ 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":
|
case "sh", "map", "json", "yaml":
|
||||||
var m struct {
|
var m struct {
|
||||||
Sh string
|
Sh string
|
||||||
Map any
|
Map any
|
||||||
|
Json string
|
||||||
|
Yaml string
|
||||||
}
|
}
|
||||||
if err := node.Decode(&m); err != nil {
|
if err := node.Decode(&m); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
v.Sh = m.Sh
|
v.Sh = m.Sh
|
||||||
v.Value = m.Map
|
v.Value = m.Map
|
||||||
|
v.Json = m.Json
|
||||||
|
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" or using a scalar value`, node.Line, key)
|
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)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
var value any
|
var value any
|
||||||
|
32
testdata/vars/any2/Taskfile.yml
vendored
32
testdata/vars/any2/Taskfile.yml
vendored
@ -19,3 +19,35 @@ tasks:
|
|||||||
{{- else}} and {{$child.name -}}
|
{{- else}} and {{$child.name -}}
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
{{- end -}}"
|
{{- end -}}"
|
||||||
|
|
||||||
|
json:
|
||||||
|
vars:
|
||||||
|
JSON_STRING:
|
||||||
|
sh: cat example.json
|
||||||
|
JSON:
|
||||||
|
json: "{{.JSON_STRING}}"
|
||||||
|
cmds:
|
||||||
|
- >-
|
||||||
|
echo "{{.JSON.name}} has {{len .JSON.children}} children called
|
||||||
|
{{- $children := .JSON.children -}}
|
||||||
|
{{- range $i, $child := $children -}}
|
||||||
|
{{- if lt $i (sub (len $children) 1)}} {{$child.name -}},
|
||||||
|
{{- else}} and {{$child.name -}}
|
||||||
|
{{- end -}}
|
||||||
|
{{- end -}}"
|
||||||
|
|
||||||
|
yaml:
|
||||||
|
vars:
|
||||||
|
YAML_STRING:
|
||||||
|
sh: cat example.yaml
|
||||||
|
YAML:
|
||||||
|
yaml: "{{.YAML_STRING}}"
|
||||||
|
cmds:
|
||||||
|
- >-
|
||||||
|
echo "{{.YAML.name}} has {{len .YAML.children}} children called
|
||||||
|
{{- $children := .YAML.children -}}
|
||||||
|
{{- range $i, $child := $children -}}
|
||||||
|
{{- if lt $i (sub (len $children) 1)}} {{$child.name -}},
|
||||||
|
{{- else}} and {{$child.name -}}
|
||||||
|
{{- end -}}
|
||||||
|
{{- end -}}"
|
||||||
|
18
testdata/vars/any2/example.json
vendored
Normal file
18
testdata/vars/any2/example.json
vendored
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"name": "Alice",
|
||||||
|
"age": 30,
|
||||||
|
"children": [
|
||||||
|
{
|
||||||
|
"name": "Bob",
|
||||||
|
"age": 5
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Charlie",
|
||||||
|
"age": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Diane",
|
||||||
|
"age": 1
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
9
testdata/vars/any2/example.yaml
vendored
Normal file
9
testdata/vars/any2/example.yaml
vendored
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
name: Alice
|
||||||
|
age: 30
|
||||||
|
children:
|
||||||
|
- name: Bob
|
||||||
|
age: 5
|
||||||
|
- name: Charlie
|
||||||
|
age: 3
|
||||||
|
- name: Diane
|
||||||
|
age: 1
|
Loading…
x
Reference in New Issue
Block a user