khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 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 | |
| 15 | package flags |
| 16 | |
| 17 | import ( |
| 18 | "flag" |
| 19 | "net/url" |
| 20 | "sort" |
| 21 | "strings" |
| 22 | |
| 23 | "go.etcd.io/etcd/pkg/types" |
| 24 | ) |
| 25 | |
| 26 | // UniqueURLs contains unique URLs |
| 27 | // with non-URL exceptions. |
| 28 | type UniqueURLs struct { |
| 29 | Values map[string]struct{} |
| 30 | uss []url.URL |
| 31 | Allowed map[string]struct{} |
| 32 | } |
| 33 | |
| 34 | // Set parses a command line set of URLs formatted like: |
| 35 | // http://127.0.0.1:2380,http://10.1.1.2:80 |
| 36 | // Implements "flag.Value" interface. |
| 37 | func (us *UniqueURLs) Set(s string) error { |
| 38 | if _, ok := us.Values[s]; ok { |
| 39 | return nil |
| 40 | } |
| 41 | if _, ok := us.Allowed[s]; ok { |
| 42 | us.Values[s] = struct{}{} |
| 43 | return nil |
| 44 | } |
| 45 | ss, err := types.NewURLs(strings.Split(s, ",")) |
| 46 | if err != nil { |
| 47 | return err |
| 48 | } |
| 49 | us.Values = make(map[string]struct{}) |
| 50 | us.uss = make([]url.URL, 0) |
| 51 | for _, v := range ss { |
| 52 | us.Values[v.String()] = struct{}{} |
| 53 | us.uss = append(us.uss, v) |
| 54 | } |
| 55 | return nil |
| 56 | } |
| 57 | |
| 58 | // String implements "flag.Value" interface. |
| 59 | func (us *UniqueURLs) String() string { |
| 60 | all := make([]string, 0, len(us.Values)) |
| 61 | for u := range us.Values { |
| 62 | all = append(all, u) |
| 63 | } |
| 64 | sort.Strings(all) |
| 65 | return strings.Join(all, ",") |
| 66 | } |
| 67 | |
| 68 | // NewUniqueURLsWithExceptions implements "url.URL" slice as flag.Value interface. |
| 69 | // Given value is to be separated by comma. |
| 70 | func NewUniqueURLsWithExceptions(s string, exceptions ...string) *UniqueURLs { |
| 71 | us := &UniqueURLs{Values: make(map[string]struct{}), Allowed: make(map[string]struct{})} |
| 72 | for _, v := range exceptions { |
| 73 | us.Allowed[v] = struct{}{} |
| 74 | } |
| 75 | if s == "" { |
| 76 | return us |
| 77 | } |
| 78 | if err := us.Set(s); err != nil { |
| 79 | plog.Panicf("new UniqueURLs should never fail: %v", err) |
| 80 | } |
| 81 | return us |
| 82 | } |
| 83 | |
| 84 | // UniqueURLsFromFlag returns a slice from urls got from the flag. |
| 85 | func UniqueURLsFromFlag(fs *flag.FlagSet, urlsFlagName string) []url.URL { |
| 86 | return (*fs.Lookup(urlsFlagName).Value.(*UniqueURLs)).uss |
| 87 | } |
| 88 | |
| 89 | // UniqueURLsMapFromFlag returns a map from url strings got from the flag. |
| 90 | func UniqueURLsMapFromFlag(fs *flag.FlagSet, urlsFlagName string) map[string]struct{} { |
| 91 | return (*fs.Lookup(urlsFlagName).Value.(*UniqueURLs)).Values |
| 92 | } |