1
1
package ast
2
2
3
3
import (
4
- "strings"
5
-
6
4
"gopkg.in/yaml.v3"
7
5
8
6
"github.com/go-task/task/v3/errors"
9
- "github.com/go-task/task/v3/internal/experiments"
10
7
)
11
8
12
9
// Var represents either a static or dynamic variable.
@@ -19,82 +16,26 @@ type Var struct {
19
16
}
20
17
21
18
func (v * Var ) UnmarshalYAML (node * yaml.Node ) error {
22
- if experiments .MapVariables .Enabled () {
23
-
24
- // This implementation is not backwards-compatible and replaces the 'sh' key with map variables
25
- if experiments .MapVariables .Value == 1 {
26
- var value any
27
- if err := node .Decode (& value ); err != nil {
28
- return errors .NewTaskfileDecodeError (err , node )
29
- }
30
- // If the value is a string and it starts with $, then it's a shell command
31
- if str , ok := value .(string ); ok {
32
- if str , ok = strings .CutPrefix (str , "$" ); ok {
33
- v .Sh = & str
34
- return nil
35
- }
36
- if str , ok = strings .CutPrefix (str , "#" ); ok {
37
- v .Ref = str
38
- return nil
39
- }
40
- }
41
- v .Value = value
42
- return nil
43
- }
44
-
45
- // This implementation IS backwards-compatible and keeps the 'sh' key and allows map variables to be added under the `map` key
46
- if experiments .MapVariables .Value == 2 {
47
- switch node .Kind {
48
- case yaml .MappingNode :
49
- key := node .Content [0 ].Value
50
- switch key {
51
- case "sh" , "ref" , "map" :
52
- var m struct {
53
- Sh * string
54
- Ref string
55
- Map any
56
- }
57
- if err := node .Decode (& m ); err != nil {
58
- return errors .NewTaskfileDecodeError (err , node )
59
- }
60
- v .Sh = m .Sh
61
- v .Ref = m .Ref
62
- v .Value = m .Map
63
- return nil
64
- default :
65
- return errors .NewTaskfileDecodeError (nil , node ).WithMessage (`%q is not a valid variable type. Try "sh", "ref", "map" or using a scalar value` , key )
66
- }
67
- default :
68
- var value any
69
- if err := node .Decode (& value ); err != nil {
70
- return errors .NewTaskfileDecodeError (err , node )
71
- }
72
- v .Value = value
73
- return nil
74
- }
75
- }
76
- }
77
-
78
19
switch node .Kind {
79
-
80
20
case yaml .MappingNode :
81
21
key := node .Content [0 ].Value
82
22
switch key {
83
- case "sh" , "ref" :
23
+ case "sh" , "ref" , "map" :
84
24
var m struct {
85
25
Sh * string
86
26
Ref string
27
+ Map any
87
28
}
88
29
if err := node .Decode (& m ); err != nil {
89
30
return errors .NewTaskfileDecodeError (err , node )
90
31
}
91
32
v .Sh = m .Sh
92
33
v .Ref = m .Ref
34
+ v .Value = m .Map
93
35
return nil
94
36
default :
95
- return errors .NewTaskfileDecodeError (nil , node ).WithMessage ("maps cannot be assigned to variables" )
37
+ return errors .NewTaskfileDecodeError (nil , node ).WithMessage (`%q is not a valid variable type. Try "sh", "ref", "map" or using a scalar value` , key )
96
38
}
97
-
98
39
default :
99
40
var value any
100
41
if err := node .Decode (& value ); err != nil {
0 commit comments