1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package flag 18 19 import ( 20 "fmt" 21 "sort" 22 "strings" 23 ) 24 25 // ColonSeparatedMultimapStringString supports setting a map[string][]string from an encoding 26 // that separates keys from values with ':' and separates key-value pairs with ','. 27 // A key can be repeated multiple times, in which case the values are appended to a 28 // slice of strings associated with that key. Items in the list associated with a given 29 // key will appear in the order provided. 30 // For example: `a:hello,b:again,c:world,b:beautiful` results in `{"a": ["hello"], "b": ["again", "beautiful"], "c": ["world"]}` 31 // The first call to Set will clear the map before adding entries; subsequent calls will simply append to the map. 32 // This makes it possible to override default values with a command-line option rather than appending to defaults, 33 // while still allowing the distribution of key-value pairs across multiple flag invocations. 34 // For example: `--flag "a:hello" --flag "b:again" --flag "b:beautiful" --flag "c:world"` results in `{"a": ["hello"], "b": ["again", "beautiful"], "c": ["world"]}` 35 type ColonSeparatedMultimapStringString struct { 36 Multimap *map[string][]string 37 initialized bool // set to true after the first Set call 38 } 39 40 // NewColonSeparatedMultimapStringString takes a pointer to a map[string][]string and returns the 41 // ColonSeparatedMultimapStringString flag parsing shim for that map. 42 func NewColonSeparatedMultimapStringString(m *map[string][]string) *ColonSeparatedMultimapStringString { 43 return &ColonSeparatedMultimapStringString{Multimap: m} 44 } 45 46 // Set implements github.com/spf13/pflag.Value 47 func (m *ColonSeparatedMultimapStringString) Set(value string) error { 48 if m.Multimap == nil { 49 return fmt.Errorf("no target (nil pointer to map[string][]string)") 50 } 51 if !m.initialized || *m.Multimap == nil { 52 // clear default values, or allocate if no existing map 53 *m.Multimap = make(map[string][]string) 54 m.initialized = true 55 } 56 for _, pair := range strings.Split(value, ",") { 57 if len(pair) == 0 { 58 continue 59 } 60 kv := strings.SplitN(pair, ":", 2) 61 if len(kv) != 2 { 62 return fmt.Errorf("malformed pair, expect string:string") 63 } 64 k := strings.TrimSpace(kv[0]) 65 v := strings.TrimSpace(kv[1]) 66 (*m.Multimap)[k] = append((*m.Multimap)[k], v) 67 } 68 return nil 69 } 70 71 // String implements github.com/spf13/pflag.Value 72 func (m *ColonSeparatedMultimapStringString) String() string { 73 type kv struct { 74 k string 75 v string 76 } 77 kvs := make([]kv, 0, len(*m.Multimap)) 78 for k, vs := range *m.Multimap { 79 for i := range vs { 80 kvs = append(kvs, kv{k: k, v: vs[i]}) 81 } 82 } 83 // stable sort by keys, order of values should be preserved 84 sort.SliceStable(kvs, func(i, j int) bool { 85 return kvs[i].k < kvs[j].k 86 }) 87 pairs := make([]string, 0, len(kvs)) 88 for i := range kvs { 89 pairs = append(pairs, fmt.Sprintf("%s:%s", kvs[i].k, kvs[i].v)) 90 } 91 return strings.Join(pairs, ",") 92 } 93 94 // Type implements github.com/spf13/pflag.Value 95 func (m *ColonSeparatedMultimapStringString) Type() string { 96 return "colonSeparatedMultimapStringString" 97 } 98 99 // Empty implements OmitEmpty 100 func (m *ColonSeparatedMultimapStringString) Empty() bool { 101 return len(*m.Multimap) == 0 102 } 103