parsers.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 // import "github.com/docker/docker/pkg/parsers"
  5. import (
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. )
  10. // ParseKeyValueOpt parses and validates the specified string as a key/value pair (key=value)
  11. func ParseKeyValueOpt(opt string) (string, string, error) {
  12. parts := strings.SplitN(opt, "=", 2)
  13. if len(parts) != 2 {
  14. return "", "", fmt.Errorf("Unable to parse key/value option: %s", opt)
  15. }
  16. return strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1]), nil
  17. }
  18. // ParseUintListMaximum parses and validates the specified string as the value
  19. // found in some cgroup file (e.g. `cpuset.cpus`, `cpuset.mems`), which could be
  20. // one of the formats below. Note that duplicates are actually allowed in the
  21. // input string. It returns a `map[int]bool` with available elements from `val`
  22. // set to `true`. Values larger than `maximum` cause an error if max is non zero,
  23. // in order to stop the map becoming excessively large.
  24. // Supported formats:
  25. //
  26. // 7
  27. // 1-6
  28. // 0,3-4,7,8-10
  29. // 0-0,0,1-7
  30. // 03,1-3 <- this is gonna get parsed as [1,2,3]
  31. // 3,2,1
  32. // 0-2,3,1
  33. func ParseUintListMaximum(val string, maximum int) (map[int]bool, error) {
  34. return parseUintList(val, maximum)
  35. }
  36. // ParseUintList parses and validates the specified string as the value
  37. // found in some cgroup file (e.g. `cpuset.cpus`, `cpuset.mems`), which could be
  38. // one of the formats below. Note that duplicates are actually allowed in the
  39. // input string. It returns a `map[int]bool` with available elements from `val`
  40. // set to `true`.
  41. // Supported formats:
  42. //
  43. // 7
  44. // 1-6
  45. // 0,3-4,7,8-10
  46. // 0-0,0,1-7
  47. // 03,1-3 <- this is gonna get parsed as [1,2,3]
  48. // 3,2,1
  49. // 0-2,3,1
  50. func ParseUintList(val string) (map[int]bool, error) {
  51. return parseUintList(val, 0)
  52. }
  53. func parseUintList(val string, maximum int) (map[int]bool, error) {
  54. if val == "" {
  55. return map[int]bool{}, nil
  56. }
  57. availableInts := make(map[int]bool)
  58. split := strings.Split(val, ",")
  59. errInvalidFormat := fmt.Errorf("invalid format: %s", val)
  60. for _, r := range split {
  61. if !strings.Contains(r, "-") {
  62. v, err := strconv.Atoi(r)
  63. if err != nil {
  64. return nil, errInvalidFormat
  65. }
  66. if maximum != 0 && v > maximum {
  67. return nil, fmt.Errorf("value of out range, maximum is %d", maximum)
  68. }
  69. availableInts[v] = true
  70. } else {
  71. split := strings.SplitN(r, "-", 2)
  72. min, err := strconv.Atoi(split[0])
  73. if err != nil {
  74. return nil, errInvalidFormat
  75. }
  76. max, err := strconv.Atoi(split[1])
  77. if err != nil {
  78. return nil, errInvalidFormat
  79. }
  80. if max < min {
  81. return nil, errInvalidFormat
  82. }
  83. if maximum != 0 && max > maximum {
  84. return nil, fmt.Errorf("value of out range, maximum is %d", maximum)
  85. }
  86. for i := min; i <= max; i++ {
  87. availableInts[i] = true
  88. }
  89. }
  90. }
  91. return availableInts, nil
  92. }