forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompts.go
63 lines (47 loc) · 1.27 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
package uaa
import (
"sort"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type Prompt struct {
Key string // e.g. "username"
Type string // e.g. "text", "password"
Label string // e.g. "Username"
}
type PromptAnswer struct {
Key string // e.g. "username"
Value string
}
func (p Prompt) IsPassword() bool { return p.Type == "password" }
type PromptsResp struct {
Prompts map[string][]string // e.g. {"username": ["text", "Username"]}
}
func (u UAAImpl) Prompts() ([]Prompt, error) {
resp, err := u.client.Prompts()
if err != nil {
return nil, err
}
var prompts []Prompt
for key, pair := range resp.Prompts {
prompts = append(prompts, Prompt{
Key: key,
Type: pair[0],
Label: pair[1],
})
}
// Ideally UAA would sort prompts...
sort.Sort(PromptSorting(prompts))
return prompts, nil
}
func (c Client) Prompts() (PromptsResp, error) {
var resp PromptsResp
err := c.clientRequest.Get("/login", &resp)
if err != nil {
return resp, bosherr.WrapError(err, "Requesting UAA prompts")
}
return resp, nil
}
type PromptSorting []Prompt
func (s PromptSorting) Len() int { return len(s) }
func (s PromptSorting) Less(i, j int) bool { return s[i].Type > s[j].Type }
func (s PromptSorting) Swap(i, j int) { s[i], s[j] = s[j], s[i] }