2023-12-29 20:32:03 +00:00
|
|
|
package ast
|
2018-02-17 14:22:18 -02:00
|
|
|
|
|
|
|
import (
|
2020-03-29 16:54:59 -03:00
|
|
|
"gopkg.in/yaml.v3"
|
2023-04-06 12:07:57 +01:00
|
|
|
|
2024-05-16 02:24:02 +01:00
|
|
|
"github.com/go-task/task/v3/errors"
|
2018-02-17 14:22:18 -02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Var represents either a static or dynamic variable.
|
|
|
|
type Var struct {
|
2023-11-29 16:21:21 +00:00
|
|
|
Value any
|
2023-11-28 18:18:28 +00:00
|
|
|
Live any
|
2024-11-04 13:30:39 +00:00
|
|
|
Sh *string
|
2023-12-29 03:49:12 +00:00
|
|
|
Ref string
|
2023-11-28 18:18:28 +00:00
|
|
|
Dir string
|
2018-02-17 14:22:18 -02:00
|
|
|
}
|
|
|
|
|
2022-12-19 01:11:31 +00:00
|
|
|
func (v *Var) UnmarshalYAML(node *yaml.Node) error {
|
|
|
|
switch node.Kind {
|
|
|
|
case yaml.MappingNode:
|
2024-05-16 16:20:59 +01:00
|
|
|
key := node.Content[0].Value
|
|
|
|
switch key {
|
2025-03-26 21:40:09 +00:00
|
|
|
case "sh", "ref", "map":
|
2024-05-16 16:20:59 +01:00
|
|
|
var m struct {
|
2024-11-04 13:30:39 +00:00
|
|
|
Sh *string
|
2024-05-16 16:20:59 +01:00
|
|
|
Ref string
|
2025-03-26 21:40:09 +00:00
|
|
|
Map any
|
2024-05-16 16:20:59 +01:00
|
|
|
}
|
|
|
|
if err := node.Decode(&m); err != nil {
|
|
|
|
return errors.NewTaskfileDecodeError(err, node)
|
|
|
|
}
|
|
|
|
v.Sh = m.Sh
|
|
|
|
v.Ref = m.Ref
|
2025-03-26 21:40:09 +00:00
|
|
|
v.Value = m.Map
|
2024-05-16 16:20:59 +01:00
|
|
|
return nil
|
|
|
|
default:
|
2025-03-26 21:40:09 +00:00
|
|
|
return errors.NewTaskfileDecodeError(nil, node).WithMessage(`%q is not a valid variable type. Try "sh", "ref", "map" or using a scalar value`, key)
|
2024-04-09 12:14:14 +01:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
var value any
|
|
|
|
if err := node.Decode(&value); err != nil {
|
2024-05-16 02:24:02 +01:00
|
|
|
return errors.NewTaskfileDecodeError(err, node)
|
2024-04-09 12:14:14 +01:00
|
|
|
}
|
|
|
|
v.Value = value
|
|
|
|
return nil
|
|
|
|
}
|
2018-02-17 14:22:18 -02:00
|
|
|
}
|