@@ -424,16 +424,47 @@ var generateCmd = &cobra.Command{
424
424
return errors .New ("cannot run in non-interactive mode" )
425
425
}
426
426
427
+ var language string
428
+ if err := survey .AskOne (& survey.Select {
429
+ Message : "Choose the language you'd like to use:" ,
430
+ Options : []string {"Nodejs" , "Golang" , "Python" },
431
+ Default : "Nodejs" ,
432
+ Help : "The project code will be in the language you choose here." ,
433
+ }, & language ); err != nil {
434
+ return err
435
+ }
436
+
437
+ var category , sample string
438
+
439
+ // Fetch the list of samples from the Defang repository
440
+ if samples , err := cli .FetchSamples (cmd .Context ()); err != nil {
441
+ term .Debug (" - unable to fetch samples:" , err )
442
+ } else if len (samples ) > 0 {
443
+ const generateWithAI = "Generate with AI"
444
+
445
+ category = strings .ToLower (language )
446
+ sampleNames := []string {generateWithAI }
447
+ // sampleDescriptions := []string{"Generate a sample from scratch using a language prompt"}
448
+ for _ , sample := range samples {
449
+ if sample .Category == category {
450
+ sampleNames = append (sampleNames , sample .Name )
451
+ // sampleDescriptions = append(sampleDescriptions, sample.Readme)
452
+ }
453
+ }
454
+
455
+ if err := survey .AskOne (& survey.Select {
456
+ Message : "Choose a sample service:" ,
457
+ Options : sampleNames ,
458
+ Help : "The project code will be based on the sample you choose here." ,
459
+ }, & sample ); err != nil {
460
+ return err
461
+ }
462
+ if sample == generateWithAI {
463
+ sample = ""
464
+ }
465
+ }
466
+
427
467
var qs = []* survey.Question {
428
- {
429
- Name : "language" ,
430
- Prompt : & survey.Select {
431
- Message : "Choose the language you'd like to use:" ,
432
- Options : []string {"Nodejs" , "Golang" , "Python" },
433
- Default : "Nodejs" ,
434
- Help : "The generated code will be in the language you choose here." ,
435
- },
436
- },
437
468
{
438
469
Name : "description" ,
439
470
Prompt : & survey.Input {
@@ -457,13 +488,16 @@ Generate will write files in the current folder. You can edit them and then depl
457
488
},
458
489
}
459
490
491
+ if sample != "" {
492
+ qs = qs [1 :] // user picked a sample, so we skip the description question
493
+ }
494
+
460
495
prompt := struct {
461
- Language string // or you can tag fields to match a specific name
462
- Description string
496
+ Description string // or you can tag fields to match a specific name
463
497
Folder string
464
498
}{}
465
499
466
- // ask the questions
500
+ // ask the remaining questions
467
501
err := survey .Ask (qs , & prompt )
468
502
if err != nil {
469
503
return err
@@ -479,7 +513,7 @@ Generate will write files in the current folder. You can edit them and then depl
479
513
}
480
514
}
481
515
482
- Track ("Generate Started" , P {"language" , prompt . Language }, P {"description" , prompt .Description }, P {"folder" , prompt .Folder })
516
+ Track ("Generate Started" , P {"language" , language }, P { "sample" , sample }, P {"description" , prompt .Description }, P {"folder" , prompt .Folder })
483
517
484
518
// create the folder if needed
485
519
cd := ""
@@ -496,10 +530,18 @@ Generate will write files in the current folder. You can edit them and then depl
496
530
term .Warn (" ! The folder is not empty. Files may be overwritten. Press Ctrl+C to abort." )
497
531
}
498
532
499
- term .Info (" * Working on it. This may take 1 or 2 minutes..." )
500
- _ , err = cli .Generate (cmd .Context (), client , prompt .Language , prompt .Description )
501
- if err != nil {
502
- return err
533
+ if prompt .Description != "" {
534
+ term .Info (" * Working on it. This may take 1 or 2 minutes..." )
535
+ _ , err := cli .GenerateWithAI (cmd .Context (), client , language , prompt .Description )
536
+ if err != nil {
537
+ return err
538
+ }
539
+ } else {
540
+ term .Info (" * Fetching sample from the Defang repository..." )
541
+ err := cli .InitFromSample (cmd .Context (), category , sample )
542
+ if err != nil {
543
+ return err
544
+ }
503
545
}
504
546
505
547
term .Info (" * Code generated successfully in folder" , prompt .Folder )
0 commit comments