2018-02-17 18:22:18 +02:00
|
|
|
package taskfile
|
2017-07-02 20:30:50 +02:00
|
|
|
|
2022-12-19 03:11:31 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v3"
|
2023-04-06 13:07:57 +02:00
|
|
|
|
|
|
|
"github.com/go-task/task/v3/internal/deepcopy"
|
2022-12-19 03:11:31 +02:00
|
|
|
)
|
|
|
|
|
2017-07-06 01:10:45 +02:00
|
|
|
// Cmd is a task command
|
2017-07-02 20:30:50 +02:00
|
|
|
type Cmd struct {
|
2018-07-10 10:44:58 +02:00
|
|
|
Cmd string
|
|
|
|
Silent bool
|
|
|
|
Task string
|
2023-01-14 21:41:56 +02:00
|
|
|
Set []string
|
|
|
|
Shopt []string
|
2020-03-29 21:54:59 +02:00
|
|
|
Vars *Vars
|
2018-07-10 10:44:58 +02:00
|
|
|
IgnoreError bool
|
2021-12-15 07:03:37 +02:00
|
|
|
Defer bool
|
2023-01-07 02:38:35 +02:00
|
|
|
Platforms []*Platform
|
2017-07-02 20:30:50 +02:00
|
|
|
}
|
|
|
|
|
2023-03-25 21:13:06 +02:00
|
|
|
func (c *Cmd) DeepCopy() *Cmd {
|
|
|
|
if c == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &Cmd{
|
|
|
|
Cmd: c.Cmd,
|
|
|
|
Silent: c.Silent,
|
|
|
|
Task: c.Task,
|
2023-04-06 13:07:57 +02:00
|
|
|
Set: deepcopy.Slice(c.Set),
|
|
|
|
Shopt: deepcopy.Slice(c.Shopt),
|
2023-03-25 21:13:06 +02:00
|
|
|
Vars: c.Vars.DeepCopy(),
|
|
|
|
IgnoreError: c.IgnoreError,
|
|
|
|
Defer: c.Defer,
|
2023-04-06 13:07:57 +02:00
|
|
|
Platforms: deepcopy.Slice(c.Platforms),
|
2023-03-25 21:13:06 +02:00
|
|
|
}
|
2017-07-02 20:30:50 +02:00
|
|
|
}
|
|
|
|
|
2022-12-19 03:11:31 +02: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 {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-02 20:15:54 +02:00
|
|
|
c.Cmd = cmd
|
2017-07-02 20:30:50 +02:00
|
|
|
return nil
|
2022-12-19 03:11:31 +02:00
|
|
|
|
|
|
|
case yaml.MappingNode:
|
|
|
|
|
|
|
|
// A command with additional options
|
|
|
|
var cmdStruct struct {
|
|
|
|
Cmd string
|
|
|
|
Silent bool
|
2023-01-14 21:41:56 +02:00
|
|
|
Set []string
|
|
|
|
Shopt []string
|
2022-12-19 03:11:31 +02:00
|
|
|
IgnoreError bool `yaml:"ignore_error"`
|
2023-01-07 02:38:35 +02:00
|
|
|
Platforms []*Platform
|
2022-12-19 03:11:31 +02:00
|
|
|
}
|
|
|
|
if err := node.Decode(&cmdStruct); err == nil && cmdStruct.Cmd != "" {
|
|
|
|
c.Cmd = cmdStruct.Cmd
|
|
|
|
c.Silent = cmdStruct.Silent
|
2023-01-14 21:41:56 +02:00
|
|
|
c.Set = cmdStruct.Set
|
|
|
|
c.Shopt = cmdStruct.Shopt
|
2022-12-19 03:11:31 +02:00
|
|
|
c.IgnoreError = cmdStruct.IgnoreError
|
2023-01-07 02:38:35 +02:00
|
|
|
c.Platforms = cmdStruct.Platforms
|
2022-12-19 03:11:31 +02: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 {
|
|
|
|
Task string
|
|
|
|
Vars *Vars
|
|
|
|
}
|
|
|
|
if err := node.Decode(&taskCall); err == nil && taskCall.Task != "" {
|
|
|
|
c.Task = taskCall.Task
|
|
|
|
c.Vars = taskCall.Vars
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("yaml: line %d: invalid keys in command", node.Line)
|
2017-07-02 20:30:50 +02:00
|
|
|
}
|
2022-12-19 03:11:31 +02:00
|
|
|
|
|
|
|
return fmt.Errorf("yaml: line %d: cannot unmarshal %s into command", node.Line, node.ShortTag())
|
2017-07-02 20:30:50 +02:00
|
|
|
}
|