urls.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright 2015 CoreOS, Inc.
  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. "log"
  17. "strings"
  18. "github.com/coreos/etcd/pkg/types"
  19. )
  20. type URLsValue types.URLs
  21. // Set parses a command line set of URLs formatted like:
  22. // http://127.0.0.1:7001,http://10.1.1.2:80
  23. func (us *URLsValue) Set(s string) error {
  24. strs := strings.Split(s, ",")
  25. nus, err := types.NewURLs(strs)
  26. if err != nil {
  27. return err
  28. }
  29. *us = URLsValue(nus)
  30. return nil
  31. }
  32. func (us *URLsValue) String() string {
  33. all := make([]string, len(*us))
  34. for i, u := range *us {
  35. all[i] = u.String()
  36. }
  37. return strings.Join(all, ",")
  38. }
  39. func NewURLsValue(init string) *URLsValue {
  40. v := &URLsValue{}
  41. if err := v.Set(init); err != nil {
  42. log.Panicf("new URLsValue should never fail: %v", err)
  43. }
  44. return v
  45. }