parsers.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Package parsers provides helper functions to parse and validate different type
  2. // of string. It can be hosts, unix addresses, tcp addresses, filters, kernel
  3. // operating system versions.
  4. package parsers
  5. import (
  6. "fmt"
  7. "path"
  8. "strconv"
  9. "strings"
  10. )
  11. // PartParser parses and validates the specified string (data) using the specified template
  12. // e.g. ip:public:private -> 192.168.0.1:80:8000
  13. func PartParser(template, data string) (map[string]string, error) {
  14. // ip:public:private
  15. var (
  16. templateParts = strings.Split(template, ":")
  17. parts = strings.Split(data, ":")
  18. out = make(map[string]string, len(templateParts))
  19. )
  20. if len(parts) != len(templateParts) {
  21. return nil, fmt.Errorf("Invalid format to parse. %s should match template %s", data, template)
  22. }
  23. for i, t := range templateParts {
  24. value := ""
  25. if len(parts) > i {
  26. value = parts[i]
  27. }
  28. out[t] = value
  29. }
  30. return out, nil
  31. }
  32. // ParseKeyValueOpt parses and validates the specified string as a key/value pair (key=value)
  33. func ParseKeyValueOpt(opt string) (string, string, error) {
  34. parts := strings.SplitN(opt, "=", 2)
  35. if len(parts) != 2 {
  36. return "", "", fmt.Errorf("Unable to parse key/value option: %s", opt)
  37. }
  38. return strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1]), nil
  39. }
  40. // ParsePortRange parses and validates the specified string as a port-range (8000-9000)
  41. func ParsePortRange(ports string) (uint64, uint64, error) {
  42. if ports == "" {
  43. return 0, 0, fmt.Errorf("Empty string specified for ports.")
  44. }
  45. if !strings.Contains(ports, "-") {
  46. start, err := strconv.ParseUint(ports, 10, 16)
  47. end := start
  48. return start, end, err
  49. }
  50. parts := strings.Split(ports, "-")
  51. start, err := strconv.ParseUint(parts[0], 10, 16)
  52. if err != nil {
  53. return 0, 0, err
  54. }
  55. end, err := strconv.ParseUint(parts[1], 10, 16)
  56. if err != nil {
  57. return 0, 0, err
  58. }
  59. if end < start {
  60. return 0, 0, fmt.Errorf("Invalid range specified for the Port: %s", ports)
  61. }
  62. return start, end, nil
  63. }
  64. // ParseLink parses and validates the specified string as a link format (name:alias)
  65. func ParseLink(val string) (string, string, error) {
  66. if val == "" {
  67. return "", "", fmt.Errorf("empty string specified for links")
  68. }
  69. arr := strings.Split(val, ":")
  70. if len(arr) > 2 {
  71. return "", "", fmt.Errorf("bad format for links: %s", val)
  72. }
  73. if len(arr) == 1 {
  74. return val, val, nil
  75. }
  76. // This is kept because we can actually get an HostConfig with links
  77. // from an already created container and the format is not `foo:bar`
  78. // but `/foo:/c1/bar`
  79. if strings.HasPrefix(arr[0], "/") {
  80. _, alias := path.Split(arr[1])
  81. return arr[0][1:], alias, nil
  82. }
  83. return arr[0], arr[1], nil
  84. }
  85. // ParseUintList parses and validates the specified string as the value
  86. // found in some cgroup file (e.g. `cpuset.cpus`, `cpuset.mems`), which could be
  87. // one of the formats below. Note that duplicates are actually allowed in the
  88. // input string. It returns a `map[int]bool` with available elements from `val`
  89. // set to `true`.
  90. // Supported formats:
  91. // 7
  92. // 1-6
  93. // 0,3-4,7,8-10
  94. // 0-0,0,1-7
  95. // 03,1-3 <- this is gonna get parsed as [1,2,3]
  96. // 3,2,1
  97. // 0-2,3,1
  98. func ParseUintList(val string) (map[int]bool, error) {
  99. if val == "" {
  100. return map[int]bool{}, nil
  101. }
  102. availableInts := make(map[int]bool)
  103. split := strings.Split(val, ",")
  104. errInvalidFormat := fmt.Errorf("invalid format: %s", val)
  105. for _, r := range split {
  106. if !strings.Contains(r, "-") {
  107. v, err := strconv.Atoi(r)
  108. if err != nil {
  109. return nil, errInvalidFormat
  110. }
  111. availableInts[v] = true
  112. } else {
  113. split := strings.SplitN(r, "-", 2)
  114. min, err := strconv.Atoi(split[0])
  115. if err != nil {
  116. return nil, errInvalidFormat
  117. }
  118. max, err := strconv.Atoi(split[1])
  119. if err != nil {
  120. return nil, errInvalidFormat
  121. }
  122. if max < min {
  123. return nil, errInvalidFormat
  124. }
  125. for i := min; i <= max; i++ {
  126. availableInts[i] = true
  127. }
  128. }
  129. }
  130. return availableInts, nil
  131. }