1
0

platforms.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package ast
  2. import (
  3. "fmt"
  4. "strings"
  5. "gopkg.in/yaml.v3"
  6. "github.com/go-task/task/v3/errors"
  7. "github.com/go-task/task/v3/internal/goext"
  8. )
  9. // Platform represents GOOS and GOARCH values
  10. type Platform struct {
  11. OS string
  12. Arch string
  13. }
  14. func (p *Platform) DeepCopy() *Platform {
  15. if p == nil {
  16. return nil
  17. }
  18. return &Platform{
  19. OS: p.OS,
  20. Arch: p.Arch,
  21. }
  22. }
  23. type ErrInvalidPlatform struct {
  24. Platform string
  25. }
  26. func (err *ErrInvalidPlatform) Error() string {
  27. return fmt.Sprintf(`invalid platform "%s"`, err.Platform)
  28. }
  29. // UnmarshalYAML implements yaml.Unmarshaler interface.
  30. func (p *Platform) UnmarshalYAML(node *yaml.Node) error {
  31. switch node.Kind {
  32. case yaml.ScalarNode:
  33. var platform string
  34. if err := node.Decode(&platform); err != nil {
  35. return errors.NewTaskfileDecodeError(err, node)
  36. }
  37. if err := p.parsePlatform(platform); err != nil {
  38. return errors.NewTaskfileDecodeError(err, node)
  39. }
  40. return nil
  41. }
  42. return errors.NewTaskfileDecodeError(nil, node).WithTypeMessage("platform")
  43. }
  44. // parsePlatform takes a string representing an OS/Arch combination (or either on their own)
  45. // and parses it into the Platform struct. It returns an error if the input string is invalid.
  46. // Valid combinations for input: OS, Arch, OS/Arch
  47. func (p *Platform) parsePlatform(input string) error {
  48. splitValues := strings.Split(input, "/")
  49. if len(splitValues) > 2 {
  50. return &ErrInvalidPlatform{Platform: input}
  51. }
  52. if err := p.parseOsOrArch(splitValues[0]); err != nil {
  53. return &ErrInvalidPlatform{Platform: input}
  54. }
  55. if len(splitValues) == 2 {
  56. if err := p.parseArch(splitValues[1]); err != nil {
  57. return &ErrInvalidPlatform{Platform: input}
  58. }
  59. }
  60. return nil
  61. }
  62. // parseOsOrArch will check if the given input is a valid OS or Arch value.
  63. // If so, it will store it. If not, an error is returned
  64. func (p *Platform) parseOsOrArch(osOrArch string) error {
  65. if osOrArch == "" {
  66. return fmt.Errorf("task: Blank OS/Arch value provided")
  67. }
  68. if goext.IsKnownOS(osOrArch) {
  69. p.OS = osOrArch
  70. return nil
  71. }
  72. if goext.IsKnownArch(osOrArch) {
  73. p.Arch = osOrArch
  74. return nil
  75. }
  76. return fmt.Errorf("task: Invalid OS/Arch value provided (%s)", osOrArch)
  77. }
  78. func (p *Platform) parseArch(arch string) error {
  79. if arch == "" {
  80. return fmt.Errorf("task: Blank Arch value provided")
  81. }
  82. if p.Arch != "" {
  83. return fmt.Errorf("task: Multiple Arch values provided")
  84. }
  85. if goext.IsKnownArch(arch) {
  86. p.Arch = arch
  87. return nil
  88. }
  89. return fmt.Errorf("task: Invalid Arch value provided (%s)", arch)
  90. }