-
Notifications
You must be signed in to change notification settings - Fork 240
/
input.go
230 lines (183 loc) · 4.78 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
package cmd
import (
"context"
"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(ctx context.Context, client *api.Client, slug string, typeFilter *api.OrganizationType) (*api.Organization, error) {
orgs, err := client.GetOrganizations(ctx, 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 selectWireGuardPeer(ctx context.Context, client *api.Client, slug string) (string, error) {
peers, err := client.GetWireGuardPeers(ctx, slug)
if err != nil {
return "", err
}
if len(peers) < 1 {
return "", fmt.Errorf(`Organization "%s" does not have any wireguard peers`, slug)
}
var options []string
for _, peer := range peers {
options = append(options, peer.Name)
}
selectedPeer := 0
prompt := &survey.Select{
Message: "Select peer:",
Options: options,
PageSize: 30,
}
if err := survey.AskOne(prompt, &selectedPeer); err != nil {
return "", err
}
return peers[selectedPeer].Name, nil
}
func selectRegion(ctx context.Context, client *api.Client, regionCode string) (*api.Region, error) {
regions, requestRegion, err := client.PlatformRegions(ctx)
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(ctx context.Context, client *api.Client, vmSizeName string) (*api.VMSize, error) {
vmSizes, err := client.PlatformVMSizes(ctx)
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, autoGenerate bool) (name string, err error) {
message := "App Name"
if autoGenerate {
message += " (leave blank to use an auto-generated name)"
}
message += ":"
prompt := &survey.Input{
Message: message,
Default: defaultName,
}
if err := survey.AskOne(prompt, &name); err != nil {
return name, err
}
return name, nil
}
func initialClusterSizeInput(defaultVal int) (int, error) {
var count int
prompt := &survey.Input{
Message: "Specify the initial cluster size:",
Default: strconv.Itoa(defaultVal),
}
if err := survey.AskOne(prompt, &count); err != nil {
return 0, err
}
return count, nil
}
func volumeSizeInput(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
}