Skip to content

Commit

Permalink
made sure an error is returned before prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
Chrisyhjiang committed Jun 27, 2024
1 parent 74f5c34 commit a57cd49
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/cmd/cli/command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ var generateCmd = &cobra.Command{
}
return cli.InitFromSamples(cmd.Context(), "", []string{sample})
}

sampleList, cachedErr := cli.FetchSamples(cmd.Context())
if sample == "" {
if err := survey.AskOne(&survey.Select{
Message: "Choose the language you'd like to use:",
Expand All @@ -448,17 +448,16 @@ var generateCmd = &cobra.Command{
}, &language); err != nil {
return err
}

// Fetch the list of samples from the Defang repository
if samples, err := cli.FetchSamples(cmd.Context()); err != nil {
term.Debug("unable to fetch samples:", err)
} else if len(samples) > 0 {
if cachedErr != nil {
term.Debug("unable to fetch samples:", cachedErr)
} else if len(sampleList) > 0 {
const generateWithAI = "Generate with AI"

lang := strings.ToLower(language)
sampleNames := []string{generateWithAI}
sampleDescriptions := []string{"Generate a sample from scratch using a language prompt"}
for _, sample := range samples {
for _, sample := range sampleList {
if slices.Contains(sample.Languages, lang) {
sampleNames = append(sampleNames, sample.Name)
sampleDescriptions = append(sampleDescriptions, sample.ShortDescription)
Expand Down Expand Up @@ -510,6 +509,17 @@ Generate will write files in the current folder. You can edit them and then depl

if sample != "" {
qs = qs[1:] // user picked a sample, so we skip the description question
sampleExists := false
for _, s := range sampleList {
if s.Name == sample {
sampleExists = true
break
}
}

if !sampleExists {
return errors.New("sample not found")
}
}

prompt := struct {
Expand Down

0 comments on commit a57cd49

Please sign in to comment.