urls.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2015 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. "strings"
  19. "go.etcd.io/etcd/pkg/types"
  20. )
  21. // URLsValue wraps "types.URLs".
  22. type URLsValue types.URLs
  23. // Set parses a command line set of URLs formatted like:
  24. // http://127.0.0.1:2380,http://10.1.1.2:80
  25. // Implements "flag.Value" interface.
  26. func (us *URLsValue) Set(s string) error {
  27. ss, err := types.NewURLs(strings.Split(s, ","))
  28. if err != nil {
  29. return err
  30. }
  31. *us = URLsValue(ss)
  32. return nil
  33. }
  34. // String implements "flag.Value" interface.
  35. func (us *URLsValue) String() string {
  36. all := make([]string, len(*us))
  37. for i, u := range *us {
  38. all[i] = u.String()
  39. }
  40. return strings.Join(all, ",")
  41. }
  42. // NewURLsValue implements "url.URL" slice as flag.Value interface.
  43. // Given value is to be separated by comma.
  44. func NewURLsValue(s string) *URLsValue {
  45. if s == "" {
  46. return &URLsValue{}
  47. }
  48. v := &URLsValue{}
  49. if err := v.Set(s); err != nil {
  50. plog.Panicf("new URLsValue should never fail: %v", err)
  51. }
  52. return v
  53. }
  54. // URLsFromFlag returns a slices from url got from the flag.
  55. func URLsFromFlag(fs *flag.FlagSet, urlsFlagName string) []url.URL {
  56. return []url.URL(*fs.Lookup(urlsFlagName).Value.(*URLsValue))
  57. }