-
Notifications
You must be signed in to change notification settings - Fork 224
/
input.go
190 lines (152 loc) · 3.89 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
package cmd
import (
"fmt"
"sort"
"strconv"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/superfly/flyctl/api"
)
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 confirmOverwrite(filename string) bool {
confirm := false
prompt := &survey.Confirm{
Message: fmt.Sprintf(`Overwrite "%s"?`, filename),
}
err := survey.AskOne(prompt, &confirm)
checkErr(err)
return confirm
}
func selectOrganization(client *api.Client, slug string, typeFilter *api.OrganizationType) (*api.Organization, error) {
orgs, err := client.GetOrganizations(typeFilter)
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 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
}