-
Notifications
You must be signed in to change notification settings - Fork 5
/
config_command_handler.go
383 lines (339 loc) · 10.5 KB
/
config_command_handler.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
package commandline
import (
"bufio"
"fmt"
"io"
"strings"
"github.com/UiPath/uipathcli/config"
)
// The ConfigCommandHandler implements commands for configuring the CLI.
// The CLI can be configured interactively or by setting config values
// programmatically.
//
// Example:
// uipath config ==> interactive configuration of the CLI
// uipath config set ==> stores a value in the configuration file
type ConfigCommandHandler struct {
StdIn io.Reader
StdOut io.Writer
ConfigProvider config.ConfigProvider
}
const notSetMessage = "not set"
const maskMessage = "*******"
const successfullyConfiguredMessage = "Successfully configured uipath CLI"
const successfullySetMessage = "Successfully set config value"
const CredentialsAuth = "credentials"
const LoginAuth = "login"
const PatAuth = "pat"
func (h ConfigCommandHandler) Set(key string, value string, profileName string) error {
config := h.getOrCreateProfile(profileName)
err := h.setConfigValue(&config, key, value)
if err != nil {
return err
}
err = h.ConfigProvider.Update(profileName, config)
if err != nil {
return err
}
fmt.Fprintln(h.StdOut, successfullySetMessage)
return nil
}
func (h ConfigCommandHandler) setConfigValue(config *config.Config, key string, value string) error {
keyParts := strings.Split(key, ".")
if key == "version" {
config.SetVersion(value)
return nil
} else if key == "organization" {
config.ConfigureOrgTenant(value, "")
return nil
} else if key == "tenant" {
config.ConfigureOrgTenant("", value)
return nil
} else if key == "uri" {
return config.SetUri(value)
} else if key == "insecure" {
insecure, err := h.convertToBool(value)
if err != nil {
return fmt.Errorf("Invalid value for 'insecure': %w", err)
}
config.SetInsecure(insecure)
return nil
} else if key == "debug" {
debug, err := h.convertToBool(value)
if err != nil {
return fmt.Errorf("Invalid value for 'debug': %w", err)
}
config.SetDebug(debug)
return nil
} else if key == "auth.grantType" {
config.SetAuthGrantType(value)
return nil
} else if key == "auth.scopes" {
config.SetAuthScopes(value)
return nil
} else if h.isHeaderKey(keyParts) {
config.SetHeader(keyParts[1], value)
return nil
} else if h.isParameterKey(keyParts) {
config.SetParameter(keyParts[1], value)
return nil
} else if h.isAuthPropertyKey(keyParts) {
config.SetAuthProperty(keyParts[2], value)
return nil
}
return fmt.Errorf("Unknown config key '%s'", key)
}
func (h ConfigCommandHandler) isHeaderKey(keyParts []string) bool {
return len(keyParts) == 2 && keyParts[0] == "header"
}
func (h ConfigCommandHandler) isParameterKey(keyParts []string) bool {
return len(keyParts) == 2 && keyParts[0] == "parameter"
}
func (h ConfigCommandHandler) isAuthPropertyKey(keyParts []string) bool {
return len(keyParts) == 3 && keyParts[0] == "auth" && keyParts[1] == "properties"
}
func (h ConfigCommandHandler) convertToBool(value string) (bool, error) {
if strings.EqualFold(value, "true") {
return true, nil
}
if strings.EqualFold(value, "false") {
return false, nil
}
return false, fmt.Errorf("Invalid boolean value: %s", value)
}
func (h ConfigCommandHandler) Configure(auth string, profileName string) error {
switch auth {
case CredentialsAuth:
return h.configureCredentials(profileName)
case LoginAuth:
return h.configureLogin(profileName)
case PatAuth:
return h.configurePat(profileName)
case "":
return h.configure(profileName)
}
return fmt.Errorf("Invalid auth, supported values: %s, %s, %s", CredentialsAuth, LoginAuth, PatAuth)
}
func (h ConfigCommandHandler) configure(profileName string) error {
config := h.getOrCreateProfile(profileName)
reader := bufio.NewReader(h.StdIn)
organization, tenant, err := h.readOrgTenantInput(config, reader)
if err != nil {
return nil
}
authChanged := h.readAuthInput(config, reader)
orgTenantChanged := config.ConfigureOrgTenant(organization, tenant)
if orgTenantChanged || authChanged {
err = h.ConfigProvider.Update(profileName, config)
if err != nil {
return err
}
fmt.Fprintln(h.StdOut, successfullyConfiguredMessage)
}
return nil
}
func (h ConfigCommandHandler) readAuthInput(config config.Config, reader *bufio.Reader) bool {
authType := h.readAuthTypeInput(config, reader)
switch authType {
case CredentialsAuth:
clientId, clientSecret, err := h.readCredentialsInput(config, reader)
if err != nil {
return false
}
return config.ConfigureCredentialsAuth(clientId, clientSecret)
case LoginAuth:
clientId, redirectUri, scopes, err := h.readLoginInput(config, reader)
if err != nil {
return false
}
return config.ConfigureLoginAuth(clientId, redirectUri, scopes)
case PatAuth:
pat, err := h.readPatInput(config, reader)
if err != nil {
return false
}
return config.ConfigurePatAuth(pat)
default:
return false
}
}
func (h ConfigCommandHandler) configureCredentials(profileName string) error {
config := h.getOrCreateProfile(profileName)
reader := bufio.NewReader(h.StdIn)
organization, tenant, err := h.readOrgTenantInput(config, reader)
if err != nil {
return nil
}
clientId, clientSecret, err := h.readCredentialsInput(config, reader)
if err != nil {
return nil
}
orgTenantChanged := config.ConfigureOrgTenant(organization, tenant)
authChanged := config.ConfigureCredentialsAuth(clientId, clientSecret)
if orgTenantChanged || authChanged {
err = h.ConfigProvider.Update(profileName, config)
if err != nil {
return err
}
fmt.Fprintln(h.StdOut, successfullyConfiguredMessage)
}
return nil
}
func (h ConfigCommandHandler) configureLogin(profileName string) error {
config := h.getOrCreateProfile(profileName)
reader := bufio.NewReader(h.StdIn)
organization, tenant, err := h.readOrgTenantInput(config, reader)
if err != nil {
return nil
}
clientId, redirectUri, scopes, err := h.readLoginInput(config, reader)
if err != nil {
return nil
}
orgTenantChanged := config.ConfigureOrgTenant(organization, tenant)
authChanged := config.ConfigureLoginAuth(clientId, redirectUri, scopes)
if orgTenantChanged || authChanged {
err = h.ConfigProvider.Update(profileName, config)
if err != nil {
return err
}
fmt.Fprintln(h.StdOut, successfullyConfiguredMessage)
}
return nil
}
func (h ConfigCommandHandler) configurePat(profileName string) error {
config := h.getOrCreateProfile(profileName)
reader := bufio.NewReader(h.StdIn)
organization, tenant, err := h.readOrgTenantInput(config, reader)
if err != nil {
return nil
}
pat, err := h.readPatInput(config, reader)
if err != nil {
return nil
}
orgTenantChanged := config.ConfigureOrgTenant(organization, tenant)
authChanged := config.ConfigurePatAuth(pat)
if orgTenantChanged || authChanged {
err = h.ConfigProvider.Update(profileName, config)
if err != nil {
return err
}
fmt.Fprintln(h.StdOut, successfullyConfiguredMessage)
}
return nil
}
func (h ConfigCommandHandler) getAuthType(config config.Config) string {
if config.Pat() != "" {
return PatAuth
}
if config.ClientId() != "" && config.RedirectUri() != "" && config.Scopes() != "" {
return LoginAuth
}
if config.ClientId() != "" && config.ClientSecret() != "" {
return CredentialsAuth
}
return ""
}
func (h ConfigCommandHandler) getOrCreateProfile(profileName string) config.Config {
config := h.ConfigProvider.Config(profileName)
if config == nil {
return h.ConfigProvider.New()
}
return *config
}
func (h ConfigCommandHandler) getDisplayValue(value string, masked bool) string {
if value == "" {
return notSetMessage
}
if masked {
return h.maskValue(value)
}
return value
}
func (h ConfigCommandHandler) maskValue(value string) string {
if len(value) < 10 {
return maskMessage
}
return maskMessage + value[len(value)-4:]
}
func (h ConfigCommandHandler) readUserInput(message string, reader *bufio.Reader) (string, error) {
fmt.Fprint(h.StdOut, message+" ")
value, err := reader.ReadString('\n')
if err != nil {
return "", err
}
return strings.Trim(value, " \r\n\t"), nil
}
func (h ConfigCommandHandler) readOrgTenantInput(config config.Config, reader *bufio.Reader) (string, string, error) {
message := fmt.Sprintf("Enter organization [%s]:", h.getDisplayValue(config.Organization, false))
organization, err := h.readUserInput(message, reader)
if err != nil {
return "", "", err
}
message = fmt.Sprintf("Enter tenant [%s]:", h.getDisplayValue(config.Tenant, false))
tenant, err := h.readUserInput(message, reader)
if err != nil {
return "", "", err
}
return organization, tenant, nil
}
func (h ConfigCommandHandler) readCredentialsInput(config config.Config, reader *bufio.Reader) (string, string, error) {
message := fmt.Sprintf("Enter client id [%s]:", h.getDisplayValue(config.ClientId(), true))
clientId, err := h.readUserInput(message, reader)
if err != nil {
return "", "", err
}
message = fmt.Sprintf("Enter client secret [%s]:", h.getDisplayValue(config.ClientSecret(), true))
clientSecret, err := h.readUserInput(message, reader)
if err != nil {
return "", "", err
}
return clientId, clientSecret, nil
}
func (h ConfigCommandHandler) readLoginInput(config config.Config, reader *bufio.Reader) (string, string, string, error) {
message := fmt.Sprintf("Enter client id [%s]:", h.getDisplayValue(config.ClientId(), true))
clientId, err := h.readUserInput(message, reader)
if err != nil {
return "", "", "", err
}
message = fmt.Sprintf("Enter redirect uri [%s]:", h.getDisplayValue(config.RedirectUri(), false))
redirectUri, err := h.readUserInput(message, reader)
if err != nil {
return "", "", "", err
}
message = fmt.Sprintf("Enter scopes [%s]:", h.getDisplayValue(config.Scopes(), false))
scopes, err := h.readUserInput(message, reader)
if err != nil {
return "", "", "", err
}
return clientId, redirectUri, scopes, nil
}
func (h ConfigCommandHandler) readPatInput(config config.Config, reader *bufio.Reader) (string, error) {
message := fmt.Sprintf("Enter personal access token [%s]:", h.getDisplayValue(config.Pat(), true))
return h.readUserInput(message, reader)
}
func (h ConfigCommandHandler) readAuthTypeInput(config config.Config, reader *bufio.Reader) string {
authType := h.getAuthType(config)
for {
message := fmt.Sprintf(`Authentication type [%s]:
[1] credentials - Client Id and Client Secret
[2] login - OAuth login using the browser
[3] pat - Personal Access Token
Select:`, h.getDisplayValue(authType, false))
input, err := h.readUserInput(message, reader)
if err != nil {
return ""
}
switch input {
case "":
return authType
case "1":
return CredentialsAuth
case "2":
return LoginAuth
case "3":
return PatAuth
}
}
}