2018-02-17 18:22:18 +02:00
|
|
|
package taskfile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrCantUnmarshalVar is returned for invalid var YAML.
|
|
|
|
ErrCantUnmarshalVar = errors.New("task: can't unmarshal var value")
|
|
|
|
)
|
|
|
|
|
|
|
|
// Vars is a string[string] variables map.
|
|
|
|
type Vars map[string]Var
|
|
|
|
|
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
|
2019-08-25 22:16:59 +02:00
|
|
|
func (vs Vars) ToCacheMap() (m map[string](interface{})) {
|
|
|
|
m = make(map[string](interface{}), len(vs))
|
2018-02-17 18:22:18 +02:00
|
|
|
for k, v := range vs {
|
|
|
|
if v.Sh != "" {
|
|
|
|
// Dynamic variable is not yet resolved; trigger
|
|
|
|
// <no value> to be used in templates.
|
|
|
|
continue
|
|
|
|
}
|
2019-08-25 22:16:59 +02:00
|
|
|
|
|
|
|
if v.Live != nil {
|
|
|
|
m[k] = v.Live
|
|
|
|
} else {
|
|
|
|
m[k] = v.Static
|
|
|
|
}
|
2018-02-17 18:22:18 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML implements yaml.Unmarshaler interface.
|
|
|
|
func (v *Var) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
var str string
|
|
|
|
if err := unmarshal(&str); err == nil {
|
|
|
|
if strings.HasPrefix(str, "$") {
|
|
|
|
v.Sh = strings.TrimPrefix(str, "$")
|
|
|
|
} else {
|
|
|
|
v.Static = str
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var sh struct {
|
|
|
|
Sh string
|
|
|
|
}
|
|
|
|
if err := unmarshal(&sh); err == nil {
|
|
|
|
v.Sh = sh.Sh
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ErrCantUnmarshalVar
|
|
|
|
}
|