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 | "errors" |
| 19 | "fmt" |
| 20 | "sort" |
| 21 | "strings" |
| 22 | ) |
| 23 | |
| 24 | // SelectiveStringValue implements the flag.Value interface. |
| 25 | type SelectiveStringValue struct { |
| 26 | v string |
| 27 | valids map[string]struct{} |
| 28 | } |
| 29 | |
| 30 | // Set verifies the argument to be a valid member of the allowed values |
| 31 | // before setting the underlying flag value. |
| 32 | func (ss *SelectiveStringValue) Set(s string) error { |
| 33 | if _, ok := ss.valids[s]; ok { |
| 34 | ss.v = s |
| 35 | return nil |
| 36 | } |
| 37 | return errors.New("invalid value") |
| 38 | } |
| 39 | |
| 40 | // String returns the set value (if any) of the SelectiveStringValue |
| 41 | func (ss *SelectiveStringValue) String() string { |
| 42 | return ss.v |
| 43 | } |
| 44 | |
| 45 | // Valids returns the list of valid strings. |
| 46 | func (ss *SelectiveStringValue) Valids() []string { |
| 47 | s := make([]string, 0, len(ss.valids)) |
| 48 | for k := range ss.valids { |
| 49 | s = append(s, k) |
| 50 | } |
| 51 | sort.Strings(s) |
| 52 | return s |
| 53 | } |
| 54 | |
| 55 | // NewSelectiveStringValue creates a new string flag |
| 56 | // for which any one of the given strings is a valid value, |
| 57 | // and any other value is an error. |
| 58 | // |
| 59 | // valids[0] will be default value. Caller must be sure |
| 60 | // len(valids) != 0 or it will panic. |
| 61 | func NewSelectiveStringValue(valids ...string) *SelectiveStringValue { |
| 62 | vm := make(map[string]struct{}) |
| 63 | for _, v := range valids { |
| 64 | vm[v] = struct{}{} |
| 65 | } |
| 66 | return &SelectiveStringValue{valids: vm, v: valids[0]} |
| 67 | } |
| 68 | |
| 69 | // SelectiveStringsValue implements the flag.Value interface. |
| 70 | type SelectiveStringsValue struct { |
| 71 | vs []string |
| 72 | valids map[string]struct{} |
| 73 | } |
| 74 | |
| 75 | // Set verifies the argument to be a valid member of the allowed values |
| 76 | // before setting the underlying flag value. |
| 77 | func (ss *SelectiveStringsValue) Set(s string) error { |
| 78 | vs := strings.Split(s, ",") |
| 79 | for i := range vs { |
| 80 | if _, ok := ss.valids[vs[i]]; ok { |
| 81 | ss.vs = append(ss.vs, vs[i]) |
| 82 | } else { |
| 83 | return fmt.Errorf("invalid value %q", vs[i]) |
| 84 | } |
| 85 | } |
| 86 | sort.Strings(ss.vs) |
| 87 | return nil |
| 88 | } |
| 89 | |
| 90 | // String returns the set value (if any) of the SelectiveStringsValue. |
| 91 | func (ss *SelectiveStringsValue) String() string { |
| 92 | return strings.Join(ss.vs, ",") |
| 93 | } |
| 94 | |
| 95 | // Valids returns the list of valid strings. |
| 96 | func (ss *SelectiveStringsValue) Valids() []string { |
| 97 | s := make([]string, 0, len(ss.valids)) |
| 98 | for k := range ss.valids { |
| 99 | s = append(s, k) |
| 100 | } |
| 101 | sort.Strings(s) |
| 102 | return s |
| 103 | } |
| 104 | |
| 105 | // NewSelectiveStringsValue creates a new string slice flag |
| 106 | // for which any one of the given strings is a valid value, |
| 107 | // and any other value is an error. |
| 108 | func NewSelectiveStringsValue(valids ...string) *SelectiveStringsValue { |
| 109 | vm := make(map[string]struct{}) |
| 110 | for _, v := range valids { |
| 111 | vm[v] = struct{}{} |
| 112 | } |
| 113 | return &SelectiveStringsValue{valids: vm, vs: []string{}} |
| 114 | } |