Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame] | 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 | // MapStringString can be set from the command line with the format `--flag "string=string"`. |
| 26 | // Multiple flag invocations are supported. For example: `--flag "a=foo" --flag "b=bar"`. If this is desired |
| 27 | // to be the only type invocation `NoSplit` should be set to true. |
| 28 | // Multiple comma-separated key-value pairs in a single invocation are supported if `NoSplit` |
| 29 | // is set to false. For example: `--flag "a=foo,b=bar"`. |
| 30 | type MapStringString struct { |
| 31 | Map *map[string]string |
| 32 | initialized bool |
| 33 | NoSplit bool |
| 34 | } |
| 35 | |
| 36 | // NewMapStringString takes a pointer to a map[string]string and returns the |
| 37 | // MapStringString flag parsing shim for that map |
| 38 | func NewMapStringString(m *map[string]string) *MapStringString { |
| 39 | return &MapStringString{Map: m} |
| 40 | } |
| 41 | |
| 42 | // NewMapStringString takes a pointer to a map[string]string and sets `NoSplit` |
| 43 | // value to `true` and returns the MapStringString flag parsing shim for that map |
| 44 | func NewMapStringStringNoSplit(m *map[string]string) *MapStringString { |
| 45 | return &MapStringString{ |
| 46 | Map: m, |
| 47 | NoSplit: true, |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // String implements github.com/spf13/pflag.Value |
| 52 | func (m *MapStringString) String() string { |
| 53 | if m == nil || m.Map == nil { |
| 54 | return "" |
| 55 | } |
| 56 | pairs := []string{} |
| 57 | for k, v := range *m.Map { |
| 58 | pairs = append(pairs, fmt.Sprintf("%s=%s", k, v)) |
| 59 | } |
| 60 | sort.Strings(pairs) |
| 61 | return strings.Join(pairs, ",") |
| 62 | } |
| 63 | |
| 64 | // Set implements github.com/spf13/pflag.Value |
| 65 | func (m *MapStringString) Set(value string) error { |
| 66 | if m.Map == nil { |
| 67 | return fmt.Errorf("no target (nil pointer to map[string]string)") |
| 68 | } |
| 69 | if !m.initialized || *m.Map == nil { |
| 70 | // clear default values, or allocate if no existing map |
| 71 | *m.Map = make(map[string]string) |
| 72 | m.initialized = true |
| 73 | } |
| 74 | |
| 75 | // account for comma-separated key-value pairs in a single invocation |
| 76 | if !m.NoSplit { |
| 77 | for _, s := range strings.Split(value, ",") { |
| 78 | if len(s) == 0 { |
| 79 | continue |
| 80 | } |
| 81 | arr := strings.SplitN(s, "=", 2) |
| 82 | if len(arr) != 2 { |
| 83 | return fmt.Errorf("malformed pair, expect string=string") |
| 84 | } |
| 85 | k := strings.TrimSpace(arr[0]) |
| 86 | v := strings.TrimSpace(arr[1]) |
| 87 | (*m.Map)[k] = v |
| 88 | } |
| 89 | return nil |
| 90 | } |
| 91 | |
| 92 | // account for only one key-value pair in a single invocation |
| 93 | arr := strings.SplitN(value, "=", 2) |
| 94 | if len(arr) != 2 { |
| 95 | return fmt.Errorf("malformed pair, expect string=string") |
| 96 | } |
| 97 | k := strings.TrimSpace(arr[0]) |
| 98 | v := strings.TrimSpace(arr[1]) |
| 99 | (*m.Map)[k] = v |
| 100 | return nil |
| 101 | |
| 102 | } |
| 103 | |
| 104 | // Type implements github.com/spf13/pflag.Value |
| 105 | func (*MapStringString) Type() string { |
| 106 | return "mapStringString" |
| 107 | } |
| 108 | |
| 109 | // Empty implements OmitEmpty |
| 110 | func (m *MapStringString) Empty() bool { |
| 111 | return len(*m.Map) == 0 |
| 112 | } |