2023-12-29 20:32:03 +00:00
|
|
|
package ast
|
2018-02-17 14:22:18 -02:00
|
|
|
|
|
|
|
import (
|
2023-11-30 01:56:30 +00:00
|
|
|
"strings"
|
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"
|
2023-11-29 16:24:56 +00:00
|
|
|
"github.com/go-task/task/v3/internal/experiments"
|
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 {
|
2025-02-08 23:02:51 +00:00
|
|
|
if experiments.MapVariables.Enabled() {
|
2023-12-23 04:59:10 +00:00
|
|
|
|
|
|
|
// This implementation is not backwards-compatible and replaces the 'sh' key with map variables
|
2024-04-09 12:14:14 +01:00
|
|
|
if experiments.MapVariables.Value == "1" {
|
2023-12-23 04:59:10 +00:00
|
|
|
var value any
|
|
|
|
if err := node.Decode(&value); err != nil {
|
2024-05-16 02:24:02 +01:00
|
|
|
return errors.NewTaskfileDecodeError(err, node)
|
2023-12-23 04:59:10 +00:00
|
|
|
}
|
|
|
|
// If the value is a string and it starts with $, then it's a shell command
|
|
|
|
if str, ok := value.(string); ok {
|
|
|
|
if str, ok = strings.CutPrefix(str, "$"); ok {
|
2024-11-04 13:30:39 +00:00
|
|
|
v.Sh = &str
|
2023-12-23 04:59:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-05-16 16:20:59 +01:00
|
|
|
if str, ok = strings.CutPrefix(str, "#"); ok {
|
|
|
|
v.Ref = str
|
|
|
|
return nil
|
|
|
|
}
|
2023-12-23 04:59:10 +00:00
|
|
|
}
|
|
|
|
v.Value = value
|
|
|
|
return nil
|
2023-11-29 16:24:56 +00:00
|
|
|
}
|
2023-12-23 04:59:10 +00:00
|
|
|
|
|
|
|
// This implementation IS backwards-compatible and keeps the 'sh' key and allows map variables to be added under the `map` key
|
2024-04-09 12:14:14 +01:00
|
|
|
if experiments.MapVariables.Value == "2" {
|
2023-12-23 04:59:10 +00:00
|
|
|
switch node.Kind {
|
|
|
|
case yaml.MappingNode:
|
|
|
|
key := node.Content[0].Value
|
|
|
|
switch key {
|
2024-05-16 16:20:59 +01:00
|
|
|
case "sh", "ref", "map":
|
2023-12-23 04:59:10 +00:00
|
|
|
var m struct {
|
2024-11-04 13:30:39 +00:00
|
|
|
Sh *string
|
2024-05-16 16:20:59 +01:00
|
|
|
Ref string
|
|
|
|
Map any
|
2023-12-23 04:59:10 +00:00
|
|
|
}
|
|
|
|
if err := node.Decode(&m); err != nil {
|
2024-05-16 02:24:02 +01:00
|
|
|
return errors.NewTaskfileDecodeError(err, node)
|
2023-12-23 04:59:10 +00:00
|
|
|
}
|
|
|
|
v.Sh = m.Sh
|
2023-12-29 03:49:12 +00:00
|
|
|
v.Ref = m.Ref
|
2023-12-23 04:59:10 +00:00
|
|
|
v.Value = m.Map
|
|
|
|
return nil
|
|
|
|
default:
|
2024-05-16 16:20:59 +01:00
|
|
|
return errors.NewTaskfileDecodeError(nil, node).WithMessage(`%q is not a valid variable type. Try "sh", "ref", "map" or using a scalar value`, key)
|
2023-12-23 04:59:10 +00: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)
|
2023-12-23 04:59:10 +00:00
|
|
|
}
|
|
|
|
v.Value = value
|
2023-11-30 01:56:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2023-11-29 16:24:56 +00:00
|
|
|
}
|
|
|
|
|
2022-12-19 01:11:31 +00:00
|
|
|
switch node.Kind {
|
|
|
|
|
|
|
|
case yaml.MappingNode:
|
2024-05-16 16:20:59 +01:00
|
|
|
key := node.Content[0].Value
|
|
|
|
switch key {
|
|
|
|
case "sh", "ref":
|
|
|
|
var m struct {
|
2024-11-04 13:30:39 +00:00
|
|
|
Sh *string
|
2024-05-16 16:20:59 +01:00
|
|
|
Ref string
|
|
|
|
}
|
|
|
|
if err := node.Decode(&m); err != nil {
|
|
|
|
return errors.NewTaskfileDecodeError(err, node)
|
|
|
|
}
|
|
|
|
v.Sh = m.Sh
|
|
|
|
v.Ref = m.Ref
|
|
|
|
return nil
|
|
|
|
default:
|
2024-05-16 02:24:02 +01:00
|
|
|
return errors.NewTaskfileDecodeError(nil, node).WithMessage("maps cannot be assigned to variables")
|
2024-04-09 12:14:14 +01:00
|
|
|
}
|
2022-12-19 01:11:31 +00:00
|
|
|
|
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
|
|
|
}
|