2023-12-29 20:32:03 +00:00
|
|
|
package ast
|
2017-07-02 15:30:50 -03:00
|
|
|
|
2022-12-19 01:11:31 +00:00
|
|
|
import (
|
|
|
|
"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-04-06 12:07:57 +01:00
|
|
|
"github.com/go-task/task/v3/internal/deepcopy"
|
2022-12-19 01:11:31 +00:00
|
|
|
)
|
|
|
|
|
2017-07-05 20:10:45 -03:00
|
|
|
// Cmd is a task command
|
2017-07-02 15:30:50 -03:00
|
|
|
type Cmd struct {
|
2018-07-10 10:44:58 +02:00
|
|
|
Cmd string
|
|
|
|
Task string
|
2023-06-15 15:04:03 +00:00
|
|
|
For *For
|
|
|
|
Silent bool
|
2023-01-14 13:41:56 -06:00
|
|
|
Set []string
|
|
|
|
Shopt []string
|
2020-03-29 16:54:59 -03:00
|
|
|
Vars *Vars
|
2018-07-10 10:44:58 +02:00
|
|
|
IgnoreError bool
|
2021-12-15 00:03:37 -05:00
|
|
|
Defer bool
|
2023-01-07 11:38:35 +11:00
|
|
|
Platforms []*Platform
|
2017-07-02 15:30:50 -03:00
|
|
|
}
|
|
|
|
|
2023-03-25 19:13:06 +00:00
|
|
|
func (c *Cmd) DeepCopy() *Cmd {
|
|
|
|
if c == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &Cmd{
|
|
|
|
Cmd: c.Cmd,
|
|
|
|
Task: c.Task,
|
2023-06-15 15:04:03 +00:00
|
|
|
For: c.For.DeepCopy(),
|
|
|
|
Silent: c.Silent,
|
2023-04-06 12:07:57 +01:00
|
|
|
Set: deepcopy.Slice(c.Set),
|
|
|
|
Shopt: deepcopy.Slice(c.Shopt),
|
2023-03-25 19:13:06 +00:00
|
|
|
Vars: c.Vars.DeepCopy(),
|
|
|
|
IgnoreError: c.IgnoreError,
|
|
|
|
Defer: c.Defer,
|
2023-04-06 12:07:57 +01:00
|
|
|
Platforms: deepcopy.Slice(c.Platforms),
|
2023-03-25 19:13:06 +00:00
|
|
|
}
|
2017-07-02 15:30:50 -03:00
|
|
|
}
|
|
|
|
|
2022-12-19 01:11:31 +00:00
|
|
|
func (c *Cmd) UnmarshalYAML(node *yaml.Node) error {
|
|
|
|
switch node.Kind {
|
|
|
|
|
|
|
|
case yaml.ScalarNode:
|
|
|
|
var cmd string
|
|
|
|
if err := node.Decode(&cmd); err != nil {
|
2024-05-16 02:24:02 +01:00
|
|
|
return errors.NewTaskfileDecodeError(err, node)
|
2022-12-19 01:11:31 +00:00
|
|
|
}
|
2022-01-02 15:15:54 -03:00
|
|
|
c.Cmd = cmd
|
2017-07-02 15:30:50 -03:00
|
|
|
return nil
|
2022-12-19 01:11:31 +00:00
|
|
|
|
|
|
|
case yaml.MappingNode:
|
|
|
|
|
|
|
|
// A command with additional options
|
|
|
|
var cmdStruct struct {
|
|
|
|
Cmd string
|
2023-06-15 15:04:03 +00:00
|
|
|
For *For
|
2022-12-19 01:11:31 +00:00
|
|
|
Silent bool
|
2023-01-14 13:41:56 -06:00
|
|
|
Set []string
|
|
|
|
Shopt []string
|
2022-12-19 01:11:31 +00:00
|
|
|
IgnoreError bool `yaml:"ignore_error"`
|
2023-01-07 11:38:35 +11:00
|
|
|
Platforms []*Platform
|
2022-12-19 01:11:31 +00:00
|
|
|
}
|
|
|
|
if err := node.Decode(&cmdStruct); err == nil && cmdStruct.Cmd != "" {
|
|
|
|
c.Cmd = cmdStruct.Cmd
|
2023-06-15 15:04:03 +00:00
|
|
|
c.For = cmdStruct.For
|
2022-12-19 01:11:31 +00:00
|
|
|
c.Silent = cmdStruct.Silent
|
2023-01-14 13:41:56 -06:00
|
|
|
c.Set = cmdStruct.Set
|
|
|
|
c.Shopt = cmdStruct.Shopt
|
2022-12-19 01:11:31 +00:00
|
|
|
c.IgnoreError = cmdStruct.IgnoreError
|
2023-01-07 11:38:35 +11:00
|
|
|
c.Platforms = cmdStruct.Platforms
|
2022-12-19 01:11:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// A deferred command
|
|
|
|
var deferredCmd struct {
|
|
|
|
Defer string
|
|
|
|
}
|
|
|
|
if err := node.Decode(&deferredCmd); err == nil && deferredCmd.Defer != "" {
|
|
|
|
c.Defer = true
|
|
|
|
c.Cmd = deferredCmd.Defer
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// A deferred task call
|
|
|
|
var deferredCall struct {
|
|
|
|
Defer Call
|
|
|
|
}
|
|
|
|
if err := node.Decode(&deferredCall); err == nil && deferredCall.Defer.Task != "" {
|
|
|
|
c.Defer = true
|
|
|
|
c.Task = deferredCall.Defer.Task
|
|
|
|
c.Vars = deferredCall.Defer.Vars
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// A task call
|
|
|
|
var taskCall struct {
|
2023-06-15 16:13:26 +00:00
|
|
|
Task string
|
|
|
|
Vars *Vars
|
|
|
|
For *For
|
|
|
|
Silent bool
|
2022-12-19 01:11:31 +00:00
|
|
|
}
|
|
|
|
if err := node.Decode(&taskCall); err == nil && taskCall.Task != "" {
|
|
|
|
c.Task = taskCall.Task
|
|
|
|
c.Vars = taskCall.Vars
|
2023-06-15 15:04:03 +00:00
|
|
|
c.For = taskCall.For
|
2023-06-15 16:13:26 +00:00
|
|
|
c.Silent = taskCall.Silent
|
2022-12-19 01:11:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-16 02:24:02 +01:00
|
|
|
return errors.NewTaskfileDecodeError(nil, node).WithMessage("invalid keys in command")
|
2017-07-02 15:30:50 -03:00
|
|
|
}
|
2022-12-19 01:11:31 +00:00
|
|
|
|
2024-05-16 02:24:02 +01:00
|
|
|
return errors.NewTaskfileDecodeError(nil, node).WithTypeMessage("command")
|
2017-07-02 15:30:50 -03:00
|
|
|
}
|