opts.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package opts
  2. import (
  3. "fmt"
  4. "net"
  5. "os"
  6. "path"
  7. "regexp"
  8. "strings"
  9. "github.com/docker/docker/api"
  10. flag "github.com/docker/docker/pkg/mflag"
  11. "github.com/docker/docker/pkg/parsers"
  12. )
  13. var (
  14. alphaRegexp = regexp.MustCompile(`[a-zA-Z]`)
  15. domainRegexp = regexp.MustCompile(`^(:?(:?[a-zA-Z0-9]|(:?[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9]))(:?\.(:?[a-zA-Z0-9]|(:?[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])))*)\.?\s*$`)
  16. )
  17. func ListVar(values *[]string, names []string, usage string) {
  18. flag.Var(newListOptsRef(values, nil), names, usage)
  19. }
  20. func HostListVar(values *[]string, names []string, usage string) {
  21. flag.Var(newListOptsRef(values, api.ValidateHost), names, usage)
  22. }
  23. func IPListVar(values *[]string, names []string, usage string) {
  24. flag.Var(newListOptsRef(values, ValidateIPAddress), names, usage)
  25. }
  26. func DnsSearchListVar(values *[]string, names []string, usage string) {
  27. flag.Var(newListOptsRef(values, ValidateDnsSearch), names, usage)
  28. }
  29. func IPVar(value *net.IP, names []string, defaultValue, usage string) {
  30. flag.Var(NewIpOpt(value, defaultValue), names, usage)
  31. }
  32. func LabelListVar(values *[]string, names []string, usage string) {
  33. flag.Var(newListOptsRef(values, ValidateLabel), names, usage)
  34. }
  35. // ListOpts type
  36. type ListOpts struct {
  37. values *[]string
  38. validator ValidatorFctType
  39. }
  40. func NewListOpts(validator ValidatorFctType) ListOpts {
  41. var values []string
  42. return *newListOptsRef(&values, validator)
  43. }
  44. func newListOptsRef(values *[]string, validator ValidatorFctType) *ListOpts {
  45. return &ListOpts{
  46. values: values,
  47. validator: validator,
  48. }
  49. }
  50. func (opts *ListOpts) String() string {
  51. return fmt.Sprintf("%v", []string((*opts.values)))
  52. }
  53. // Set validates if needed the input value and add it to the
  54. // internal slice.
  55. func (opts *ListOpts) Set(value string) error {
  56. if opts.validator != nil {
  57. v, err := opts.validator(value)
  58. if err != nil {
  59. return err
  60. }
  61. value = v
  62. }
  63. (*opts.values) = append((*opts.values), value)
  64. return nil
  65. }
  66. // Delete remove the given element from the slice.
  67. func (opts *ListOpts) Delete(key string) {
  68. for i, k := range *opts.values {
  69. if k == key {
  70. (*opts.values) = append((*opts.values)[:i], (*opts.values)[i+1:]...)
  71. return
  72. }
  73. }
  74. }
  75. // GetMap returns the content of values in a map in order to avoid
  76. // duplicates.
  77. // FIXME: can we remove this?
  78. func (opts *ListOpts) GetMap() map[string]struct{} {
  79. ret := make(map[string]struct{})
  80. for _, k := range *opts.values {
  81. ret[k] = struct{}{}
  82. }
  83. return ret
  84. }
  85. // GetAll returns the values' slice.
  86. // FIXME: Can we remove this?
  87. func (opts *ListOpts) GetAll() []string {
  88. return (*opts.values)
  89. }
  90. // Get checks the existence of the given key.
  91. func (opts *ListOpts) Get(key string) bool {
  92. for _, k := range *opts.values {
  93. if k == key {
  94. return true
  95. }
  96. }
  97. return false
  98. }
  99. // Len returns the amount of element in the slice.
  100. func (opts *ListOpts) Len() int {
  101. return len((*opts.values))
  102. }
  103. // Validators
  104. type ValidatorFctType func(val string) (string, error)
  105. type ValidatorFctListType func(val string) ([]string, error)
  106. func ValidateAttach(val string) (string, error) {
  107. s := strings.ToLower(val)
  108. for _, str := range []string{"stdin", "stdout", "stderr"} {
  109. if s == str {
  110. return s, nil
  111. }
  112. }
  113. return val, fmt.Errorf("valid streams are STDIN, STDOUT and STDERR.")
  114. }
  115. func ValidateLink(val string) (string, error) {
  116. if _, err := parsers.PartParser("name:alias", val); err != nil {
  117. return val, err
  118. }
  119. return val, nil
  120. }
  121. func ValidatePath(val string) (string, error) {
  122. var containerPath string
  123. if strings.Count(val, ":") > 2 {
  124. return val, fmt.Errorf("bad format for volumes: %s", val)
  125. }
  126. splited := strings.SplitN(val, ":", 2)
  127. if len(splited) == 1 {
  128. containerPath = splited[0]
  129. val = path.Clean(splited[0])
  130. } else {
  131. containerPath = splited[1]
  132. val = fmt.Sprintf("%s:%s", splited[0], path.Clean(splited[1]))
  133. }
  134. if !path.IsAbs(containerPath) {
  135. return val, fmt.Errorf("%s is not an absolute path", containerPath)
  136. }
  137. return val, nil
  138. }
  139. func ValidateEnv(val string) (string, error) {
  140. arr := strings.Split(val, "=")
  141. if len(arr) > 1 {
  142. return val, nil
  143. }
  144. return fmt.Sprintf("%s=%s", val, os.Getenv(val)), nil
  145. }
  146. func ValidateIPAddress(val string) (string, error) {
  147. var ip = net.ParseIP(strings.TrimSpace(val))
  148. if ip != nil {
  149. return ip.String(), nil
  150. }
  151. return "", fmt.Errorf("%s is not an ip address", val)
  152. }
  153. // Validates domain for resolvconf search configuration.
  154. // A zero length domain is represented by .
  155. func ValidateDnsSearch(val string) (string, error) {
  156. if val = strings.Trim(val, " "); val == "." {
  157. return val, nil
  158. }
  159. return validateDomain(val)
  160. }
  161. func validateDomain(val string) (string, error) {
  162. if alphaRegexp.FindString(val) == "" {
  163. return "", fmt.Errorf("%s is not a valid domain", val)
  164. }
  165. ns := domainRegexp.FindSubmatch([]byte(val))
  166. if len(ns) > 0 {
  167. return string(ns[1]), nil
  168. }
  169. return "", fmt.Errorf("%s is not a valid domain", val)
  170. }
  171. func ValidateExtraHost(val string) (string, error) {
  172. arr := strings.Split(val, ":")
  173. if len(arr) != 2 || len(arr[0]) == 0 {
  174. return "", fmt.Errorf("bad format for add-host: %s", val)
  175. }
  176. if _, err := ValidateIPAddress(arr[1]); err != nil {
  177. return "", fmt.Errorf("bad format for add-host: %s", val)
  178. }
  179. return val, nil
  180. }
  181. func ValidateLabel(val string) (string, error) {
  182. if strings.Count(val, "=") != 1 {
  183. return "", fmt.Errorf("bad attribute format: %s", val)
  184. }
  185. return val, nil
  186. }