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