-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt.go
450 lines (413 loc) · 10.6 KB
/
prompt.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package cmd
import (
"context"
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/core"
"github.com/AlecAivazis/survey/v2/terminal"
"github.com/iancoleman/orderedmap"
"github.com/lade-io/go-lade"
"github.com/mattn/go-colorable"
"github.com/mattn/go-isatty"
"github.com/mgutz/ansi"
"github.com/olekukonko/ts"
)
var (
validEnvName = regexp.MustCompile(`^[A-Z0-9-_]+$`)
validName = regexp.MustCompile(`^[a-z][a-z0-9-_]*$`)
)
type optionsFunc func(*lade.Client) (*orderedmap.OrderedMap, error)
func askError(err error) error {
if err == terminal.InterruptErr {
os.Exit(1)
}
return err
}
func askConfirm(msg string, choice bool, result interface{}) error {
prompt := &survey.Confirm{Message: msg, Default: choice}
return askError(survey.AskOne(prompt, result, nil))
}
func askInput(msg string, value, result interface{}, validator survey.Validator) error {
if !isZero(result) {
if validator != nil {
return validator(toString(result))
}
return nil
}
var choice string
switch v := value.(type) {
case func() string:
choice = v()
case string:
choice = v
case int:
choice = strconv.Itoa(v)
}
prompt := &survey.Input{Message: msg, Default: choice}
return askError(survey.AskOne(prompt, result, survey.WithValidator(validator)))
}
func askSelect(msg string, value interface{}, client *lade.Client, fn optionsFunc, result interface{}) error {
if !isZero(result) {
return nil
}
var wg sync.WaitGroup
var choice string
switch v := value.(type) {
case func(client *lade.Client) string:
wg.Add(1)
go func() {
defer wg.Done()
choice = v(client)
}()
case func() string:
choice = v()
case string:
choice = v
}
options, err := fn(client)
if err != nil {
return err
}
wg.Wait()
prompt := &survey.Select{Message: msg, Options: options.Keys(), PageSize: getPageSize()}
if choice != "" {
key, ok := options.GetKey(choice)
if ok {
prompt.Default = key
}
}
var answer string
err = survey.AskOne(prompt, &answer, nil)
if err != nil {
return askError(err)
}
value, ok := options.Get(answer)
if ok {
core.WriteAnswer(result, "", value)
}
return nil
}
func askMultiSelect(msg string, client *lade.Client, fn optionsFunc, result interface{}, validator survey.Validator) error {
if !isZero(result) {
return nil
}
options, err := fn(client)
if err != nil {
return err
}
var answers []string
prompt := &survey.MultiSelect{Message: msg, Options: options.Keys(), PageSize: getPageSize()}
err = survey.AskOne(prompt, &answers, survey.WithValidator(validator))
if err != nil {
return askError(err)
}
var values []string
for _, answer := range answers {
value, ok := options.Get(answer)
if ok {
values = append(values, value.(string))
}
}
core.WriteAnswer(result, "", values)
return nil
}
func getAppOptions(client *lade.Client) (*orderedmap.OrderedMap, error) {
apps, err := client.App.List()
if err != nil {
return nil, err
}
if len(apps) == 0 {
return nil, errors.New("You have not created any apps")
}
options := orderedmap.New()
for _, app := range apps {
options.Set(app.Name, app.Name)
}
options.SortKeys(sort.Strings)
return options, nil
}
func getAddonOptions(client *lade.Client) (*orderedmap.OrderedMap, error) {
addons, err := client.Addon.List()
if err != nil {
return nil, err
}
if len(addons) == 0 {
return nil, errors.New("You have not created any addons")
}
options := orderedmap.New()
for _, addon := range addons {
options.Set(addon.Name, addon.Name)
}
options.SortKeys(sort.Strings)
return options, nil
}
func getDomainOptions(appName string) optionsFunc {
return func(client *lade.Client) (*orderedmap.OrderedMap, error) {
domains, err := client.Domain.List(appName)
if err != nil {
return nil, err
}
if len(domains) == 0 {
return nil, errors.New("There are no domains available")
}
options := orderedmap.New()
for _, domain := range domains {
options.Set(domain.Hostname, domain.Hostname)
}
return options, nil
}
}
func getKeyOptions(appName string) optionsFunc {
return func(client *lade.Client) (*orderedmap.OrderedMap, error) {
envs, err := client.Env.List(appName)
if err != nil {
return nil, err
}
if len(envs) == 0 {
return nil, errors.New("There are no keys available")
}
options := orderedmap.New()
for _, env := range envs {
options.Set(env.Name, env.Name)
}
return options, nil
}
}
func getPlanOptions(id string) optionsFunc {
return func(client *lade.Client) (*orderedmap.OrderedMap, error) {
plans, err := client.Plan.User(id)
if err != nil {
return nil, err
}
if len(plans) == 0 {
return nil, errors.New("There are no plans available")
}
options := orderedmap.New()
for _, plan := range plans {
options.Set(plan.ID, plan.ID)
}
return options, nil
}
}
func getProcessOptions(appName string) optionsFunc {
return func(client *lade.Client) (*orderedmap.OrderedMap, error) {
processes, err := client.Process.List(appName)
if err != nil {
return nil, err
}
if len(processes) == 0 {
return nil, errors.New("There are no processes available")
}
options := orderedmap.New()
for _, process := range processes {
options.Set(process.Type, process.Type)
}
return options, nil
}
}
func getRegionOptions(client *lade.Client) (*orderedmap.OrderedMap, error) {
regions, err := client.Region.List()
if err != nil {
return nil, err
}
if len(regions) == 0 {
return nil, errors.New("There are no regions available")
}
options := orderedmap.New()
for _, region := range regions {
options.Set(region.Name, region.ID)
}
return options, nil
}
func getServiceOptions(client *lade.Client) (*orderedmap.OrderedMap, error) {
services, err := client.Service.List()
if err != nil {
return nil, err
}
if len(services) == 0 {
return nil, errors.New("There are no services available")
}
options := orderedmap.New()
for _, service := range services {
options.Set(service.Title, service.Name)
}
return options, nil
}
func getVersionOptions(serviceName string) optionsFunc {
return func(client *lade.Client) (*orderedmap.OrderedMap, error) {
service, err := client.Service.Get(serviceName)
if err != nil {
return nil, err
}
if service.Repo == nil || len(service.Repo.Tags) == 0 {
return nil, errors.New("There are no versions available")
}
options := orderedmap.New()
for _, version := range service.Repo.Tags {
options.Set(version, version)
}
return options, nil
}
}
func getPageSize() int {
size, err := ts.GetSize()
if err != nil {
return 20
}
return size.Row() * 4 / 5
}
func getValueOf(val interface{}) reflect.Value {
v := reflect.ValueOf(val)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
return v
}
func isZero(val interface{}) bool {
v := getValueOf(val)
if v.Kind() == reflect.Slice {
return v.IsNil()
}
z := reflect.Zero(v.Type())
return v.Interface() == z.Interface()
}
func toString(val interface{}) interface{} {
return fmt.Sprint(getValueOf(val).Interface())
}
func printBool(val bool) string {
if val {
return "Yes"
}
return "No"
}
func printDeployLog(cancel context.CancelFunc, entry *lade.LogEntry) {
if entry.Source == "stderr" {
if entry.Line == io.EOF.Error() {
cancel()
return
}
log.Fatal(entry.Line)
}
fmt.Println(entry.Line)
}
func printLog(cancel context.CancelFunc, entry *lade.LogEntry) {
fmt.Println(entry.Line)
}
func printNameLog(width int) lade.LogHandler {
out := colorable.NewColorableStdout()
if !isatty.IsTerminal(os.Stdout.Fd()) {
ansi.DisableColors(true)
}
colors := []string{"yellow", "green", "cyan", "blue", "magenta", "red"}
names := map[string]string{}
return func(cancel context.CancelFunc, entry *lade.LogEntry) {
name, ok := names[entry.Name]
if !ok {
color := colors[len(names)%len(colors)]
name = ansi.Color(fmt.Sprintf("%-*s | ", width+2, entry.Name), color)
names[entry.Name] = name
}
fmt.Fprintln(out, name+entry.Line)
}
}
func printPrice(val float64, prec int) string {
if val == 0 {
return "Free"
}
return strconv.FormatFloat(val, 'f', prec, 64)
}
func processInfo(processes []*lade.Process) string {
results := []string{}
for _, process := range processes {
if process.Replicas == 0 {
continue
}
result := fmt.Sprintf("%s: %d/%d", process.Type, process.Count, process.Replicas)
results = append(results, result)
}
return strings.Join(results, ", ")
}
func splitEnvArg(arg string) (string, string, error) {
args := strings.SplitN(arg, "=", 2)
if len(args) < 2 || args[0] == "" || args[1] == "" {
return "", "", errors.New("Argument must be declared <key>=<val>")
}
return args[0], args[1], nil
}
func splitProcessArg(arg string) (string, string, string, error) {
args := strings.SplitN(arg, "=", 2)
if len(args) < 2 || args[0] == "" || args[1] == "" {
return "", "", "", errors.New("Argument must be declared <type>=<count>[:<plan>]")
}
spec := strings.SplitN(args[1], ":", 2)
if len(spec) < 2 {
return args[0], args[1], "", nil
}
return args[0], spec[0], spec[1], nil
}
func validateAddonName(client *lade.Client) survey.Validator {
return survey.ComposeValidators(validateName, validateUniqueName(func(name string) error {
return client.Addon.Head(name)
}))
}
func validateAppName(client *lade.Client) survey.Validator {
return survey.ComposeValidators(validateName, validateUniqueName(func(name string) error {
return client.App.Head(name)
}))
}
func validateDomainName(client *lade.Client, appName string) survey.Validator {
return survey.ComposeValidators(survey.Required, validateUniqueName(func(name string) error {
return client.Domain.Head(appName, name)
}))
}
func validateCount(min, max int) func(interface{}) error {
return func(val interface{}) error {
num, err := strconv.Atoi(val.(string))
if err != nil {
return errors.New("Count must be a number")
}
if num < min {
return fmt.Errorf("Count must be at least %d", min)
}
if num > max {
return fmt.Errorf("Count must be at most %d", max)
}
return nil
}
}
func validateEnvName(val interface{}) error {
if !validEnvName.MatchString(val.(string)) {
return errors.New("Name must only contain A-Z, 0-9, dash (-) or underscore (_)")
}
return nil
}
func validateName(val interface{}) error {
if !validName.MatchString(val.(string)) {
return errors.New("Name must start with a-z followed by a-z, 0-9, dash (-) or underscore (_)")
}
return nil
}
func validateUniqueName(fn func(string) error) survey.Validator {
return func(val interface{}) error {
err := fn(val.(string))
if err == nil {
return errors.New("Name is already taken")
}
var e *lade.APIError
if !errors.As(err, &e) || e.Status != http.StatusNotFound {
return err
}
return nil
}
}