-
Notifications
You must be signed in to change notification settings - Fork 7
/
helper.go
626 lines (559 loc) · 15.8 KB
/
helper.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
package main
import (
"encoding/json"
"errors"
"fmt"
"os"
"regexp"
"strings"
"unicode"
"github.com/AlecAivazis/survey/v2"
"github.com/iancoleman/strcase"
log "github.com/sirupsen/logrus"
"golang.org/x/exp/slices"
"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
"gopkg.in/yaml.v3"
)
const (
Prod = "Prod"
QA = "QA"
Dev = "Dev"
Prod3 = "Prod3"
SelfManaged = "SelfManaged"
DevSpace = "DevSpace"
)
const (
MigratorService = "Migrator"
NextGenService = "NextGen"
TemplateService = "Template"
PipelineService = "Pipeline"
)
var host = os.Getenv("HOST_NAME")
var urlMap = map[string]map[string]string{
Prod: {
PipelineService: "https://app.harness.io/gateway/pipeline",
TemplateService: "https://app.harness.io/gateway/template",
MigratorService: "https://app.harness.io/gateway/ng-migration/api/ng-migration",
NextGenService: "https://app.harness.io/gateway/ng",
},
QA: {
PipelineService: "https://qa.harness.io/gateway/pipeline",
TemplateService: "https://qa.harness.io/gateway/template",
MigratorService: "https://qa.harness.io/gateway/ng-migration/api/ng-migration",
NextGenService: "https://qa.harness.io/gateway/ng",
},
Dev: {
PipelineService: "https://localhost:8181/pipeline",
TemplateService: "https://localhost:8181/template",
MigratorService: "https://localhost:9080/api/ng-migration",
NextGenService: "https://localhost:8181/ng",
},
Prod3: {
PipelineService: "https://app3.harness.io/gateway/pipeline",
TemplateService: "https://app3.harness.io/gateway/template",
MigratorService: "https://app3.harness.io/gateway/ng-migration/api/ng-migration",
NextGenService: "https://app3.harness.io/gateway/ng",
},
DevSpace: {
PipelineService: "https://" + host + ".pr2.harness.io/gateway/pipeline",
TemplateService: "https://" + host + ".pr2.harness.io/gateway/template",
MigratorService: "https://" + host + ".pr2.harness.io/gateway/ng-migration/api/ng-migration",
NextGenService: "https://" + host + ".pr2.harness.io/gateway/ng",
},
}
func TextInput(question string) string {
var text = ""
prompt := &survey.Input{
Message: question,
}
err := survey.AskOne(prompt, &text, survey.WithValidator(survey.Required))
if err != nil {
log.Error(err.Error())
os.Exit(0)
}
return text
}
func SelectInput(question string, options []string, defaultValue interface{}) string {
var text = ""
prompt := &survey.Select{
Message: question,
Options: options,
Default: defaultValue,
}
err := survey.AskOne(prompt, &text, survey.WithValidator(survey.Required))
if err != nil {
log.Error(err.Error())
os.Exit(0)
}
return text
}
func ConfirmInput(question string) bool {
confirm := false
prompt := &survey.Confirm{
Message: question,
}
_ = survey.AskOne(prompt, &confirm)
return confirm
}
func GetUrlWithQueryParams(environment string, service string, endpoint string, queryParams map[string]string) string {
params := ""
for k, v := range queryParams {
params = params + k + "=" + v + "&"
}
return fmt.Sprintf("%s/%s?%s", GetBaseUrl(environment, service), endpoint, params)
}
func GetUrl(environment string, service string, path string, accountId string) string {
return fmt.Sprintf("%s/%s?accountIdentifier=%s", GetBaseUrl(environment, service), path, accountId)
}
func getOrDefault(value string, defaultValue string) string {
if len(value) == 0 {
return defaultValue
}
return value
}
func ContainsAny[E comparable](source []E, values []E) bool {
for i := range values {
v := values[i]
if slices.Contains(source, v) {
return true
}
}
return false
}
func MkDir(path string) error {
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
err := os.Mkdir(path, os.ModePerm)
if err != nil {
return err
}
}
return nil
}
func EndsWith(str string, suffixes []string) bool {
for _, suffix := range suffixes {
if strings.HasSuffix(str, suffix) {
return true
}
}
return false
}
func Set(str []string) []string {
var result []string
dict := make(map[string]bool)
for _, val := range str {
dict[val] = true
}
for k := range dict {
result = append(result, k)
}
return result
}
func WriteToFile(absFilePath string, content []byte) error {
f, err := os.Create(absFilePath)
if err != nil {
return err
}
defer f.Close()
_, err = f.Write(content)
return err
}
func ReadFile(absFilePath string) (string, error) {
d, err := os.ReadFile(absFilePath)
if err != nil {
return "", err
}
return string(d), err
}
func ToLowerCase(input string) string {
return strings.ToLower(input)
}
func ToSnakeCase(input string) string {
return strcase.ToSnake(input)
}
func TrimQuotes(input string) string {
if strings.HasPrefix(input, "\"") && strings.HasSuffix(input, "\"") {
return input[1 : len(input)-1]
}
return input
}
func GenerateHarnessUIFormatWithHyphensIdentifier(name string) string {
pattern := regexp.MustCompile(`<\+([^>]+)>`)
matches := pattern.FindAllStringSubmatch(name, -1)
preservedSubstrings := make(map[string]string)
for _, match := range matches {
placeholder := match[1]
preservedKey := fmt.Sprintf("<+%s>", placeholder)
preservedSubstrings[preservedKey] = match[0]
name = strings.ReplaceAll(name, preservedKey, "PLACEHOLDER")
}
name = removeAccents(name)
name = stripStartingChars(name)
name = stripSpecialChars2(name)
name = strings.ReplaceAll(name, " ", "-")
for _, preservedValue := range preservedSubstrings {
name = strings.ReplaceAll(name, "PLACEHOLDER", preservedValue)
}
return name
}
func GenerateHarnessUIFormatIdentifier(name string) string {
pattern := regexp.MustCompile(`<\+([^>]+)>`)
matches := pattern.FindAllStringSubmatch(name, -1)
preservedSubstrings := make(map[string]string)
for _, match := range matches {
placeholder := match[1]
preservedKey := fmt.Sprintf("<+%s>", placeholder)
preservedSubstrings[preservedKey] = match[0]
name = strings.ReplaceAll(name, preservedKey, "PLACEHOLDER")
}
name = removeAccents(name)
name = stripStartingChars(name)
name = stripSpecialChars(name)
name = strings.ReplaceAll(name, " ", "_")
for _, preservedValue := range preservedSubstrings {
name = strings.ReplaceAll(name, "PLACEHOLDER", preservedValue)
}
return name
}
func removeAccents(input string) string {
t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
output, _, err := transform.String(t, input)
if err != nil {
return ""
}
return output
}
func stripStartingChars(s string) string {
return regexp.MustCompile("^[0-9-$]*").ReplaceAllString(s, "")
}
func stripSpecialChars2(s string) string {
return regexp.MustCompile("[^-0-9a-zA-Z_$ ]").ReplaceAllString(s, "")
}
func stripSpecialChars(s string) string {
return regexp.MustCompile("[^0-9a-zA-Z_$ ]").ReplaceAllString(s, "")
}
func ToCamelCase(s string) string {
s = strings.TrimSpace(s)
s = strings.ToLower(s)
if s == "" {
return s
}
pattern := regexp.MustCompile(`<\+([^>]+)>`)
matches := pattern.FindAllStringSubmatch(s, -1)
preservedSubstrings := make(map[string]string)
for _, match := range matches {
placeholder := match[1]
preservedKey := fmt.Sprintf("<+%s>", placeholder)
preservedSubstrings[preservedKey] = match[0]
s = strings.ReplaceAll(s, preservedKey, "PLACEHOLDER")
}
n := strings.Builder{}
n.Grow(len(s))
capNext := false
for i, v := range []byte(s) {
vIsCap := v >= 'A' && v <= 'Z'
vIsLow := v >= 'a' && v <= 'z'
vIsNum := v >= '0' && v <= '9'
if vIsNum && i == 0 {
n.WriteByte('_')
}
if capNext {
if vIsLow {
v += 'A'
v -= 'a'
}
} else if i == 0 {
if vIsCap {
v += 'a'
v -= 'A'
}
}
if vIsCap || vIsLow {
n.WriteByte(v)
capNext = false
} else if vIsNum {
n.WriteByte(v)
capNext = true
} else {
capNext = v == '_' || v == ' ' || v == '-' || v == '.'
}
}
treatedString := n.String()
for _, preservedValue := range preservedSubstrings {
treatedString = strings.ReplaceAll(treatedString, "PLACEHOLDER", preservedValue)
}
return treatedString
}
func Split(str string, sep string) (result []string) {
if len(strings.TrimSpace(str)) == 0 {
return
}
result = strings.Split(str, sep)
for i, s := range result {
result[i] = strings.TrimSpace(s)
}
return
}
func listEntities(entity string) (data []BaseEntityDetail, err error) {
url := GetUrlWithQueryParams(migrationReq.Environment, MigratorService, entity, map[string]string{
AccountIdentifier: migrationReq.Account,
"appId": migrationReq.AppId,
})
resp, err := Get(url, migrationReq.Auth)
if err != nil {
return
}
byteData, err := json.Marshal(resp.Resource)
if err != nil {
return
}
err = json.Unmarshal(byteData, &data)
if err != nil {
return
}
return
}
func GetBaseUrl(environment string, service string) string {
if environment == "Prod1" || environment == "Prod2" {
environment = "Prod"
}
if environment != SelfManaged {
if len(host) == 0 && environment == DevSpace {
log.Fatal("HOST_NAME env variable is not set for DevSpace Environment")
}
url := urlMap[environment][service]
if len(url) == 0 {
log.Fatalf("invalid environment value - %s", environment)
}
return url
}
var url string
switch service {
case PipelineService:
url = migrationReq.BaseUrl + "/pipeline"
case TemplateService:
url = migrationReq.BaseUrl + "/template"
case NextGenService:
url = migrationReq.BaseUrl + "/ng"
case MigratorService:
url = migrationReq.BaseUrl + "/ng-migration/api/ng-migration"
default:
panic("Unknown service! Please contact Harness support")
}
log.Debugf("BaseUrl for SelfManaged - %s", url)
return url
}
func GetEntityIds(entity string, idsString string, namesString string) ([]string, error) {
ids := Split(idsString, ",")
if len(ids) > 0 {
return ids, nil
}
names := Split(namesString, ",")
if len(names) == 0 {
return nil, nil
}
nameToIdMap, err := GetEntityNameIdMap(entity)
if err != nil {
return nil, err
}
var result []string
for _, item := range names {
itemId, ok := nameToIdMap[item]
if !ok {
continue
}
result = append(result, itemId)
}
return result, nil
}
func GetEntityNameIdMap(entity string) (map[string]string, error) {
items, err := listEntities(entity)
if err != nil {
return nil, err
}
var nameToIdMap = make(map[string]string)
for _, item := range items {
nameToIdMap[item.Name] = item.Id
}
return nameToIdMap, err
}
func MigrateEntities(promptConfirm bool, scopes []string, pluralValue string, entityType EntityType) (err error) {
promptConfirm = PromptOrgAndProject(scopes) || promptConfirm
logMigrationDetails()
if promptConfirm {
confirm := ConfirmInput("Do you want to proceed?")
if !confirm {
log.Fatal("Aborting...")
}
}
importType := ImportType("ALL")
var ids []string
if !migrationReq.All {
importType = "SPECIFIC"
ids, err = GetEntityIds(pluralValue, migrationReq.Identifiers, migrationReq.Names)
if err != nil {
log.Fatal(fmt.Sprintf("Failed to get ids of the %s", pluralValue))
}
if len(ids) == 0 {
log.Fatal(fmt.Sprintf("No %s found with given names/ids", pluralValue))
}
}
log.Info(fmt.Sprintf("Importing the %s....", pluralValue))
scope := AccountScope
if len(migrationReq.AppId) > 0 {
scope = AppScope
}
CreateEntities(getReqBody(entityType, Filter{
AppId: migrationReq.AppId,
Type: importType,
Ids: ids,
Scope: scope,
}))
log.Info(fmt.Sprintf("Imported the %s.", pluralValue))
return nil
}
func LoadYamlFromFile(filePath string) map[string]string {
filePath = strings.TrimSpace(filePath)
if len(filePath) == 0 {
return nil
}
yFile, err := os.ReadFile(filePath)
if err != nil {
log.Fatal(err)
}
data := make(map[string]string)
err = yaml.Unmarshal(yFile, &data)
if err != nil {
log.Fatal(err)
}
log.Infof("Successfully loaded %d custom expressions from the file", len(data))
return data
}
func LoadOverridesFromFile(filePath string) map[string]EntityOverrideInput {
filePath = strings.TrimSpace(filePath)
if len(filePath) == 0 {
return nil
}
yFile, err := os.ReadFile(filePath)
if err != nil {
log.Fatal(err)
}
var data OverrideFileData
err = yaml.Unmarshal(yFile, &data)
if err != nil {
log.Fatal(err)
}
log.Infof("Successfully loaded %d overrides & %d settings from the file", len(data.Overrides), len(data.Settings))
if len(data.Overrides) == 0 {
return nil
}
var overrides = make(map[string]EntityOverrideInput)
var nameToIdMap = make(map[string]map[string]string)
for i, override := range data.Overrides {
assertNotBlank(override.Type, fmt.Sprintf("Type cannot be blank in overrides for index - %d", i))
assertNotAllBlank(fmt.Sprintf("Name, Identifier & Scope are blank in overrides for index - %d", i), override.Name, override.Identifier, override.Scope)
assertAllowedValues(override.Type, []string{UserGroups, Template, Connector, Secret, Service, Environment, Workflow, Pipeline, SecretManager}, fmt.Sprintf("Only a few types of entities support overrides for index %d", i))
if len(strings.TrimSpace(override.ID)) > 0 {
overrides[fmt.Sprintf("CgEntityId(id=%s, type=%s)", override.ID, override.Type)] = EntityOverrideInput{
Name: override.Name,
Identifier: override.Identifier,
Scope: override.Scope,
}
} else {
assertNotBlank(override.FirstGenName, fmt.Sprintf("Both firstGen name & ID fields cannot be blank in overrides for index %d", i))
if len(nameToIdMap[override.Type]) == 0 {
nameToIdMap[override.Type], err = GetEntityNameIdMap(GetEndpointFromType(override.Type))
if err != nil {
log.Fatal(fmt.Sprintf("Failed to fetch ids from names for - %s", override.Type), err)
}
}
id, ok := nameToIdMap[override.Type][override.FirstGenName]
if !ok {
log.Fatal(fmt.Sprintf("Failed to fetch id for name %s of type - %s", override.FirstGenName, override.Type))
}
overrides[fmt.Sprintf("CgEntityId(id=%s, type=%s)", id, override.Type)] = EntityOverrideInput{
Name: override.Name,
Identifier: override.Identifier,
Scope: override.Scope,
}
}
}
return overrides
}
type ReplaceSection struct {
Old string `yaml:"old"`
New string `yaml:"new"`
}
func LoadCustomeStringsFromFile(filePath string) map[string]string {
filePath = strings.TrimSpace(filePath)
if len(filePath) == 0 {
return nil
}
// Read the entire file
content, err := os.ReadFile(filePath)
if err != nil {
log.Info(fmt.Sprintf("error reading file: %v", err))
}
// Unmarshal YAML content into a slice of maps
var replaceSections []map[string]string
err = yaml.Unmarshal(content, &replaceSections)
if err != nil {
log.Info(fmt.Sprintf("error unmarshalling YAML: %v", err))
}
// Create a map to store the accumulated sections
mergedSections := make(map[string]string)
// Process each section
for _, replaceSection := range replaceSections {
oldValue, oldExists := replaceSection["old"]
newValue, newExists := replaceSection["new"]
if oldExists && newExists {
delete(replaceSection, "old")
delete(replaceSection, "new")
mergedSections[oldValue] = newValue
} else {
log.Info("'old' or 'new' keys not found in a section")
}
}
return mergedSections
}
func GetEndpointFromType(entityType string) string {
if entityType == UserGroups {
return "usergroups"
}
return strings.ToLower(entityType + "s")
}
func LoadSettingsFromFile(filePath string) []Setting {
filePath = strings.TrimSpace(filePath)
if len(filePath) == 0 {
return nil
}
yFile, err := os.ReadFile(filePath)
if err != nil {
return nil
}
var data OverrideFileData
err = yaml.Unmarshal(yFile, &data)
if err != nil {
log.Fatal(err)
}
return data.Settings
}
func assertNotBlank(value string, message string) {
if len(strings.TrimSpace(value)) == 0 {
log.Fatal(message)
}
}
func assertAllowedValues(value string, allowed []string, message string) {
if !slices.Contains(allowed, value) {
log.Fatal(message)
}
}
func assertNotAllBlank(message string, values ...*string) {
for _, value := range values {
if value != nil && len(strings.TrimSpace(*value)) > 0 {
return
}
}
log.Fatal(message)
}