2018-02-17 18:22:18 +02:00
|
|
|
package taskfile
|
|
|
|
|
|
|
|
import (
|
2022-12-19 03:11:31 +02:00
|
|
|
"fmt"
|
2020-03-29 21:54:59 +02:00
|
|
|
|
|
|
|
"gopkg.in/yaml.v3"
|
2023-04-06 13:07:57 +02:00
|
|
|
|
|
|
|
"github.com/go-task/task/v3/internal/orderedmap"
|
2018-02-17 18:22:18 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Vars is a string[string] variables map.
|
2020-03-29 21:54:59 +02:00
|
|
|
type Vars struct {
|
2023-04-06 13:07:57 +02:00
|
|
|
orderedmap.OrderedMap[string, Var]
|
2020-03-29 21:54:59 +02:00
|
|
|
}
|
2018-02-17 18:22:18 +02:00
|
|
|
|
2019-08-25 22:16:59 +02:00
|
|
|
// ToCacheMap converts Vars to a map containing only the static
|
2018-02-17 18:22:18 +02:00
|
|
|
// variables
|
2023-03-30 22:03:59 +02:00
|
|
|
func (vs *Vars) ToCacheMap() (m map[string]any) {
|
|
|
|
m = make(map[string]any, vs.Len())
|
2022-05-15 02:00:15 +02:00
|
|
|
_ = vs.Range(func(k string, v Var) error {
|
2018-02-17 18:22:18 +02:00
|
|
|
if v.Sh != "" {
|
|
|
|
// Dynamic variable is not yet resolved; trigger
|
|
|
|
// <no value> to be used in templates.
|
2020-03-29 21:54:59 +02:00
|
|
|
return nil
|
2018-02-17 18:22:18 +02:00
|
|
|
}
|
2019-08-25 22:16:59 +02:00
|
|
|
|
|
|
|
if v.Live != nil {
|
|
|
|
m[k] = v.Live
|
|
|
|
} else {
|
|
|
|
m[k] = v.Static
|
|
|
|
}
|
2020-03-29 21:54:59 +02:00
|
|
|
return nil
|
|
|
|
})
|
2018-02-17 18:22:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-06 13:07:57 +02:00
|
|
|
// Wrapper around OrderedMap.Set to ensure we don't get nil pointer errors
|
|
|
|
func (vs *Vars) Range(f func(k string, v Var) error) error {
|
|
|
|
if vs == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return vs.OrderedMap.Range(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrapper around OrderedMap.Merge to ensure we don't get nil pointer errors
|
|
|
|
func (vs *Vars) Merge(other *Vars) {
|
|
|
|
if vs == nil || other == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
vs.OrderedMap.Merge(other.OrderedMap)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrapper around OrderedMap.Len to ensure we don't get nil pointer errors
|
2021-03-20 16:56:19 +02:00
|
|
|
func (vs *Vars) Len() int {
|
|
|
|
if vs == nil {
|
|
|
|
return 0
|
|
|
|
}
|
2023-04-06 13:07:57 +02:00
|
|
|
return vs.OrderedMap.Len()
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeepCopy creates a new instance of Vars and copies
|
|
|
|
// data by value from the source struct.
|
|
|
|
func (vs *Vars) DeepCopy() *Vars {
|
|
|
|
if vs == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &Vars{
|
|
|
|
OrderedMap: vs.OrderedMap.DeepCopy(),
|
|
|
|
}
|
2021-03-20 16:56:19 +02:00
|
|
|
}
|
|
|
|
|
2018-02-17 18:22:18 +02:00
|
|
|
// Var represents either a static or dynamic variable.
|
|
|
|
type Var struct {
|
|
|
|
Static string
|
2023-03-30 22:03:59 +02:00
|
|
|
Live any
|
2018-02-17 18:22:18 +02:00
|
|
|
Sh string
|
2021-01-09 17:09:23 +02:00
|
|
|
Dir string
|
2018-02-17 18:22:18 +02:00
|
|
|
}
|
|
|
|
|
2022-12-19 03:11:31 +02:00
|
|
|
func (v *Var) UnmarshalYAML(node *yaml.Node) error {
|
|
|
|
switch node.Kind {
|
|
|
|
|
|
|
|
case yaml.ScalarNode:
|
|
|
|
var str string
|
|
|
|
if err := node.Decode(&str); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-02 20:15:54 +02:00
|
|
|
v.Static = str
|
2018-02-17 18:22:18 +02:00
|
|
|
return nil
|
|
|
|
|
2022-12-19 03:11:31 +02:00
|
|
|
case yaml.MappingNode:
|
|
|
|
var sh struct {
|
|
|
|
Sh string
|
|
|
|
}
|
|
|
|
if err := node.Decode(&sh); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Sh = sh.Sh
|
|
|
|
return nil
|
2018-02-17 18:22:18 +02:00
|
|
|
}
|
2022-12-19 03:11:31 +02:00
|
|
|
|
|
|
|
return fmt.Errorf("yaml: line %d: cannot unmarshal %s into variable", node.Line, node.ShortTag())
|
2018-02-17 18:22:18 +02:00
|
|
|
}
|