@@ -2,11 +2,9 @@ package command
2
2
3
3
import (
4
4
"context"
5
- "fmt"
6
5
"os"
7
6
"path/filepath"
8
7
9
- "github.com/AlecAivazis/survey/v2"
10
8
"github.com/pkg/errors"
11
9
"github.com/spf13/cobra"
12
10
"go.jetpack.io/launchpad/goutil/errorutil"
@@ -16,23 +14,6 @@ import (
16
14
"go.jetpack.io/launchpad/pkg/kubevalidate"
17
15
)
18
16
19
- type serviceTypeOption string
20
-
21
- const (
22
- webServiceType serviceTypeOption = "Web Service"
23
- cronjobServiceType serviceTypeOption = "Cron Job"
24
- jetpackManagedCluster string = "Jetpack managed cluster"
25
- createJetpackCluster string = "Create a new cluster with Jetpack"
26
- )
27
-
28
- type SurveyAnswers struct {
29
- AppName string
30
- AppType string
31
- ClusterOption string
32
- KubeContext string
33
- ImageRepositoryLocation string
34
- }
35
-
36
17
func initCmd () * cobra.Command {
37
18
var initCmd = & cobra.Command {
38
19
Use : "init [path]" ,
@@ -44,7 +25,6 @@ func initCmd() *cobra.Command {
44
25
if err != nil {
45
26
return errors .WithStack (err )
46
27
}
47
-
48
28
return initConfig (cmd .Context (), authProvider , path )
49
29
},
50
30
}
@@ -102,15 +82,15 @@ func initConfig(ctx context.Context, authProvider provider.Auth, path string) er
102
82
return errors .WithStack (err )
103
83
}
104
84
105
- answers , err := runConfigSurvey (ctx , authProvider , appName )
85
+ answers , err := cmdOpts . InitSurveyProvider (). Run (ctx , authProvider , appName )
106
86
if err != nil {
107
87
return errors .WithStack (err )
108
88
}
109
89
if answers == nil {
110
90
return nil
111
91
}
112
92
113
- if answers .ClusterOption == createJetpackCluster {
93
+ if answers .ClusterOption == provider . CreateJetpackCluster {
114
94
// Ask users to create a cluster first.
115
95
jetlog .Logger (ctx ).Printf ("\n To create a new cluster, run `launchpad cluster create <cluster_name>`.\n " )
116
96
return nil
@@ -122,17 +102,17 @@ func initConfig(ctx context.Context, authProvider provider.Auth, path string) er
122
102
Services : []jetconfig.Service {},
123
103
}
124
104
125
- if answers .AppType == string (webServiceType ) {
105
+ if answers .AppType == string (provider . WebServiceType ) {
126
106
jetCfg .AddNewWebService (answers .AppName + "-" + jetconfig .WebType )
127
- } else if answers .AppType == string (cronjobServiceType ) {
107
+ } else if answers .AppType == string (provider . CronjobServiceType ) {
128
108
jetCfg .AddNewCronService (
129
109
answers .AppName + "-" + jetconfig .CronType ,
130
110
[]string {"/bin/sh" , "-c" , "date; echo Hello from Launchpad" },
131
111
"* * * * *" ,
132
112
)
133
113
}
134
114
135
- if answers .ClusterOption != "" && answers .ClusterOption != createJetpackCluster {
115
+ if answers .ClusterOption != "" && answers .ClusterOption != provider . CreateJetpackCluster {
136
116
jetCfg .Cluster = answers .ClusterOption
137
117
}
138
118
if answers .ImageRepositoryLocation != "" {
@@ -156,173 +136,6 @@ func initConfig(ctx context.Context, authProvider provider.Auth, path string) er
156
136
return nil
157
137
}
158
138
159
- func runConfigSurvey (
160
- ctx context.Context ,
161
- authProvider provider.Auth ,
162
- appName string ,
163
- ) (* SurveyAnswers , error ) {
164
-
165
- clusters , err := cmdOpts .ClusterProvider ().GetAll (ctx )
166
- if err != nil {
167
- return nil , errors .WithStack (err )
168
- }
169
-
170
- clusterNames := []string {}
171
- for _ , c := range clusters {
172
- clusterNames = append (clusterNames , c .GetName ())
173
- }
174
- // In case user wants to log in and use a jetpack managed cluster.
175
- clusterNames = append (clusterNames , jetpackManagedCluster )
176
- // In case user wants to create a cluster.
177
- clusterNames = append (clusterNames , createJetpackCluster )
178
-
179
- qs , err := surveyQuestions (ctx , appName , clusterNames )
180
- if err != nil {
181
- return nil , errors .WithStack (err )
182
- }
183
-
184
- answers := & SurveyAnswers {}
185
-
186
- err = survey .Ask ([]* survey.Question {qs ["AppName" ]}, & answers .AppName )
187
- if err != nil {
188
- return nil , errors .WithStack (err )
189
- }
190
-
191
- err = survey .Ask ([]* survey.Question {qs ["AppType" ]}, & answers .AppType )
192
- if err != nil {
193
- return nil , errors .WithStack (err )
194
- }
195
-
196
- err = survey .Ask ([]* survey.Question {qs ["ClusterOption" ]}, & answers .ClusterOption )
197
- if err != nil {
198
- return nil , errors .WithStack (err )
199
- }
200
-
201
- if answers .ClusterOption == jetpackManagedCluster {
202
- // Prompt users to log in.
203
- ctx , err := authProvider .Identify (ctx )
204
- if err != nil {
205
- return nil , errors .WithStack (err )
206
- }
207
- // Get all the cluster names again.
208
- clusters , err := cmdOpts .ClusterProvider ().GetAll (ctx )
209
- if err != nil {
210
- return nil , errors .WithStack (err )
211
- }
212
-
213
- // Only show the jetpack managed cluster names.
214
- clusterNames := []string {}
215
- for _ , c := range clusters {
216
- if c .IsJetpackManaged () {
217
- clusterNames = append (clusterNames , c .GetName ())
218
- }
219
- }
220
- if len (clusterNames ) == 0 {
221
- answers .ClusterOption = createJetpackCluster
222
- } else {
223
- // Add option to select creating a new managed cluster.
224
- clusterNames = append (clusterNames , createJetpackCluster )
225
- additionalClusterSurvey := surveyJetpackManagedClusterQuestions (ctx , clusterNames )
226
- err = survey .Ask ([]* survey.Question {additionalClusterSurvey ["ClusterOption" ]}, & answers .ClusterOption )
227
- if err != nil {
228
- return nil , errors .WithStack (err )
229
- }
230
- }
231
- }
232
-
233
- return answers , nil
234
- }
235
-
236
- func surveyQuestions (ctx context.Context , appName string , clusterNames []string ) (map [string ]* survey.Question , error ) {
237
-
238
- appTypes := jetconfig .GetServiceTypes ()
239
- appTypeOptions := getAppTypeOptions (appTypes )
240
-
241
- questions := map [string ]* survey.Question {
242
- "AppName" : {
243
- Name : "AppName" ,
244
- Prompt : & survey.Input {
245
- Message : "What is the name of this project?" ,
246
- Default : appName ,
247
- },
248
- Validate : func (val any ) error {
249
- if err := survey .MinLength (3 )(val ); err != nil {
250
- return err
251
- }
252
-
253
- nameEntered := val .(string )
254
- if ok := kubevalidate .IsValidName (nameEntered ); ! ok {
255
- // NOTE: we can create an API `kubevalidate.IsValidNameWithReasons` that returns
256
- // the error messages from k8s.io/apimachinery/pkg/util/validation
257
- // However, those messages speak about DNS RFC 1035, which may be confusing
258
- // to our users.
259
-
260
- // The default error text by the Survey lib is prefixed by:
261
- // X Sorry, your reply was invalid:
262
- // to which we are adding a suffix of the nameEntered via the %s below.
263
- // This gives feedback to the user that we actually processed their entered name.
264
- msg := "%s\n " +
265
- "For compatibility with kubernetes, we require app names to be:\n " +
266
- " - less than 64 characters\n " +
267
- " - consist of lower case alphanumeric characters or '-'\n " +
268
- " - start with an alphabetic character \n " +
269
- " - end with an alphanumeric character"
270
- return fmt .Errorf (msg , nameEntered )
271
- }
272
- return nil
273
- },
274
- },
275
- "AppType" : {
276
- Name : "AppType" ,
277
- Prompt : & survey.Select {
278
- Message : "What type of service you would like to add to this project?" ,
279
- Options : appTypeOptions ,
280
- },
281
- },
282
- "ClusterOption" : {
283
- Name : "ClusterOption" ,
284
- Prompt : & survey.Select {
285
- Message : "To which cluster do you want to deploy this project?" ,
286
- Options : clusterNames ,
287
- },
288
- },
289
- "ImageRepositoryLocation" : {
290
- Name : "ImageRepositoryLocation" ,
291
- Prompt : & survey.Input {
292
- Message : imageRepositoryFlagHelpMsg ,
293
- },
294
- Validate : survey .Required ,
295
- },
296
- }
297
-
298
- return questions , nil
299
- }
300
-
301
- func surveyJetpackManagedClusterQuestions (ctx context.Context , clusterNames []string ) map [string ]* survey.Question {
302
- return map [string ]* survey.Question {
303
- "ClusterOption" : {
304
- Name : "ClusterOption" ,
305
- Prompt : & survey.Select {
306
- Message : "To which jetpack managed cluster do you want to deploy this project?" ,
307
- Options : clusterNames ,
308
- },
309
- },
310
- }
311
- }
312
-
313
139
func appName (path string ) (string , error ) {
314
140
return kubevalidate .ToValidName (filepath .Base (jetconfig .ConfigDir (path )))
315
141
}
316
-
317
- func getAppTypeOptions (appTypes []string ) []string {
318
- var appTypeOptions []string
319
- for _ , appType := range appTypes {
320
- switch appType {
321
- case jetconfig .WebType :
322
- appTypeOptions = append (appTypeOptions , string (webServiceType ))
323
- case jetconfig .CronType :
324
- appTypeOptions = append (appTypeOptions , string (cronjobServiceType ))
325
- }
326
- }
327
- return appTypeOptions
328
- }
0 commit comments