/
common.go
283 lines (246 loc) · 6.17 KB
/
common.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package mconf
import (
"fmt"
"strconv"
"strings"
)
const (
// YamlFormat yaml格式
YamlFormat = "yaml"
// JSONFormat json格式
JSONFormat = "json"
// TomlFormat toml格式
TomlFormat = "toml"
// NotFound 记录yaml未找到的key的统一值
NotFound = "__not_found__"
)
// Bytes2Str bytes转string
func Bytes2Str(val []byte) string {
str := string(val)
str = strings.Replace(str, "\n", "", -1)
return strings.Trim(str, " ")
}
// Bytes2Int bytes转int
func Bytes2Int(val []byte) int {
str := Bytes2Str(val)
n, _ := strconv.Atoi(str)
return n
}
// Bytes2Map bytes转map
func Bytes2Map(val []byte) map[string]string {
str := string(val)
str = strings.Trim(str, " ")
ss := strings.Split(str, "\n")
out := map[string]string{}
for _, s := range ss {
subStrs := strings.Split(s, ":")
if len(subStrs) == 2 {
out[strings.Trim(subStrs[0], " ")] = strings.Trim(subStrs[1], " ")
}
}
return out
}
// Bytes2Slice bytes转slice
func Bytes2Slice(val []byte) []string {
str := string(val)
str = strings.Trim(str, " ")
ss := strings.Split(str, "\n")
out := []string{}
for _, s := range ss {
if s != "" {
out = append(out, s)
}
}
return out
}
// k8s的容器启动参数
// ---------------------------------------------------------------------------------------
// Bytes2SliceForArgs bytes转slice
func Bytes2SliceForArgs(val []byte) []string {
str := Bytes2Str(val)
strs := []string{}
if strings.Contains(str, " -") {
ss := strings.Split(str, " -")
for i, s := range ss {
s = strings.Trim(s, " ")
if i == 0 {
strs = append(strs, s)
continue
}
strs = append(strs, "-"+s)
}
} else {
return []string{str}
}
return strs
}
// ResourcesMap2Str 资源字典转字符串
func ResourcesMap2Str(m map[string]string) string {
_, values := resourcesSizeMap2Slice(m)
return strings.Join(values, ", ")
}
// 把map转为适合规则的slice
func resourcesSizeMap2Slice(kvs map[string]string) ([]string, []string) {
valuesType := []string{}
values := []string{}
if v, ok := kvs["cpu"]; ok {
valuesType = append(valuesType, "string")
values = append(values, fmt.Sprintf("cpu=%s", v))
}
if v, ok := kvs["memory"]; ok {
valuesType = append(valuesType, "string")
values = append(values, fmt.Sprintf("memory=%s", v))
}
return valuesType, values
}
// 参数字典转字符串
func argsMap2Str(m map[string]string, sep string) string { //nolint
str := ""
for k, v := range m {
k = "-" + trimKey(k)
if v == "" {
str = fmt.Sprintf("%s %s ", str, k)
} else {
if sep == "" {
sep = " "
}
str = fmt.Sprintf("%s %s%s%s ", str, k, sep, v)
}
}
return strings.Trim(str, " ")
}
// 把二维args转为一维度slice
func args2sliceAndType(args [][]string) []string { //nolint
var out []string
for _, arg := range args {
out = append(out, strings.Join(arg, " "))
}
return out
}
// 字符串转map,a=1,b=2 --> {a:1,b:2}
func str2Map(str string) map[string]string { //nolint
str = strings.Trim(str, " ")
ss := strings.Split(str, ",")
out := map[string]string{}
for _, s := range ss {
if strings.Contains(s, "=") {
n := strings.Index(s, "=")
key := strings.Trim(s[:n], " ")
value := strings.Trim(s[n+1:], " ")
fmt.Println(key, value)
out[key] = value
} else {
out[strings.Trim(s, " ")] = ""
}
}
return out
}
// ---------------------------------------------------------------------------------------
// PutRecord 已成功替换的参数记录
type PutRecord struct {
Key string `json:"key"`
OldVal string `json:"oldVal"` // 如果存在则替换,如果查找的key不存在,则补充统一值__not_found__,表示是新添加
NewVal string `json:"newVal"`
}
// 添加或替换参数
func addOrReplaceArgs(args [][]string, argsMap map[string]string) []*PutRecord { //nolint
records := []*PutRecord{}
addKVs := []string{}
for i, arg := range args {
for j, s := range arg {
record, newKV := getMatchArgs(s, argsMap)
if newKV == "" {
continue
}
args[i][j] = newKV
records = append(records, record)
}
}
// 判断是否有需要新添加的kv
addRecords := []*PutRecord{}
if len(argsMap) > len(records) {
for k, v := range argsMap {
if !isExistKey(records, k) {
sep := "=" // 默认分割符是=号
if v == "" { // 如果值为空改为空格
sep = ""
}
addKVs = append(addKVs, fmt.Sprintf(`-%s%s%s`, trimKey(k), sep, v))
addRecords = append(addRecords, &PutRecord{
Key: k,
OldVal: NotFound,
NewVal: v,
})
}
}
}
if len(addKVs) > 0 {
args[len(args)-1] = append(args[len(args)-1], addKVs...) // 添加新kv
records = append(records, addRecords...) // 添加新记录
}
return records
}
// 获取匹配的参数,如果匹配,返回替换后的kv值
func getMatchArgs(s string, argsMap map[string]string) (*PutRecord, string) { //nolint
for k, v := range argsMap {
kTmp := trimKey(k)
sep := "" // 原参数k和v之间的分割符,只支持=号和空格两种
if strings.Contains(s, kTmp) { // 判读是否匹配key(去掉前缀-号)
index := strings.Index(s, kTmp)
sepIndex := 0
isSpace := false
// 搜索定位分割符位置和值
for n, char := range s[index+len(kTmp):] {
if char == '=' {
sep = "="
sepIndex = index + len(kTmp) + n
break
}
if char == ' ' {
isSpace = true
continue
}
if (char >= 'A' && char <= 'z') || (char >= '0' && char <= '9') || char == '_' {
if isSpace {
sep = " "
sepIndex = index + len(kTmp) + n - 1
break
}
}
}
if sep == "" {
continue
}
// 验证分割后的key是否完全一致
if kTmp != trimKey(s[0:sepIndex]) {
continue
}
newKV := s[0:sepIndex] + sep + v // 保持原样
replaceRecord := &PutRecord{
Key: k,
OldVal: strings.Replace(s, s[0:sepIndex]+sep, "", -1),
NewVal: v,
}
return replaceRecord, newKV
}
}
return nil, ""
}
func trimKey(k string) string { //nolint
kTmp := strings.Trim(k, " ")
for len(kTmp) > 0 {
if kTmp[0] != '-' {
break
}
kTmp = strings.TrimLeft(kTmp, "-")
}
return kTmp
}
func isExistKey(records []*PutRecord, k string) bool { //nolint
for _, record := range records {
if record.Key == k {
return true
}
}
return false
}