-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
extra_options.go
164 lines (138 loc) · 4.67 KB
/
extra_options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
Copyright 2019 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package config
import (
"fmt"
"strings"
"k8s.io/klog/v2"
)
// ExtraOption is an extra option
type ExtraOption struct {
Component string
Key string
Value string
}
func (e *ExtraOption) String() string {
return fmt.Sprintf("%s.%s=%s", e.Component, e.Key, e.Value)
}
// ExtraOptionSlice is a slice of ExtraOption
type ExtraOptionSlice []ExtraOption
// ComponentExtraOptionMap maps components to their extra opts, which is a map of keys to values
type ComponentExtraOptionMap map[string]map[string]string
// Exists returns true if component.key (parsed from value) is already in ExtraOptionSlice
func (es *ExtraOptionSlice) Exists(value string) bool {
// The component is the value before the first dot.
componentSplit := strings.SplitN(value, ".", 2)
if len(componentSplit) != 2 {
klog.Errorf("invalid value: must contain at least one period: %q", value)
return false
}
keySplit := strings.SplitN(componentSplit[1], "=", 2)
if len(keySplit) != 2 {
klog.Errorf("invalid value: must contain one equal sign: %q", value)
return false
}
for _, opt := range *es {
if opt.Component == componentSplit[0] && opt.Key == keySplit[0] {
return true
}
}
return false
}
// Set parses the string value into a slice
func (es *ExtraOptionSlice) Set(value string) error {
// Check we don't end with suffix quotation.
prefixExists := strings.HasPrefix(value, "”") || strings.HasPrefix(value, "“")
suffixExists := strings.HasSuffix(value, "”") || strings.HasSuffix(value, "“")
if !prefixExists && suffixExists {
return fmt.Errorf("invalid value: extra-config cannot contain end quotation: %q", value)
}
// The component is the value before the first dot.
componentSplit := strings.SplitN(value, ".", 2)
if len(componentSplit) < 2 {
return fmt.Errorf("invalid value: must contain at least one period: %q", value)
}
remainder := strings.Join(componentSplit[1:], "")
keySplit := strings.SplitN(remainder, "=", 2)
if len(keySplit) != 2 {
return fmt.Errorf("invalid value: must contain one equal sign: %q", value)
}
e := ExtraOption{
Component: componentSplit[0],
Key: keySplit[0],
Value: keySplit[1],
}
*es = append(*es, e)
return nil
}
// String converts the slice to a string value
func (es *ExtraOptionSlice) String() string {
s := []string{}
for _, e := range *es {
s = append(s, e.String())
}
return strings.Join(s, " ")
}
// Get finds and returns the value of an argument with the specified key and component (optional) or an empty string
// if not found. If component contains more than one value, the value for the first component found is returned. If
// component is not specified, all of the components are used.
func (es *ExtraOptionSlice) Get(key string, component ...string) string {
for _, opt := range *es {
if component == nil || ContainsParam(component, opt.Component) {
if opt.Key == key {
return opt.Value
}
}
}
return ""
}
// AsMap converts the slice to a map of components to a map of keys and values.
func (es *ExtraOptionSlice) AsMap() ComponentExtraOptionMap {
ret := ComponentExtraOptionMap{}
for _, opt := range *es {
if _, ok := ret[opt.Component]; !ok {
ret[opt.Component] = map[string]string{opt.Key: opt.Value}
} else {
ret[opt.Component][opt.Key] = opt.Value
}
}
return ret
}
// Type returns the type
func (es *ExtraOptionSlice) Type() string {
return "ExtraOption"
}
// Get returns the extra option map of keys to values for the specified component
func (cm ComponentExtraOptionMap) Get(component string) map[string]string {
return cm[component]
}
// ContainsParam checks if a given slice of strings contains the provided string.
// If a modifier func is provided, it is called with the slice item before the comparison.
func ContainsParam(slice []string, s string) bool {
for _, item := range slice {
if item == s {
return true
}
}
return false
}
// NewUnversionedOption returns a VersionedExtraOption that applies to all versions.
func NewUnversionedOption(component, k, v string) VersionedExtraOption {
return VersionedExtraOption{
Option: ExtraOption{
Component: component,
Key: k,
Value: v,
},
}
}