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
216 lines (191 loc) · 5.49 KB
/
Copy pathprompt.go
File metadata and controls
216 lines (191 loc) · 5.49 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
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 = "Deploy a starter project"
InitFromAccount Selection = "Connect to existing project"
InitFromID Selection = "Enter existing project id"
)
func PromptInit(isLoggedIn bool) (Selection, error) {
existingProjectPrompt := InitFromID
if isLoggedIn {
existingProjectPrompt = InitFromAccount
}
selectPrompt := promptui.Select{
Label: InitPrompt,
Items: []Selection{InitNew, existingProjectPrompt, InitFromTemplate},
}
_, selection, err := selectPrompt.Run()
return Selection(selection), err
}
func PromptText(text string) (string, error) {
prompt := promptui.Prompt{
Label: text,
}
return prompt.Run()
}
func PromptProjects(projects []*entity.Project) (*entity.Project, error) {
prompt := promptui.Select{
Label: "Select Project",
Items: projects,
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
}
func PromptConfirm(msg string) error {
prompt := promptui.Prompt{
Label: msg,
}
_, err := prompt.Run()
return err
}
func validatorRequired(errorMsg string) func(s string) error {
return func(s string) error {
if strings.TrimSpace(s) == "" {
return errors.New(errorMsg)
}
return nil
}
}