/
select.go
185 lines (149 loc) · 5.45 KB
/
select.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
package prompts
import (
"fmt"
"github.com/manifoldco/promptui"
"github.com/manifoldco/torus-cli/errs"
"github.com/manifoldco/torus-cli/prefs"
"github.com/manifoldco/torus-cli/ui"
"github.com/manifoldco/torus-cli/validate"
)
// SelectPrompt represents a function that prompts the user with a list of
// choices they can select. If the choice exists then the index and displayed
// name of the item are returned and the prompt is not displayed.
type SelectPrompt func([]string, string) (int, string, error)
// SelectCreatePrompt represents a function that prompts the user witha list
// of choices they can select or the ability to create a new item. If they
// choose to create a new item they are prompted with a string prompt. The
// index returned will be SelectedAdd.
//
// A user can provide a default choice, if it exists then the index and
// displayed name of the item are returned. The success state of the prompt is
// displayed.
type SelectCreatePrompt func([]string, string) (int, string, error)
// SelectOrg prompts the user to select or create an org depending on arguments
// provided by the callee.
// SelectOrg prompts the user to select an org.
var SelectOrg SelectPrompt
// SelectCreateOrg prompts the user to select or create an org
var SelectCreateOrg SelectCreatePrompt
// SelectRole prompts the user to select a role.
var SelectRole SelectPrompt
// SelectCreateRole prompts the user to select or create a new role.
var SelectCreateRole SelectCreatePrompt
// SelectTeam prompts the user to select a team.
var SelectTeam SelectPrompt
// SelectCreateTeam prompts the user to select or create a new team.
var SelectCreateTeam SelectCreatePrompt
// SelectProject prompts the user to select a project.
var SelectProject SelectPrompt
// SelectCreateProject prompts the user to select or create a new project.
var SelectCreateProject SelectCreatePrompt
// SelectAcceptAction prompts the user to select whether they want to log into
// an existing account or to create a new one when they're accepting an org
// invitation.
func SelectAcceptAction() (int, string, error) {
label := "Do you want to login or create an account?"
v := []string{"Login", "Signup"}
p := selectPrompt(label, "Action", validate.OneOf(v))
return p(v, "")
}
func selectTmpl(name string) *promptui.SelectTemplates {
return &promptui.SelectTemplates{
Label: fmt.Sprintf(`%s {{ . | bold }}: `, promptui.IconInitial),
Active: fmt.Sprintf(`%s {{ . | underline }}`, promptui.IconSelect),
Inactive: ` {{ . }}`,
Selected: fmt.Sprintf(`{{ "%s" | green }} {{ "%s" | bold }}: {{ . | faint }}`, promptui.IconGood, name),
}
}
func selectCreatePrompt(label, addLabel, name string, validator validate.Func) SelectCreatePrompt {
return func(values []string, providedValue string) (int, string, error) {
preferences, err := prefs.NewPreferences()
if err != nil {
return 0, "", err
}
tmpl := selectTmpl(name)
createTmpl := stringTmpl()
if providedValue != "" || !ui.Attached() {
err = validator(providedValue)
if err != nil {
fmt.Println(direct(createTmpl.Invalid, name, providedValue))
return 0, "", err
}
idx := find(values, providedValue)
if idx == -1 {
fmt.Println(direct(createTmpl.Invalid, name, providedValue))
return 0, "", errs.NewNotFound(name)
}
fmt.Println(direct(createTmpl.Valid, name, providedValue))
return idx, providedValue, nil
}
prompt := selectWithAdd{
Label: label,
AddLabel: addLabel,
Items: values,
Validate: promptui.ValidateFunc(validator),
IsVimMode: preferences.Core.Vim,
SelectTemplates: tmpl,
PromptTemplates: createTmpl,
}
idx, value, err := prompt.Run()
if err != nil {
return 0, "", convertErr(err)
}
if idx == promptui.SelectedAdd {
return SelectedAdd, value, nil
}
return idx, value, nil
}
}
func selectPrompt(label, name string, validator validate.Func) SelectPrompt {
return func(values []string, providedValue string) (int, string, error) {
preferences, err := prefs.NewPreferences()
if err != nil {
return 0, "", err
}
if providedValue != "" || !ui.Attached() {
err = validator(providedValue)
if err != nil {
return 0, "", err
}
idx := find(values, providedValue)
if idx == -1 {
return 0, "", errs.NewNotFound(name)
}
return idx, providedValue, nil
}
tmpl := selectTmpl(name)
prompt := promptui.Select{
Label: label,
Items: values,
Templates: tmpl,
IsVimMode: preferences.Core.Vim,
}
idx, value, err := prompt.Run()
if err != nil {
return 0, "", convertErr(err)
}
return idx, value, nil
}
}
func find(values []string, name string) int {
idx := -1
for i, v := range values {
if v == name {
idx = i
break
}
}
return idx
}
func init() {
SelectOrg = selectPrompt("Select an org", "Org", validate.OrgName)
SelectCreateOrg = selectCreatePrompt("Select an org", "Create a new org", "Org", validate.OrgName)
SelectRole = selectPrompt("Select a Role", "Machine Role", validate.RoleName)
SelectCreateRole = selectCreatePrompt("Select a role", "Create a new role", "Machine Role", validate.RoleName)
SelectTeam = selectPrompt("Select a team", "Team", validate.TeamName)
SelectCreateTeam = selectCreatePrompt("Select a team", "Create a new team", "Team", validate.TeamName)
SelectProject = selectPrompt("Select a project", "Project", validate.ProjectName)
SelectCreateProject = selectCreatePrompt("Select a project", "Create a new project", "Project", validate.ProjectName)
}