2023-12-29 20:32:03 +00:00
package ast
2018-02-17 14:22:18 -02:00
import (
2025-11-11 16:49:37 -03:00
"go.yaml.in/yaml/v4"
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 {
2025-11-15 21:52:04 +01:00
Value any
Live any
Sh * string
Ref string
Dir string
Secret bool
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 :
2025-09-12 20:29:40 +02:00
key := "<none>"
if len ( node . Content ) > 0 {
key = node . Content [ 0 ] . Value
}
2024-05-16 16:20:59 +01:00
switch key {
2025-11-15 21:52:04 +01:00
case "sh" , "ref" , "map" , "value" :
2024-05-16 16:20:59 +01:00
var m struct {
2025-11-15 21:52:04 +01:00
Sh * string
Ref string
Map any
Value any
Secret bool
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-11-15 21:52:04 +01:00
v . Secret = m . Secret
// Handle both "map" and "value" keys
if m . Map != nil {
v . Value = m . Map
} else if m . Value != nil {
v . Value = m . Value
}
2024-05-16 16:20:59 +01:00
return nil
default :
2025-11-15 21:52:04 +01:00
return errors . NewTaskfileDecodeError ( nil , node ) . WithMessage ( ` %q is not a valid variable type. Try "sh", "ref", "map", "value" 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
}