2018-02-17 18:22:18 +02:00
|
|
|
package taskfile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2020-03-29 21:54:59 +02:00
|
|
|
|
2022-09-18 01:25:55 +02:00
|
|
|
"golang.org/x/exp/slices"
|
2020-03-29 21:54:59 +02:00
|
|
|
"gopkg.in/yaml.v3"
|
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 {
|
|
|
|
Keys []string
|
|
|
|
Mapping map[string]Var
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
|
|
|
func (vs *Vars) UnmarshalYAML(node *yaml.Node) error {
|
|
|
|
if node.Kind != yaml.MappingNode {
|
|
|
|
return errors.New("task: vars is not a map")
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE(@andreynering): on this style of custom unmarsheling,
|
|
|
|
// even number contains the keys, while odd numbers contains
|
|
|
|
// the values.
|
|
|
|
for i := 0; i < len(node.Content); i += 2 {
|
|
|
|
keyNode := node.Content[i]
|
|
|
|
valueNode := node.Content[i+1]
|
|
|
|
|
|
|
|
var v Var
|
|
|
|
if err := valueNode.Decode(&v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
vs.Set(keyNode.Value, v)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-02 07:45:27 +02:00
|
|
|
// 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{
|
|
|
|
Keys: deepCopySlice(vs.Keys),
|
|
|
|
Mapping: deepCopyMap(vs.Mapping),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-29 21:54:59 +02:00
|
|
|
// Merge merges the given Vars into the caller one
|
|
|
|
func (vs *Vars) Merge(other *Vars) {
|
2022-05-15 02:00:15 +02:00
|
|
|
_ = other.Range(func(key string, value Var) error {
|
2020-03-29 21:54:59 +02:00
|
|
|
vs.Set(key, value)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set sets a value to a given key
|
|
|
|
func (vs *Vars) Set(key string, value Var) {
|
|
|
|
if vs.Mapping == nil {
|
|
|
|
vs.Mapping = make(map[string]Var, 1)
|
|
|
|
}
|
2022-09-18 01:25:55 +02:00
|
|
|
if !slices.Contains(vs.Keys, key) {
|
2020-03-29 21:54:59 +02:00
|
|
|
vs.Keys = append(vs.Keys, key)
|
|
|
|
}
|
|
|
|
vs.Mapping[key] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
// Range allows you to loop into the vars in its right order
|
|
|
|
func (vs *Vars) Range(yield func(key string, value Var) error) error {
|
|
|
|
if vs == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, k := range vs.Keys {
|
|
|
|
if err := yield(k, vs.Mapping[k]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
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
|
2020-03-29 21:54:59 +02:00
|
|
|
func (vs *Vars) ToCacheMap() (m map[string]interface{}) {
|
2021-03-20 16:56:19 +02:00
|
|
|
m = make(map[string]interface{}, 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
|
|
|
|
}
|
|
|
|
|
2021-03-20 16:56:19 +02:00
|
|
|
// Len returns the size of the map
|
|
|
|
func (vs *Vars) Len() int {
|
|
|
|
if vs == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return len(vs.Keys)
|
|
|
|
}
|
|
|
|
|
2018-02-17 18:22:18 +02:00
|
|
|
// Var represents either a static or dynamic variable.
|
|
|
|
type Var struct {
|
|
|
|
Static string
|
2019-08-25 22:16:59 +02:00
|
|
|
Live interface{}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML implements yaml.Unmarshaler interface.
|
|
|
|
func (v *Var) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
var str string
|
|
|
|
if err := unmarshal(&str); err == nil {
|
2022-01-02 20:15:54 +02:00
|
|
|
v.Static = str
|
2018-02-17 18:22:18 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var sh struct {
|
|
|
|
Sh string
|
|
|
|
}
|
2021-04-17 22:12:39 +02:00
|
|
|
if err := unmarshal(&sh); err != nil {
|
|
|
|
return err
|
2018-02-17 18:22:18 +02:00
|
|
|
}
|
2021-04-17 22:12:39 +02:00
|
|
|
v.Sh = sh.Sh
|
|
|
|
return nil
|
2018-02-17 18:22:18 +02:00
|
|
|
}
|