forked from railwayapp/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprompt.go
More file actions
270 lines (241 loc) · 6.67 KB
/
Copy pathprompt.go
File metadata and controls
270 lines (241 loc) · 6.67 KB
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
package ui
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"github.com/manifoldco/promptui"
"github.com/railwayapp/cli/entity"
)
type Prompt string
type Selection string
const (
InitPrompt Prompt = "What would you like to do?"
InitNew Selection = "Create new Project"
InitFromTemplate Selection = "Select starter template"
)
func PromptInit() (Selection, error) {
selectPrompt := promptui.Select{
Label: InitPrompt,
Items: []Selection{InitNew, InitFromTemplate},
}
_, selection, err := selectPrompt.Run()
return Selection(selection), err
}
func PromptText(text string) (string, error) {
prompt := promptui.Prompt{
Label: text,
}
return prompt.Run()
}
func hasTeams(projects []*entity.Project) bool {
for _, project := range projects {
if project.Team != nil {
return true
}
}
return false
}
func promptTeams(projects []*entity.Project) (*string, error) {
if hasTeams(projects) {
tm := make(map[string]bool)
for _, project := range projects {
if project.Team != nil {
tm[*project.Team] = true
}
}
teams := make([]string, 0)
for team := range tm {
teams = append(teams, team)
}
prompt := promptui.Select{
Label: "Select Team",
Items: teams,
Templates: &promptui.SelectTemplates{
Selected: fmt.Sprintf("%s Team: {{ .Name | green | bold }} ", promptui.IconGood),
},
}
_, team, err := prompt.Run()
return &team, err
}
return nil, nil
}
func PromptProjects(projects []*entity.Project) (*entity.Project, error) {
// Check if need to prompt teams
team, err := promptTeams(projects)
if err != nil {
return nil, err
}
filteredProjects := make([]*entity.Project, 0)
for _, project := range projects {
if *project.Team == *team {
filteredProjects = append(filteredProjects, project)
}
}
prompt := promptui.Select{
Label: "Select Project",
Items: filteredProjects,
Templates: &promptui.SelectTemplates{
Active: `{{ .Name | underline }}`,
Inactive: `{{ .Name }}`,
Selected: fmt.Sprintf("%s Project: {{ .Name | magenta | bold }} ", promptui.IconGood),
},
}
i, _, err := prompt.Run()
return projects[i], err
}
// PromptStarterTemplates fetches available templates and prompts the user to select one
func PromptStarterTemplates() (*entity.Template, error) {
StartSpinner(&SpinnerCfg{
Message: "Fetching starter templates",
})
resp, err := http.Get("https://raw.githubusercontent.com/railwayapp/starters/master/featured.json")
if err != nil {
return nil, err
}
defer resp.Body.Close()
var data struct {
Templates []entity.Template `json:"examples"`
}
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&data)
if err != nil {
return nil, err
}
StopSpinner("")
prompt := promptui.Select{
Label: "Select Starter Template",
Items: data.Templates,
Templates: &promptui.SelectTemplates{
Active: fmt.Sprintf("%s {{ .Text | underline }}", promptui.IconSelect),
Inactive: ` {{ .Text }}`,
Selected: fmt.Sprintf("%s Template: {{ .Text | magenta | bold }} ", GreenText("✔")),
},
}
i, _, err := prompt.Run()
return &data.Templates[i], err
}
func PromptIsRepoPrivate() (bool, error) {
prompt := promptui.Select{
Label: "Select repo visibility",
Items: []string{"Public", "Private"},
}
_, visibility, err := prompt.Run()
return visibility == "Private", err
}
func PromptEnvVars(envVars []entity.TemplateEnvVar) (map[string]string, error) {
variables := make(map[string]string)
if len(envVars) > 0 {
fmt.Printf("\n%s\n", Bold("Environment Variables"))
}
for _, envVar := range envVars {
prompt := promptui.Prompt{
Label: envVar.Name,
Default: envVar.DefaultValue,
}
if envVar.Optional {
fmt.Printf("\n%s %s\n", envVar.Desc, GrayText("(Optional)"))
} else {
fmt.Printf("\n%s %s\n", envVar.Desc, GrayText("(Required)"))
prompt.Validate = validatorRequired("value required")
}
v, err := prompt.Run()
if err != nil {
return nil, err
}
variables[envVar.Name] = v
}
// Extra newline to match the ones outputted in the loop
fmt.Print("\n")
return variables, nil
}
func PromptProjectName() (string, error) {
prompt := promptui.Prompt{
Label: "Enter project name",
Templates: &promptui.PromptTemplates{
Prompt: "{{ . }} ",
Valid: fmt.Sprintf("%s {{ . | bold }}: ", promptui.IconGood),
Invalid: fmt.Sprintf("%s {{ . | bold }}: ", promptui.IconBad),
Success: fmt.Sprintf("%s {{ . | magenta | bold }}: ", promptui.IconGood),
},
Validate: validatorRequired("project name required"),
}
return prompt.Run()
}
// PromptGitHubScopes prompts the user to select one of the provides scopes
func PromptGitHubScopes(scopes []string) (string, error) {
if len(scopes) == 1 {
return scopes[0], nil
}
prompt := promptui.Select{
Label: "Select GitHub Owner",
Items: scopes,
Templates: &promptui.SelectTemplates{
Active: fmt.Sprintf("%s {{ . | underline }}", promptui.IconSelect),
Inactive: ` {{ . }}`,
Selected: fmt.Sprintf("%s GitHub: {{ . | magenta | bold }} ", GreenText("✔")),
},
}
_, scope, err := prompt.Run()
return scope, err
}
func PromptEnvironments(environments []*entity.Environment) (*entity.Environment, error) {
if len(environments) == 1 {
environment := environments[0]
fmt.Printf("%s Environment: %s\n", promptui.IconGood, BlueText(environment.Name))
return environment, nil
}
prompt := promptui.Select{
Label: "Select Environment",
Items: environments,
Templates: &promptui.SelectTemplates{
Active: `{{ .Name | underline }}`,
Inactive: `{{ .Name }}`,
Selected: fmt.Sprintf("%s Environment: {{ .Name | blue | bold }} ", promptui.IconGood),
},
}
i, _, err := prompt.Run()
return environments[i], err
}
func PromptPlugins(plugins []string) (string, error) {
prompt := promptui.Select{
Label: "Select Plugin",
Items: plugins,
Templates: &promptui.SelectTemplates{
Active: `{{ . | underline }}`,
Inactive: `{{ . }}`,
Selected: fmt.Sprintf("%s Plugin: {{ . | blue | bold }} ", promptui.IconGood),
},
}
i, _, err := prompt.Run()
return plugins[i], err
}
// PromptYesNo prompts the user to continue an action using the common (y/N) action
func PromptYesNo(msg string) (bool, error) {
fmt.Printf("%s (y/N): ", msg)
var response string
_, err := fmt.Scan(&response)
if err != nil {
return false, err
}
response = strings.ToLower(response)
isNo := response == "n" || response == "no"
isYes := response == "y" || response == "yes"
if isYes {
return true, nil
} else if isNo {
return false, nil
} else {
fmt.Println("Please type yes or no and then press enter:")
return PromptYesNo(msg)
}
}
func validatorRequired(errorMsg string) func(s string) error {
return func(s string) error {
if strings.TrimSpace(s) == "" {
return errors.New(errorMsg)
}
return nil
}
}