precondition.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package ast
  2. import (
  3. "fmt"
  4. "gopkg.in/yaml.v3"
  5. "github.com/go-task/task/v3/errors"
  6. )
  7. // Precondition represents a precondition necessary for a task to run
  8. type Precondition struct {
  9. Sh string
  10. Msg string
  11. }
  12. func (p *Precondition) DeepCopy() *Precondition {
  13. if p == nil {
  14. return nil
  15. }
  16. return &Precondition{
  17. Sh: p.Sh,
  18. Msg: p.Msg,
  19. }
  20. }
  21. // UnmarshalYAML implements yaml.Unmarshaler interface.
  22. func (p *Precondition) UnmarshalYAML(node *yaml.Node) error {
  23. switch node.Kind {
  24. case yaml.ScalarNode:
  25. var cmd string
  26. if err := node.Decode(&cmd); err != nil {
  27. return errors.NewTaskfileDecodeError(err, node)
  28. }
  29. p.Sh = cmd
  30. p.Msg = fmt.Sprintf("`%s` failed", cmd)
  31. return nil
  32. case yaml.MappingNode:
  33. var sh struct {
  34. Sh string
  35. Msg string
  36. }
  37. if err := node.Decode(&sh); err != nil {
  38. return errors.NewTaskfileDecodeError(err, node)
  39. }
  40. p.Sh = sh.Sh
  41. p.Msg = sh.Msg
  42. if p.Msg == "" {
  43. p.Msg = fmt.Sprintf("%s failed", sh.Sh)
  44. }
  45. return nil
  46. }
  47. return errors.NewTaskfileDecodeError(nil, node).WithTypeMessage("precondition")
  48. }