-
Notifications
You must be signed in to change notification settings - Fork 151
/
prompt.go
249 lines (217 loc) · 6.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
package init
import (
"context"
"fmt"
"github.com/fatih/color"
"github.com/scaleway/scaleway-cli/v2/internal/core"
"github.com/scaleway/scaleway-cli/v2/internal/interactive"
"github.com/scaleway/scaleway-cli/v2/internal/terminal"
"github.com/scaleway/scaleway-sdk-go/api/account/v3"
"github.com/scaleway/scaleway-sdk-go/logger"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/scaleway-sdk-go/validation"
)
func promptOrganizationID(ctx context.Context) (string, error) {
_, _ = interactive.Println()
return interactive.PromptStringWithConfig(&interactive.PromptStringConfig{
Ctx: ctx,
Prompt: "Choose your default organization ID",
ValidateFunc: func(s string) error {
if !validation.IsUUID(s) {
return fmt.Errorf("organization id is not a valid uuid")
}
return nil
},
})
}
func promptManualProjectID(ctx context.Context, defaultProjectID string) (string, error) {
_, _ = interactive.Println()
return interactive.PromptStringWithConfig(&interactive.PromptStringConfig{
Ctx: ctx,
Prompt: "Choose your default project ID",
DefaultValue: defaultProjectID,
DefaultValueDoc: defaultProjectID,
ValidateFunc: func(s string) error {
if !validation.IsProjectID(s) {
return fmt.Errorf("organization id is not a valid uuid")
}
return nil
},
})
}
func promptProjectID(ctx context.Context, accessKey string, secretKey string, organizationID string, defaultProjectID string) (string, error) {
if defaultProjectID == "" {
defaultProjectID = organizationID
}
if !interactive.IsInteractive {
return defaultProjectID, nil
}
client := core.ExtractClient(ctx)
api := account.NewProjectAPI(client)
res, err := api.ListProjects(&account.ProjectAPIListProjectsRequest{
OrganizationID: organizationID,
}, scw.WithAllPages(), scw.WithContext(ctx), scw.WithAuthRequest(accessKey, secretKey))
if err != nil {
return "", fmt.Errorf("failed to list projects: %w", err)
}
if len(res.Projects) == 0 {
return promptManualProjectID(ctx, defaultProjectID)
}
defaultIndex := 0
projects := make([]string, len(res.Projects))
for i := range res.Projects {
if res.Projects[i].ID == defaultProjectID {
defaultIndex = i
}
projects[i] = fmt.Sprintf("%s (%s)", res.Projects[i].Name, res.Projects[i].ID)
}
prompt := interactive.ListPrompt{
Prompt: "Choose your default project ID",
Choices: projects,
DefaultIndex: defaultIndex,
}
_, _ = interactive.Println()
index, err := prompt.Execute(ctx)
if err != nil {
return "", err
}
return res.Projects[index].ID, nil
}
func promptTelemetry(ctx context.Context) (*bool, error) {
_, _ = interactive.Println()
_, _ = interactive.PrintlnWithoutIndent(`
To improve this tool we rely on diagnostic and usage data.
Sending such data is optional and can be disabled at any time by running "scw config set send-telemetry=false".
`)
sendTelemetry, err := interactive.PromptBoolWithConfig(&interactive.PromptBoolConfig{
Prompt: "Do you want to send usage statistics and diagnostics?",
DefaultValue: true,
Ctx: ctx,
})
if err != nil {
return nil, err
}
return scw.BoolPtr(sendTelemetry), nil
}
func promptAutocomplete(ctx context.Context) (*bool, error) {
_, _ = interactive.Println()
_, _ = interactive.PrintlnWithoutIndent(`
To fully enjoy Scaleway CLI we recommend you install autocomplete support in your shell.
`)
installAutocomplete, err := interactive.PromptBoolWithConfig(&interactive.PromptBoolConfig{
Ctx: ctx,
Prompt: "Do you want to install autocomplete?",
DefaultValue: true,
})
if err != nil {
return nil, err
}
return scw.BoolPtr(installAutocomplete), nil
}
func promptSecretKey(ctx context.Context) (string, error) {
_, _ = interactive.Println()
secret, err := interactive.Readline(&interactive.ReadlineConfig{
Ctx: ctx,
PromptFunc: func(value string) string {
secretKey := "secret-key"
switch {
case validation.IsUUID(value):
secretKey = terminal.Style(secretKey, color.FgBlue)
}
return terminal.Style(fmt.Sprintf("Enter a valid %s: ", secretKey), color.Bold)
},
ValidateFunc: func(s string) error {
if validation.IsSecretKey(s) {
return nil
}
return fmt.Errorf("invalid secret-key")
},
})
if err != nil {
return "", err
}
switch {
case validation.IsUUID(secret):
return secret, nil
default:
return "", fmt.Errorf("invalid secret-key: '%v'", secret)
}
}
func promptAccessKey(ctx context.Context) (string, error) {
_, _ = interactive.Println()
key, err := interactive.Readline(&interactive.ReadlineConfig{
Ctx: ctx,
PromptFunc: func(value string) string {
accessKey := "access-key"
switch {
case validation.IsAccessKey(value):
accessKey = terminal.Style(accessKey, color.FgBlue)
}
return terminal.Style(fmt.Sprintf("Enter a valid %s: ", accessKey), color.Bold)
},
ValidateFunc: func(s string) error {
if !validation.IsAccessKey(s) {
return fmt.Errorf("invalid access-key")
}
return nil
},
})
if err != nil {
return "", err
}
switch {
case validation.IsAccessKey(key):
return key, nil
default:
return "", fmt.Errorf("invalid access-key: '%v'", key)
}
}
func promptDefaultZone(ctx context.Context) (scw.Zone, error) {
_, _ = interactive.Println()
zone, err := interactive.PromptStringWithConfig(&interactive.PromptStringConfig{
Ctx: ctx,
Prompt: "Select a zone",
DefaultValueDoc: "fr-par-1",
DefaultValue: "fr-par-1",
ValidateFunc: func(s string) error {
logger.Debugf("s: %v", s)
if !validation.IsZone(s) {
return fmt.Errorf("invalid zone")
}
return nil
},
})
if err != nil {
return "", err
}
return scw.ParseZone(zone)
}
// promptProfileOverride prompt user if profileName is getting override in config
func promptProfileOverride(ctx context.Context, config *scw.Config, configPath string, profileName string) error {
var profile *scw.Profile
var profileExists bool
if profileName == scw.DefaultProfileName {
profile = &config.Profile
profileExists = true
} else {
profile, profileExists = config.Profiles[profileName]
}
if !config.IsEmpty() && profileExists {
_, _ = interactive.PrintlnWithoutIndent(`
Current config is located at ` + configPath + `
` + terminal.Style(fmt.Sprint(profile), color.Faint) + `
`)
overrideConfig, err := interactive.PromptBoolWithConfig(&interactive.PromptBoolConfig{
Prompt: fmt.Sprintf("Do you want to override the current profile (%s) ?", profileName),
DefaultValue: true,
Ctx: ctx,
})
if err != nil {
return err
}
if !overrideConfig {
return fmt.Errorf("initialization canceled")
}
}
return nil
}