cmd.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package ast
  2. import (
  3. "gopkg.in/yaml.v3"
  4. "github.com/go-task/task/v3/errors"
  5. "github.com/go-task/task/v3/internal/deepcopy"
  6. )
  7. // Cmd is a task command
  8. type Cmd struct {
  9. Cmd string
  10. Task string
  11. For *For
  12. Silent bool
  13. Set []string
  14. Shopt []string
  15. Vars *Vars
  16. IgnoreError bool
  17. Defer bool
  18. Platforms []*Platform
  19. }
  20. func (c *Cmd) DeepCopy() *Cmd {
  21. if c == nil {
  22. return nil
  23. }
  24. return &Cmd{
  25. Cmd: c.Cmd,
  26. Task: c.Task,
  27. For: c.For.DeepCopy(),
  28. Silent: c.Silent,
  29. Set: deepcopy.Slice(c.Set),
  30. Shopt: deepcopy.Slice(c.Shopt),
  31. Vars: c.Vars.DeepCopy(),
  32. IgnoreError: c.IgnoreError,
  33. Defer: c.Defer,
  34. Platforms: deepcopy.Slice(c.Platforms),
  35. }
  36. }
  37. func (c *Cmd) UnmarshalYAML(node *yaml.Node) error {
  38. switch node.Kind {
  39. case yaml.ScalarNode:
  40. var cmd string
  41. if err := node.Decode(&cmd); err != nil {
  42. return errors.NewTaskfileDecodeError(err, node)
  43. }
  44. c.Cmd = cmd
  45. return nil
  46. case yaml.MappingNode:
  47. // A command with additional options
  48. var cmdStruct struct {
  49. Cmd string
  50. For *For
  51. Silent bool
  52. Set []string
  53. Shopt []string
  54. IgnoreError bool `yaml:"ignore_error"`
  55. Platforms []*Platform
  56. }
  57. if err := node.Decode(&cmdStruct); err == nil && cmdStruct.Cmd != "" {
  58. c.Cmd = cmdStruct.Cmd
  59. c.For = cmdStruct.For
  60. c.Silent = cmdStruct.Silent
  61. c.Set = cmdStruct.Set
  62. c.Shopt = cmdStruct.Shopt
  63. c.IgnoreError = cmdStruct.IgnoreError
  64. c.Platforms = cmdStruct.Platforms
  65. return nil
  66. }
  67. // A deferred command
  68. var deferredCmd struct {
  69. Defer string
  70. }
  71. if err := node.Decode(&deferredCmd); err == nil && deferredCmd.Defer != "" {
  72. c.Defer = true
  73. c.Cmd = deferredCmd.Defer
  74. return nil
  75. }
  76. // A deferred task call
  77. var deferredCall struct {
  78. Defer Call
  79. }
  80. if err := node.Decode(&deferredCall); err == nil && deferredCall.Defer.Task != "" {
  81. c.Defer = true
  82. c.Task = deferredCall.Defer.Task
  83. c.Vars = deferredCall.Defer.Vars
  84. return nil
  85. }
  86. // A task call
  87. var taskCall struct {
  88. Task string
  89. Vars *Vars
  90. For *For
  91. Silent bool
  92. }
  93. if err := node.Decode(&taskCall); err == nil && taskCall.Task != "" {
  94. c.Task = taskCall.Task
  95. c.Vars = taskCall.Vars
  96. c.For = taskCall.For
  97. c.Silent = taskCall.Silent
  98. return nil
  99. }
  100. return errors.NewTaskfileDecodeError(nil, node).WithMessage("invalid keys in command")
  101. }
  102. return errors.NewTaskfileDecodeError(nil, node).WithTypeMessage("command")
  103. }