• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

UiPath / uipathcli / 6442892572

07 Oct 2023 06:45PM UTC coverage: 67.119% (-23.3%) from 90.418%
6442892572

push

github

thschmitt
Upgrade to golang 1.21, update dependencies to latest versions

- Upgrade to golang 1.21
- Update all dependencies to latest versions

3068 of 4571 relevant lines covered (67.12%)

362.28 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/commandline/config_command_handler.go
1
package commandline
2

3
import (
4
        "bufio"
5
        "fmt"
6
        "io"
7
        "strings"
8

9
        "github.com/UiPath/uipathcli/config"
10
)
11

12
// The ConfigCommandHandler implements commands for configuring the CLI.
13
// The CLI can be configured interactively or by setting config values
14
// programmatically.
15
//
16
// Example:
17
// uipath config ==> interactive configuration of the CLI
18
// uipath config set ==> stores a value in the configuration file
19
type ConfigCommandHandler struct {
20
        StdIn          io.Reader
21
        StdOut         io.Writer
22
        ConfigProvider config.ConfigProvider
23
}
24

25
const notSetMessage = "not set"
26
const maskMessage = "*******"
27
const successfullyConfiguredMessage = "Successfully configured uipath CLI"
28
const successfullySetMessage = "Successfully set config value"
29

30
const CredentialsAuth = "credentials"
31
const LoginAuth = "login"
32
const PatAuth = "pat"
33

34
func (h ConfigCommandHandler) Set(key string, value string, profileName string) error {
×
35
        config := h.getOrCreateProfile(profileName)
×
36
        err := h.setConfigValue(&config, key, value)
×
37
        if err != nil {
×
38
                return err
×
39
        }
×
40
        err = h.ConfigProvider.Update(profileName, config)
×
41
        if err != nil {
×
42
                return err
×
43
        }
×
44
        fmt.Fprintln(h.StdOut, successfullySetMessage)
×
45
        return nil
×
46
}
47

48
func (h ConfigCommandHandler) setConfigValue(config *config.Config, key string, value string) error {
×
49
        keyParts := strings.Split(key, ".")
×
50
        if key == "version" {
×
51
                config.SetVersion(value)
×
52
                return nil
×
53
        } else if key == "organization" {
×
54
                config.ConfigureOrgTenant(value, "")
×
55
                return nil
×
56
        } else if key == "tenant" {
×
57
                config.ConfigureOrgTenant("", value)
×
58
                return nil
×
59
        } else if key == "uri" {
×
60
                return config.SetUri(value)
×
61
        } else if key == "insecure" {
×
62
                insecure, err := h.convertToBool(value)
×
63
                if err != nil {
×
64
                        return fmt.Errorf("Invalid value for 'insecure': %w", err)
×
65
                }
×
66
                config.SetInsecure(insecure)
×
67
                return nil
×
68
        } else if key == "debug" {
×
69
                debug, err := h.convertToBool(value)
×
70
                if err != nil {
×
71
                        return fmt.Errorf("Invalid value for 'debug': %w", err)
×
72
                }
×
73
                config.SetDebug(debug)
×
74
                return nil
×
75
        } else if key == "auth.grantType" {
×
76
                config.SetAuthGrantType(value)
×
77
                return nil
×
78
        } else if key == "auth.scopes" {
×
79
                config.SetAuthScopes(value)
×
80
                return nil
×
81
        } else if h.isHeaderKey(keyParts) {
×
82
                config.SetHeader(keyParts[1], value)
×
83
                return nil
×
84
        } else if h.isParameterKey(keyParts) {
×
85
                config.SetParameter(keyParts[1], value)
×
86
                return nil
×
87
        } else if h.isAuthPropertyKey(keyParts) {
×
88
                config.SetAuthProperty(keyParts[2], value)
×
89
                return nil
×
90
        }
×
91
        return fmt.Errorf("Unknown config key '%s'", key)
×
92
}
93

94
func (h ConfigCommandHandler) isHeaderKey(keyParts []string) bool {
×
95
        return len(keyParts) == 2 && keyParts[0] == "header"
×
96
}
×
97

98
func (h ConfigCommandHandler) isParameterKey(keyParts []string) bool {
×
99
        return len(keyParts) == 2 && keyParts[0] == "parameter"
×
100
}
×
101

102
func (h ConfigCommandHandler) isAuthPropertyKey(keyParts []string) bool {
×
103
        return len(keyParts) == 3 && keyParts[0] == "auth" && keyParts[1] == "properties"
×
104
}
×
105

106
func (h ConfigCommandHandler) convertToBool(value string) (bool, error) {
×
107
        if strings.EqualFold(value, "true") {
×
108
                return true, nil
×
109
        }
×
110
        if strings.EqualFold(value, "false") {
×
111
                return false, nil
×
112
        }
×
113
        return false, fmt.Errorf("Invalid boolean value: %s", value)
×
114
}
115

116
func (h ConfigCommandHandler) Configure(auth string, profileName string) error {
×
117
        switch auth {
×
118
        case CredentialsAuth:
×
119
                return h.configureCredentials(profileName)
×
120
        case LoginAuth:
×
121
                return h.configureLogin(profileName)
×
122
        case PatAuth:
×
123
                return h.configurePat(profileName)
×
124
        case "":
×
125
                return h.configure(profileName)
×
126
        }
127
        return fmt.Errorf("Invalid auth, supported values: %s, %s, %s", CredentialsAuth, LoginAuth, PatAuth)
×
128
}
129

130
func (h ConfigCommandHandler) configure(profileName string) error {
×
131
        config := h.getOrCreateProfile(profileName)
×
132
        reader := bufio.NewReader(h.StdIn)
×
133

×
134
        organization, tenant, err := h.readOrgTenantInput(config, reader)
×
135
        if err != nil {
×
136
                return nil
×
137
        }
×
138

139
        authChanged := h.readAuthInput(config, reader)
×
140
        orgTenantChanged := config.ConfigureOrgTenant(organization, tenant)
×
141

×
142
        if orgTenantChanged || authChanged {
×
143
                err = h.ConfigProvider.Update(profileName, config)
×
144
                if err != nil {
×
145
                        return err
×
146
                }
×
147
                fmt.Fprintln(h.StdOut, successfullyConfiguredMessage)
×
148
        }
149
        return nil
×
150
}
151

152
func (h ConfigCommandHandler) readAuthInput(config config.Config, reader *bufio.Reader) bool {
×
153
        authType := h.readAuthTypeInput(config, reader)
×
154
        switch authType {
×
155
        case CredentialsAuth:
×
156
                clientId, clientSecret, err := h.readCredentialsInput(config, reader)
×
157
                if err != nil {
×
158
                        return false
×
159
                }
×
160
                return config.ConfigureCredentialsAuth(clientId, clientSecret)
×
161
        case LoginAuth:
×
162
                clientId, redirectUri, scopes, err := h.readLoginInput(config, reader)
×
163
                if err != nil {
×
164
                        return false
×
165
                }
×
166
                return config.ConfigureLoginAuth(clientId, redirectUri, scopes)
×
167
        case PatAuth:
×
168
                pat, err := h.readPatInput(config, reader)
×
169
                if err != nil {
×
170
                        return false
×
171
                }
×
172
                return config.ConfigurePatAuth(pat)
×
173
        default:
×
174
                return false
×
175
        }
176
}
177

178
func (h ConfigCommandHandler) configureCredentials(profileName string) error {
×
179
        config := h.getOrCreateProfile(profileName)
×
180
        reader := bufio.NewReader(h.StdIn)
×
181

×
182
        organization, tenant, err := h.readOrgTenantInput(config, reader)
×
183
        if err != nil {
×
184
                return nil
×
185
        }
×
186
        clientId, clientSecret, err := h.readCredentialsInput(config, reader)
×
187
        if err != nil {
×
188
                return nil
×
189
        }
×
190

191
        orgTenantChanged := config.ConfigureOrgTenant(organization, tenant)
×
192
        authChanged := config.ConfigureCredentialsAuth(clientId, clientSecret)
×
193

×
194
        if orgTenantChanged || authChanged {
×
195
                err = h.ConfigProvider.Update(profileName, config)
×
196
                if err != nil {
×
197
                        return err
×
198
                }
×
199
                fmt.Fprintln(h.StdOut, successfullyConfiguredMessage)
×
200
        }
201
        return nil
×
202
}
203

204
func (h ConfigCommandHandler) configureLogin(profileName string) error {
×
205
        config := h.getOrCreateProfile(profileName)
×
206
        reader := bufio.NewReader(h.StdIn)
×
207

×
208
        organization, tenant, err := h.readOrgTenantInput(config, reader)
×
209
        if err != nil {
×
210
                return nil
×
211
        }
×
212
        clientId, redirectUri, scopes, err := h.readLoginInput(config, reader)
×
213
        if err != nil {
×
214
                return nil
×
215
        }
×
216

217
        orgTenantChanged := config.ConfigureOrgTenant(organization, tenant)
×
218
        authChanged := config.ConfigureLoginAuth(clientId, redirectUri, scopes)
×
219

×
220
        if orgTenantChanged || authChanged {
×
221
                err = h.ConfigProvider.Update(profileName, config)
×
222
                if err != nil {
×
223
                        return err
×
224
                }
×
225
                fmt.Fprintln(h.StdOut, successfullyConfiguredMessage)
×
226
        }
227
        return nil
×
228
}
229

230
func (h ConfigCommandHandler) configurePat(profileName string) error {
×
231
        config := h.getOrCreateProfile(profileName)
×
232
        reader := bufio.NewReader(h.StdIn)
×
233

×
234
        organization, tenant, err := h.readOrgTenantInput(config, reader)
×
235
        if err != nil {
×
236
                return nil
×
237
        }
×
238
        pat, err := h.readPatInput(config, reader)
×
239
        if err != nil {
×
240
                return nil
×
241
        }
×
242

243
        orgTenantChanged := config.ConfigureOrgTenant(organization, tenant)
×
244
        authChanged := config.ConfigurePatAuth(pat)
×
245

×
246
        if orgTenantChanged || authChanged {
×
247
                err = h.ConfigProvider.Update(profileName, config)
×
248
                if err != nil {
×
249
                        return err
×
250
                }
×
251
                fmt.Fprintln(h.StdOut, successfullyConfiguredMessage)
×
252
        }
253
        return nil
×
254
}
255

256
func (h ConfigCommandHandler) getAuthType(config config.Config) string {
×
257
        if config.Pat() != "" {
×
258
                return PatAuth
×
259
        }
×
260
        if config.ClientId() != "" && config.RedirectUri() != "" && config.Scopes() != "" {
×
261
                return LoginAuth
×
262
        }
×
263
        if config.ClientId() != "" && config.ClientSecret() != "" {
×
264
                return CredentialsAuth
×
265
        }
×
266
        return ""
×
267
}
268

269
func (h ConfigCommandHandler) getOrCreateProfile(profileName string) config.Config {
×
270
        config := h.ConfigProvider.Config(profileName)
×
271
        if config == nil {
×
272
                return h.ConfigProvider.New()
×
273
        }
×
274
        return *config
×
275
}
276

277
func (h ConfigCommandHandler) getDisplayValue(value string, masked bool) string {
×
278
        if value == "" {
×
279
                return notSetMessage
×
280
        }
×
281
        if masked {
×
282
                return h.maskValue(value)
×
283
        }
×
284
        return value
×
285
}
286

287
func (h ConfigCommandHandler) maskValue(value string) string {
×
288
        if len(value) < 10 {
×
289
                return maskMessage
×
290
        }
×
291
        return maskMessage + value[len(value)-4:]
×
292
}
293

294
func (h ConfigCommandHandler) readUserInput(message string, reader *bufio.Reader) (string, error) {
×
295
        fmt.Fprint(h.StdOut, message+" ")
×
296
        value, err := reader.ReadString('\n')
×
297
        if err != nil {
×
298
                return "", err
×
299
        }
×
300
        return strings.Trim(value, " \r\n\t"), nil
×
301
}
302

303
func (h ConfigCommandHandler) readOrgTenantInput(config config.Config, reader *bufio.Reader) (string, string, error) {
×
304
        message := fmt.Sprintf("Enter organization [%s]:", h.getDisplayValue(config.Organization, false))
×
305
        organization, err := h.readUserInput(message, reader)
×
306
        if err != nil {
×
307
                return "", "", err
×
308
        }
×
309

310
        message = fmt.Sprintf("Enter tenant [%s]:", h.getDisplayValue(config.Tenant, false))
×
311
        tenant, err := h.readUserInput(message, reader)
×
312
        if err != nil {
×
313
                return "", "", err
×
314
        }
×
315

316
        return organization, tenant, nil
×
317
}
318

319
func (h ConfigCommandHandler) readCredentialsInput(config config.Config, reader *bufio.Reader) (string, string, error) {
×
320
        message := fmt.Sprintf("Enter client id [%s]:", h.getDisplayValue(config.ClientId(), true))
×
321
        clientId, err := h.readUserInput(message, reader)
×
322
        if err != nil {
×
323
                return "", "", err
×
324
        }
×
325

326
        message = fmt.Sprintf("Enter client secret [%s]:", h.getDisplayValue(config.ClientSecret(), true))
×
327
        clientSecret, err := h.readUserInput(message, reader)
×
328
        if err != nil {
×
329
                return "", "", err
×
330
        }
×
331

332
        return clientId, clientSecret, nil
×
333
}
334

335
func (h ConfigCommandHandler) readLoginInput(config config.Config, reader *bufio.Reader) (string, string, string, error) {
×
336
        message := fmt.Sprintf("Enter client id [%s]:", h.getDisplayValue(config.ClientId(), true))
×
337
        clientId, err := h.readUserInput(message, reader)
×
338
        if err != nil {
×
339
                return "", "", "", err
×
340
        }
×
341
        message = fmt.Sprintf("Enter redirect uri [%s]:", h.getDisplayValue(config.RedirectUri(), false))
×
342
        redirectUri, err := h.readUserInput(message, reader)
×
343
        if err != nil {
×
344
                return "", "", "", err
×
345
        }
×
346
        message = fmt.Sprintf("Enter scopes [%s]:", h.getDisplayValue(config.Scopes(), false))
×
347
        scopes, err := h.readUserInput(message, reader)
×
348
        if err != nil {
×
349
                return "", "", "", err
×
350
        }
×
351

352
        return clientId, redirectUri, scopes, nil
×
353
}
354

355
func (h ConfigCommandHandler) readPatInput(config config.Config, reader *bufio.Reader) (string, error) {
×
356
        message := fmt.Sprintf("Enter personal access token [%s]:", h.getDisplayValue(config.Pat(), true))
×
357
        return h.readUserInput(message, reader)
×
358
}
×
359

360
func (h ConfigCommandHandler) readAuthTypeInput(config config.Config, reader *bufio.Reader) string {
×
361
        authType := h.getAuthType(config)
×
362
        for {
×
363
                message := fmt.Sprintf(`Authentication type [%s]:
×
364
  [1] credentials - Client Id and Client Secret
×
365
  [2] login - OAuth login using the browser
×
366
  [3] pat - Personal Access Token
×
367
Select:`, h.getDisplayValue(authType, false))
×
368
                input, err := h.readUserInput(message, reader)
×
369
                if err != nil {
×
370
                        return ""
×
371
                }
×
372
                switch input {
×
373
                case "":
×
374
                        return authType
×
375
                case "1":
×
376
                        return CredentialsAuth
×
377
                case "2":
×
378
                        return LoginAuth
×
379
                case "3":
×
380
                        return PatAuth
×
381
                }
382
        }
383
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc