unique_urls.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2018 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package flags
  15. import (
  16. "flag"
  17. "net/url"
  18. "sort"
  19. "strings"
  20. "go.etcd.io/etcd/pkg/types"
  21. )
  22. // UniqueURLs contains unique URLs
  23. // with non-URL exceptions.
  24. type UniqueURLs struct {
  25. Values map[string]struct{}
  26. uss []url.URL
  27. Allowed map[string]struct{}
  28. }
  29. // Set parses a command line set of URLs formatted like:
  30. // http://127.0.0.1:2380,http://10.1.1.2:80
  31. // Implements "flag.Value" interface.
  32. func (us *UniqueURLs) Set(s string) error {
  33. if _, ok := us.Values[s]; ok {
  34. return nil
  35. }
  36. if _, ok := us.Allowed[s]; ok {
  37. us.Values[s] = struct{}{}
  38. return nil
  39. }
  40. ss, err := types.NewURLs(strings.Split(s, ","))
  41. if err != nil {
  42. return err
  43. }
  44. us.Values = make(map[string]struct{})
  45. us.uss = make([]url.URL, 0)
  46. for _, v := range ss {
  47. us.Values[v.String()] = struct{}{}
  48. us.uss = append(us.uss, v)
  49. }
  50. return nil
  51. }
  52. // String implements "flag.Value" interface.
  53. func (us *UniqueURLs) String() string {
  54. all := make([]string, 0, len(us.Values))
  55. for u := range us.Values {
  56. all = append(all, u)
  57. }
  58. sort.Strings(all)
  59. return strings.Join(all, ",")
  60. }
  61. // NewUniqueURLsWithExceptions implements "url.URL" slice as flag.Value interface.
  62. // Given value is to be separated by comma.
  63. func NewUniqueURLsWithExceptions(s string, exceptions ...string) *UniqueURLs {
  64. us := &UniqueURLs{Values: make(map[string]struct{}), Allowed: make(map[string]struct{})}
  65. for _, v := range exceptions {
  66. us.Allowed[v] = struct{}{}
  67. }
  68. if s == "" {
  69. return us
  70. }
  71. if err := us.Set(s); err != nil {
  72. plog.Panicf("new UniqueURLs should never fail: %v", err)
  73. }
  74. return us
  75. }
  76. // UniqueURLsFromFlag returns a slice from urls got from the flag.
  77. func UniqueURLsFromFlag(fs *flag.FlagSet, urlsFlagName string) []url.URL {
  78. return (*fs.Lookup(urlsFlagName).Value.(*UniqueURLs)).uss
  79. }
  80. // UniqueURLsMapFromFlag returns a map from url strings got from the flag.
  81. func UniqueURLsMapFromFlag(fs *flag.FlagSet, urlsFlagName string) map[string]struct{} {
  82. return (*fs.Lookup(urlsFlagName).Value.(*UniqueURLs)).Values
  83. }