-
Notifications
You must be signed in to change notification settings - Fork 6
/
helpers.go
248 lines (219 loc) · 7.68 KB
/
helpers.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
/*
Copyright (C) 2018 Synopsys, Inc.
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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 util
import (
"fmt"
"strconv"
"strings"
"time"
)
// MergeEnvMaps will merge the source and destination environs. If the same value exist in both, source environ will given more preference
func MergeEnvMaps(source, destination map[string]string) map[string]string {
// if the source key present in the destination map, it will overrides the destination value
// if the source value is empty, then delete it from the destination
for key, value := range source {
if len(value) == 0 {
delete(destination, key)
} else {
destination[key] = value
}
}
return destination
}
// MergeEnvSlices will merge the source and destination environs. If the same value exist in both, source environ will given more preference
func MergeEnvSlices(source, destination []string) []string {
// create a destination map
destinationMap := make(map[string]string)
for _, value := range destination {
values := strings.SplitN(value, ":", 2)
if len(values) == 2 {
mapKey := strings.TrimSpace(values[0])
mapValue := strings.TrimSpace(values[1])
if len(mapKey) > 0 && len(mapValue) > 0 {
destinationMap[mapKey] = mapValue
}
}
}
// if the source key present in the destination map, it will overrides the destination value
// if the source value is empty, then delete it from the destination
for _, value := range source {
values := strings.SplitN(value, ":", 2)
if len(values) == 2 {
mapKey := strings.TrimSpace(values[0])
mapValue := strings.TrimSpace(values[1])
if len(mapValue) == 0 {
delete(destinationMap, mapKey)
} else {
destinationMap[mapKey] = mapValue
}
}
}
// convert destination map to string array
mergedValues := []string{}
for key, value := range destinationMap {
mergedValues = append(mergedValues, fmt.Sprintf("%s:%s", key, value))
}
return mergedValues
}
// UniqueStringSlice returns a unique subset of the string slice provided.
func UniqueStringSlice(input []string) []string {
output := []string{}
m := make(map[string]bool)
for _, val := range input {
if _, ok := m[val]; !ok {
m[val] = true
output = append(output, val)
}
}
return output
}
// GetResourceName returns the name of the resource
func GetResourceName(name string, appName string, defaultName string) string {
if len(appName) == 0 {
return fmt.Sprintf("%s-%s", name, defaultName)
}
if len(defaultName) == 0 {
return fmt.Sprintf("%s-%s", name, appName)
}
return fmt.Sprintf("%s-%s-%s", name, appName, defaultName)
}
// RemoveFromStringSlice will remove the string from the slice and it will maintain the order
func RemoveFromStringSlice(slice []string, str string) []string {
for index, value := range slice {
if value == str {
slice = append(slice[:index], slice[index+1:]...)
}
}
return slice
}
// IsExistInStringSlice will check for the input string in the given slice
func IsExistInStringSlice(slice []string, str string) bool {
for _, value := range slice {
if value == str {
return true
}
}
return false
}
// IsExposeServiceValid validates the expose service type
func IsExposeServiceValid(serviceType string) bool {
switch strings.ToUpper(serviceType) {
case NONE, NODEPORT, LOADBALANCER, OPENSHIFT:
return true
}
return false
}
// IsBlackDuckVersionSupportMultipleInstance returns whether it supports multiple instance in a single namespace
func IsBlackDuckVersionSupportMultipleInstance(version string) (bool, error) {
return isYearAndMonthGreaterThanOrEqualTo(version, 2019, time.August)
}
// isYearAndMonthGreaterThanOrEqualTo returns whether the given version is greater than or equal to the given year and month
func isYearAndMonthGreaterThanOrEqualTo(version string, year int, month time.Month) (bool, error) {
versionArr := strings.Split(version, ".")
if len(versionArr) >= 3 {
t, err := time.Parse("2006.1", fmt.Sprintf("%s.%s", versionArr[0], versionArr[1]))
if err != nil {
return false, err
}
if t.Year() >= year && t.Month() >= month {
return true, nil
}
}
return false, nil
}
// IsVersionEqualTo returns whether the given version is equal to the given year, month and dot release
func IsVersionEqualTo(version string, year int, month time.Month, dotRelease int) (bool, error) {
versionArr := strings.Split(version, ".")
if len(versionArr) >= 3 {
t, err := time.Parse("2006.1", fmt.Sprintf("%s.%s", versionArr[0], versionArr[1]))
if err != nil {
return false, err
}
minorDotVersion, err := strconv.Atoi(versionArr[2])
if err != nil {
return false, err
}
if t.Year() == year && t.Month() == month && minorDotVersion == dotRelease {
return true, nil
}
}
return false, nil
}
// IsVersionGreaterThanOrEqualTo returns whether the given version is greater than or equal to the given year, month and dot release
func IsVersionGreaterThanOrEqualTo(version string, year int, month time.Month, dotRelease int) (bool, error) {
versionArr := strings.Split(version, ".")
if len(versionArr) >= 3 {
t, err := time.Parse("2006.1", fmt.Sprintf("%s.%s", versionArr[0], versionArr[1]))
if err != nil {
return false, err
}
minorDotVersion, err := strconv.Atoi(versionArr[2])
if err != nil {
return false, err
}
if (t.Year() > year) ||
(t.Year() == year && t.Month() > month) ||
(t.Year() == year && t.Month() == month && minorDotVersion >= dotRelease) {
return true, nil
}
}
return false, nil
}
// IsNotDefaultVersionGreaterThanOrEqualTo returns whether the given version is greater than or equal to the given inputs
func IsNotDefaultVersionGreaterThanOrEqualTo(version string, majorRelease int, minorRelease int, dotRelease int) (bool, error) {
versionArr := strings.Split(version, ".")
if len(versionArr) >= 3 {
majorReleaseVersion, err := strconv.Atoi(versionArr[0])
if err != nil {
return false, err
}
minorReleaseVersion, err := strconv.Atoi(versionArr[1])
if err != nil {
return false, err
}
dotReleaseVersion, err := strconv.Atoi(versionArr[2])
if err != nil {
return false, err
}
if (majorReleaseVersion > majorRelease) ||
(majorReleaseVersion == majorRelease && minorReleaseVersion > minorRelease) ||
(majorReleaseVersion == majorRelease && minorReleaseVersion == minorRelease && dotReleaseVersion >= dotRelease) {
return true, nil
}
}
return false, nil
}
// StringArrayToMapSplitBySeparator converts the string array to map based on separator for each string in the array
func StringArrayToMapSplitBySeparator(strs []string, separator string) map[string]string {
maps := make(map[string]string, 0)
for _, str := range strs {
strArr := strings.SplitN(str, separator, 2)
if len(strArr) == 2 {
maps[strArr[0]] = strArr[1]
}
}
return maps
}
// MapToStringArrayJoinBySeparator converts the map to string array and each key and value of the map will be joined by separator
func MapToStringArrayJoinBySeparator(maps map[string]string, separator string) []string {
strs := make([]string, 0)
for key, value := range maps {
strs = append(strs, fmt.Sprintf("%s%s%s", key, separator, value))
}
return strs
}