urls.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package flags
  14. import (
  15. "log"
  16. "strings"
  17. "github.com/coreos/etcd/pkg/types"
  18. )
  19. type URLsValue types.URLs
  20. // Set parses a command line set of URLs formatted like:
  21. // http://127.0.0.1:7001,http://10.1.1.2:80
  22. func (us *URLsValue) Set(s string) error {
  23. strs := strings.Split(s, ",")
  24. nus, err := types.NewURLs(strs)
  25. if err != nil {
  26. return err
  27. }
  28. *us = URLsValue(nus)
  29. return nil
  30. }
  31. func (us *URLsValue) String() string {
  32. all := make([]string, len(*us))
  33. for i, u := range *us {
  34. all[i] = u.String()
  35. }
  36. return strings.Join(all, ",")
  37. }
  38. func NewURLsValue(init string) *URLsValue {
  39. v := &URLsValue{}
  40. if err := v.Set(init); err != nil {
  41. log.Panicf("new URLsValue should never fail: %v", err)
  42. }
  43. return v
  44. }