-
Notifications
You must be signed in to change notification settings - Fork 7
/
prompts.go
138 lines (114 loc) · 3.85 KB
/
prompts.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
package main
import (
"crypto/tls"
"net/http"
"net/url"
"regexp"
"strings"
log "github.com/sirupsen/logrus"
)
func PromptDefaultInputs() bool {
promptConfirm := PromptEnvDetails()
if len(migrationReq.SecretScope) == 0 {
promptConfirm = true
migrationReq.SecretScope = SelectInput("Scope for secrets & secret managers:", scopes, Project)
}
if len(migrationReq.ConnectorScope) == 0 {
promptConfirm = true
migrationReq.ConnectorScope = SelectInput("Scope for connectors:", scopes, Project)
}
if len(migrationReq.TemplateScope) == 0 {
promptConfirm = true
migrationReq.TemplateScope = SelectInput("Scope for templates:", scopes, Project)
}
return promptConfirm
}
func PromptSecretDetails() (promptConfirm bool) {
promptConfirm = PromptEnvDetails()
if len(migrationReq.SecretScope) == 0 {
promptConfirm = true
migrationReq.SecretScope = SelectInput("Scope for secrets & secret managers:", scopes, Project)
}
return
}
func PromptConnectorDetails() (promptConfirm bool) {
promptConfirm = PromptSecretDetails()
if len(migrationReq.ConnectorScope) == 0 {
promptConfirm = true
migrationReq.ConnectorScope = SelectInput("Scope for connectors:", scopes, Project)
}
return
}
func PromptEnvDetails() bool {
promptConfirm := false
if len(migrationReq.Environment) == 0 {
promptConfirm = true
migrationReq.Environment = SelectInput("Which environment?", []string{Dev, QA, Prod, Prod3}, Dev)
}
// Check if auth is provided. If not provided then request for one
if len(migrationReq.Auth) == 0 {
migrationReq.Auth = TextInput("The environment variable 'HARNESS_MIGRATOR_AUTH' is not set. What is the api key?")
}
if len(migrationReq.UrlNG) != 0 && len(migrationReq.UrlCG) != 0 {
ParseNGUrl()
ParseCGUrl()
}
if migrationReq.Environment == "Dev" || migrationReq.AllowInsecureReq {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
if len(migrationReq.Account) == 0 {
promptConfirm = true
migrationReq.Account = TextInput("Account that you wish to migrate:")
}
return promptConfirm
}
func PromptOrgAndProject(scope []string) bool {
promptConfirm := false
promptOrg := len(migrationReq.OrgIdentifier) == 0 && ContainsAny(scope, []string{Org, Project})
promptProject := len(migrationReq.ProjectIdentifier) == 0 && ContainsAny(scope, []string{Project})
if promptOrg {
promptConfirm = true
migrationReq.OrgIdentifier = TextInput("Which Org?")
}
if promptProject {
promptConfirm = true
migrationReq.ProjectIdentifier = TextInput("Which Project?")
}
return promptConfirm
}
func ParseNGUrl() {
re := regexp.MustCompile(`https:\/\/.*\.harness\.io/ng/#/account/([a-zA-Z0-9-]+)/.*/orgs/([a-zA-Z0-9_]+)/projects/([a-zA-Z0-9_]+)/.*`)
u, err := url.Parse(migrationReq.UrlNG)
if err != nil {
log.Warning(err)
}
if !re.MatchString(u.String()) {
log.Warning("Destination Project URL did not match the expected format, skipping...")
} else {
u, _ := url.Parse(migrationReq.UrlNG)
fragment := u.Fragment
migrationReq.Account = strings.Split(fragment, "/")[2]
migrationReq.OrgIdentifier = strings.Split(fragment, "/")[5]
migrationReq.ProjectIdentifier = strings.Split(fragment, "/")[7]
migrationReq.ProjectName = strings.Split(fragment, "/")[7]
}
}
func ParseCGUrl() {
re := regexp.MustCompile(`https:\/\/.*\.harness\.io\/#\/account\/[a-zA-Z0-9-]+\/app\/[a-zA-Z0-9-_]+/.*`)
u, err := url.Parse(migrationReq.UrlCG)
if err != nil {
log.Warning(err)
}
if !re.MatchString(u.String()) {
log.Warning("Source Application URL did not match the expected format, skipping...")
} else {
u, _ := url.Parse(migrationReq.UrlCG)
fragment := u.Fragment
acc := strings.Split(fragment, "/")[2]
if acc != migrationReq.Account {
log.Warning("Source Application URL account did not match the Destination Project account provided!")
return
}
migrationReq.AppId = strings.Split(fragment, "/")[4]
}
}