opts.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package opts
  2. import (
  3. "fmt"
  4. "net"
  5. "regexp"
  6. "strings"
  7. )
  8. var (
  9. alphaRegexp = regexp.MustCompile(`[a-zA-Z]`)
  10. 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*$`)
  11. )
  12. // ListOpts holds a list of values and a validation function.
  13. type ListOpts struct {
  14. values *[]string
  15. validator ValidatorFctType
  16. }
  17. // NewListOpts creates a new ListOpts with the specified validator.
  18. func NewListOpts(validator ValidatorFctType) ListOpts {
  19. var values []string
  20. return *NewListOptsRef(&values, validator)
  21. }
  22. // NewListOptsRef creates a new ListOpts with the specified values and validator.
  23. func NewListOptsRef(values *[]string, validator ValidatorFctType) *ListOpts {
  24. return &ListOpts{
  25. values: values,
  26. validator: validator,
  27. }
  28. }
  29. func (opts *ListOpts) String() string {
  30. return fmt.Sprintf("%v", []string((*opts.values)))
  31. }
  32. // Set validates if needed the input value and add it to the
  33. // internal slice.
  34. func (opts *ListOpts) Set(value string) error {
  35. if opts.validator != nil {
  36. v, err := opts.validator(value)
  37. if err != nil {
  38. return err
  39. }
  40. value = v
  41. }
  42. (*opts.values) = append((*opts.values), value)
  43. return nil
  44. }
  45. // Delete removes the specified element from the slice.
  46. func (opts *ListOpts) Delete(key string) {
  47. for i, k := range *opts.values {
  48. if k == key {
  49. (*opts.values) = append((*opts.values)[:i], (*opts.values)[i+1:]...)
  50. return
  51. }
  52. }
  53. }
  54. // GetMap returns the content of values in a map in order to avoid
  55. // duplicates.
  56. func (opts *ListOpts) GetMap() map[string]struct{} {
  57. ret := make(map[string]struct{})
  58. for _, k := range *opts.values {
  59. ret[k] = struct{}{}
  60. }
  61. return ret
  62. }
  63. // GetAll returns the values of slice.
  64. func (opts *ListOpts) GetAll() []string {
  65. return (*opts.values)
  66. }
  67. // GetAllOrEmpty returns the values of the slice
  68. // or an empty slice when there are no values.
  69. func (opts *ListOpts) GetAllOrEmpty() []string {
  70. v := *opts.values
  71. if v == nil {
  72. return make([]string, 0)
  73. }
  74. return v
  75. }
  76. // Get checks the existence of the specified key.
  77. func (opts *ListOpts) Get(key string) bool {
  78. for _, k := range *opts.values {
  79. if k == key {
  80. return true
  81. }
  82. }
  83. return false
  84. }
  85. // Len returns the amount of element in the slice.
  86. func (opts *ListOpts) Len() int {
  87. return len((*opts.values))
  88. }
  89. //MapOpts holds a map of values and a validation function.
  90. type MapOpts struct {
  91. values map[string]string
  92. validator ValidatorFctType
  93. }
  94. // Set validates if needed the input value and add it to the
  95. // internal map, by splitting on '='.
  96. func (opts *MapOpts) Set(value string) error {
  97. if opts.validator != nil {
  98. v, err := opts.validator(value)
  99. if err != nil {
  100. return err
  101. }
  102. value = v
  103. }
  104. vals := strings.SplitN(value, "=", 2)
  105. if len(vals) == 1 {
  106. (opts.values)[vals[0]] = ""
  107. } else {
  108. (opts.values)[vals[0]] = vals[1]
  109. }
  110. return nil
  111. }
  112. // GetAll returns the values of MapOpts as a map.
  113. func (opts *MapOpts) GetAll() map[string]string {
  114. return opts.values
  115. }
  116. func (opts *MapOpts) String() string {
  117. return fmt.Sprintf("%v", map[string]string((opts.values)))
  118. }
  119. // NewMapOpts creates a new MapOpts with the specified map of values and a validator.
  120. func NewMapOpts(values map[string]string, validator ValidatorFctType) *MapOpts {
  121. if values == nil {
  122. values = make(map[string]string)
  123. }
  124. return &MapOpts{
  125. values: values,
  126. validator: validator,
  127. }
  128. }
  129. // ValidatorFctType defines a validator function that returns a validated string and/or an error.
  130. type ValidatorFctType func(val string) (string, error)
  131. // ValidatorFctListType defines a validator function that returns a validated list of string and/or an error
  132. type ValidatorFctListType func(val string) ([]string, error)
  133. // ValidateIPAddress validates an Ip address.
  134. func ValidateIPAddress(val string) (string, error) {
  135. var ip = net.ParseIP(strings.TrimSpace(val))
  136. if ip != nil {
  137. return ip.String(), nil
  138. }
  139. return "", fmt.Errorf("%s is not an ip address", val)
  140. }
  141. // ValidateDNSSearch validates domain for resolvconf search configuration.
  142. // A zero length domain is represented by a dot (.).
  143. func ValidateDNSSearch(val string) (string, error) {
  144. if val = strings.Trim(val, " "); val == "." {
  145. return val, nil
  146. }
  147. return validateDomain(val)
  148. }
  149. func validateDomain(val string) (string, error) {
  150. if alphaRegexp.FindString(val) == "" {
  151. return "", fmt.Errorf("%s is not a valid domain", val)
  152. }
  153. ns := domainRegexp.FindSubmatch([]byte(val))
  154. if len(ns) > 0 && len(ns[1]) < 255 {
  155. return string(ns[1]), nil
  156. }
  157. return "", fmt.Errorf("%s is not a valid domain", val)
  158. }
  159. // ValidateLabel validates that the specified string is a valid label, and returns it.
  160. // Labels are in the form on key=value.
  161. func ValidateLabel(val string) (string, error) {
  162. if strings.Count(val, "=") < 1 {
  163. return "", fmt.Errorf("bad attribute format: %s", val)
  164. }
  165. return val, nil
  166. }