-
Notifications
You must be signed in to change notification settings - Fork 219
/
input.go
389 lines (308 loc) · 9.65 KB
/
input.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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package cmd
import (
"fmt"
"path"
"sort"
"strconv"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/pkg/errors"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/cmdctx"
"github.com/superfly/flyctl/helpers"
"github.com/superfly/flyctl/internal/build/imgsrc/builtins"
)
func isInterrupt(err error) bool {
return err != nil && err.Error() == "interrupt"
}
func confirm(message string) bool {
confirm := false
prompt := &survey.Confirm{
Message: message,
}
err := survey.AskOne(prompt, &confirm)
checkErr(err)
return confirm
}
func selectOrganization(client *api.Client, slug string) (*api.Organization, error) {
orgs, err := client.GetOrganizations()
if err != nil {
return nil, err
}
if slug != "" {
for _, org := range orgs {
if org.Slug == slug {
return &org, nil
}
}
return nil, fmt.Errorf(`organization "%s" not found`, slug)
}
if len(orgs) == 1 && orgs[0].Type == "PERSONAL" {
fmt.Printf("Automatically selected %s organization: %s\n", strings.ToLower(orgs[0].Type), orgs[0].Name)
return &orgs[0], nil
}
sort.Slice(orgs[:], func(i, j int) bool { return orgs[i].Type < orgs[j].Type })
options := []string{}
for _, org := range orgs {
options = append(options, fmt.Sprintf("%s (%s)", org.Name, org.Slug))
}
selectedOrg := 0
prompt := &survey.Select{
Message: "Select organization:",
Options: options,
PageSize: 15,
}
if err := survey.AskOne(prompt, &selectedOrg); err != nil {
return nil, err
}
return &orgs[selectedOrg], nil
}
func selectRegion(client *api.Client, regionCode string) (*api.Region, error) {
regions, requestRegion, err := client.PlatformRegions()
if err != nil {
return nil, err
}
if regionCode != "" {
for _, region := range regions {
if region.Code == regionCode {
return ®ion, nil
}
}
return nil, fmt.Errorf(`region "%s" not found`, regionCode)
}
options := []string{}
for _, region := range regions {
options = append(options, fmt.Sprintf("%s (%s)", region.Code, region.Name))
}
selectedRegion := 0
prompt := &survey.Select{
Message: "Select region:",
Options: options,
PageSize: 15,
}
if requestRegion != nil {
prompt.Default = fmt.Sprintf("%s (%s)", requestRegion.Code, requestRegion.Name)
}
if err := survey.AskOne(prompt, &selectedRegion); err != nil {
return nil, err
}
return ®ions[selectedRegion], nil
}
func selectVMSize(client *api.Client, vmSizeName string) (*api.VMSize, error) {
vmSizes, err := client.PlatformVMSizes()
if err != nil {
return nil, err
}
if vmSizeName != "" {
for _, vmSize := range vmSizes {
if vmSize.Name == vmSizeName {
return &vmSize, nil
}
}
return nil, fmt.Errorf(`vm size "%s" not found`, vmSizeName)
}
options := []string{}
for _, vmSize := range vmSizes {
options = append(options, fmt.Sprintf("%s - %d", vmSize.Name, vmSize.MemoryMB))
}
selectedVMSize := 0
prompt := &survey.Select{
Message: "Select VM size:",
Options: options,
PageSize: 15,
}
if err := survey.AskOne(prompt, &selectedVMSize); err != nil {
return nil, err
}
return &vmSizes[selectedVMSize], nil
}
func inputAppName(defaultName string) (name string, err error) {
prompt := &survey.Input{
Message: "App name:",
Default: defaultName,
}
if err := survey.AskOne(prompt, &name); err != nil {
return name, err
}
return name, nil
}
func volumeSizeInput(client *api.Client, defaultVal int) (int, error) {
var volumeSize int
prompt := &survey.Input{
Message: "Volume size (GB):",
Default: strconv.Itoa(defaultVal),
}
if err := survey.AskOne(prompt, &volumeSize); err != nil {
return 0, err
}
return volumeSize, nil
}
// func selectZone(client *api.Client, orgslug string, slug string) (string, error) {
// return "", nil
// // zones, err := client.GetDNSZones(orgslug)
// // if err != nil {
// // return "", err
// // }
// // sort.Slice(zones[:], func(i, j int) bool { return (*zones[i]).Domain < (*zones[j]).Domain })
// // options := []string{}
// // for _, zone := range zones {
// // options = append(options, fmt.Sprintf("%s", zone.Domain))
// // }
// // selectedOrg := 0
// // prompt := &survey.Select{
// // Message: "Select zone:",
// // Options: options,
// // PageSize: 15,
// // }
// // if err := survey.AskOne(prompt, &selectedOrg); err != nil {
// // return "", err
// // }
// // return (*zones[selectedOrg]).Domain, nil
// }
type suggestedBuilder struct {
Vendor string
Image string
DefaultDescription string
}
var suggestedBuilders = []suggestedBuilder{
{
Vendor: "Google",
Image: "gcr.io/buildpacks/builder",
DefaultDescription: "GCP Builder for all runtimes",
},
{
Vendor: "Heroku",
Image: "heroku/buildpacks:18",
DefaultDescription: "heroku-18 base image with buildpacks for Ruby, Java, Node.js, Python, Golang, & PHP",
},
{
Vendor: "Paketo Buildpacks",
Image: "gcr.io/paketo-buildpacks/builder:base",
DefaultDescription: "Small base image with buildpacks for Java, Node.js, Golang, & .NET Core",
},
{
Vendor: "Paketo Buildpacks",
Image: "gcr.io/paketo-buildpacks/builder:full-cf",
DefaultDescription: "Larger base image with buildpacks for Java, Node.js, Golang, .NET Core, & PHP",
},
{
Vendor: "Paketo Buildpacks",
Image: "gcr.io/paketo-buildpacks/builder:tiny",
DefaultDescription: "Tiny base image (bionic build image, distroless run image) with buildpacks for Golang",
},
{
Vendor: "Fly",
Image: "flyio/builder",
DefaultDescription: "Fly's own Buildpack - currently supporting Deno",
},
}
func selectBuildtype(commandContext *cmdctx.CmdContext) (string, bool, error) {
dockerfileExists := helpers.FileExists(path.Join(commandContext.WorkingDir, "Dockerfile"))
builders := []string{}
// None/Dockerfile first - always entry 0
if dockerfileExists {
builders = append(builders, fmt.Sprintf("%s\n (%s)", "Dockerfile", "Do not set a builder and use the existing Dockerfile"))
} else {
builders = append(builders, fmt.Sprintf("%s\n (%s)", "None", "Do not set a builder"))
}
builders = append(builders, fmt.Sprintf("%s\n (%s)", "Image", "Use a public Docker image"))
builtins := builtins.GetBuiltins()
sort.Slice(builtins, func(i, j int) bool { return builtins[i].Name < builtins[j].Name })
builtinsStart := len(builders)
for _, b := range builtins {
builders = append(builders, fmt.Sprintf("%s\n %s", b.Name, helpers.WrapString(b.Description, 60, 4)))
}
buildersStart := len(builders)
for _, b := range suggestedBuilders {
builders = append(builders, fmt.Sprintf("%s\n %s", b.Image, helpers.WrapString(b.DefaultDescription, 70, 4)))
}
selectedBuilder := 0
prompt := &survey.Select{
Message: "Select builder:",
Options: builders,
PageSize: 8,
}
if err := survey.AskOne(prompt, &selectedBuilder); err != nil {
return "", false, err
}
// Selected a Dockerfile or the absence of a builder
if selectedBuilder == 0 {
if dockerfileExists {
return "Dockerfile", false, nil
}
return "None", false, nil
}
// Selected an image
if selectedBuilder == 1 {
return "Image", false, nil
}
if selectedBuilder >= builtinsStart && selectedBuilder < buildersStart {
// Selected a built in
return builtins[selectedBuilder-builtinsStart].Name, true, nil
}
// All that is lef is builders by name
return suggestedBuilders[selectedBuilder-buildersStart].Image, false, nil
}
func selectBuiltin(commandContext *cmdctx.CmdContext) (string, error) {
availablebuiltins := []string{}
builtins := builtins.GetBuiltins()
sort.Slice(builtins, func(i, j int) bool { return builtins[i].Name < builtins[j].Name })
for _, b := range builtins {
availablebuiltins = append(availablebuiltins, fmt.Sprintf("%s\n %s", b.Name, helpers.WrapString(b.Description, 60, 4)))
}
selectedBuiltin := 0
prompt := &survey.Select{
Message: "Select builtin:",
Options: availablebuiltins,
PageSize: 8,
}
if err := survey.AskOne(prompt, &selectedBuiltin); err != nil {
return "", err
}
return builtins[selectedBuiltin].Name, nil
}
func selectImage(commandContext *cmdctx.CmdContext) (string, error) {
prompt := &survey.Input{Message: "Select Image:", Default: "flyio/hellofly:latest", Help: `The name and tag for the image you want to use.`}
sSelectedImage := ""
if err := survey.AskOne(prompt, &sSelectedImage /* survey.WithValidator(isIntPort) */); err != nil {
return sSelectedImage, err
}
return sSelectedImage, nil
}
func selectPort(commandContext *cmdctx.CmdContext, defport int) (int, error) {
sDefport := strconv.Itoa(defport)
prompt := &survey.Input{Message: "Select Internal Port:", Default: sDefport, Help: `The internal port is the port your application uses. External traffic will be directed to this port.
If incorrectly set, health checks may fail and your application deployment will fail.`}
sSelectedPort := ""
if err := survey.AskOne(prompt, &sSelectedPort, survey.WithValidator(isIntPort)); err != nil {
return -1, err
}
selectedPort, err := strconv.Atoi(sSelectedPort)
if err != nil {
return -1, errors.New("number did not parse after verification")
}
return selectedPort, nil
}
func isIntPort(val interface{}) error {
str, ok := val.(string)
if ok {
val, err := strconv.Atoi(str)
if err != nil {
return errors.New("has to be an integer")
}
if val < 1 || val > 65536 {
return errors.New("port must be between 1 and 65536")
}
return nil
}
return errors.New("couldn't convert to string")
}
func inputUserEmail() (email string, err error) {
prompt := &survey.Input{
Message: "User email:",
}
if err := survey.AskOne(prompt, &email); err != nil {
return email, err
}
return email, nil
}