Skip to content

Commit

Permalink
Merge pull request #512 from Chrisyhjiang/483
Browse files Browse the repository at this point in the history
Nonexistent project error fix
  • Loading branch information
lionello authored Jun 28, 2024
2 parents 060f18a + 1384b42 commit 5cafb4f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/cmd/cli/command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ func Execute(ctx context.Context) error {
if code == connect.CodeFailedPrecondition && (strings.Contains(err.Error(), "EULA") || strings.Contains(err.Error(), "terms")) {
printDefangHint("Please use the following command to see the Defang terms of service:", "terms")
}

return ExitCode(code)
}

Expand Down Expand Up @@ -438,7 +437,7 @@ var generateCmd = &cobra.Command{
}
return cli.InitFromSamples(cmd.Context(), "", []string{sample})
}

sampleList, fetchSamplesErr := 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 +447,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 fetchSamplesErr != nil {
term.Debug("unable to fetch samples:", fetchSamplesErr)
} 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 +508,13 @@ 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 := slices.ContainsFunc(sampleList, func(s cli.Sample) bool {
return s.Name == sample
})

if !sampleExists {
return cli.ErrSampleNotFound
}
}

prompt := struct {
Expand Down
10 changes: 10 additions & 0 deletions src/pkg/cli/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"compress/gzip"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
Expand All @@ -15,6 +16,8 @@ import (
"github.com/DefangLabs/defang/src/pkg/term"
)

var ErrSampleNotFound = errors.New("sample not found")

type Sample struct {
Name string `json:"name"`
Title string `json:"title"`
Expand Down Expand Up @@ -63,6 +66,9 @@ func InitFromSamples(ctx context.Context, dir string, names []string) error {
defer tarball.Close()
tarReader := tar.NewReader(tarball)
term.Info("Writing files to disk...")

sampleFound := false

for {
h, err := tarReader.Next()
if err != nil {
Expand All @@ -83,6 +89,7 @@ func InitFromSamples(ctx context.Context, dir string, names []string) error {
}
prefix := fmt.Sprintf("%s-%s/samples/%s/", repo, branch, name)
if base, ok := strings.CutPrefix(h.Name, prefix); ok && len(base) > 0 {
sampleFound = true
fmt.Println(" -", base)
path := filepath.Join(dir, subdir, base)
if h.FileInfo().IsDir() {
Expand All @@ -97,6 +104,9 @@ func InitFromSamples(ctx context.Context, dir string, names []string) error {
}
}
}
if !sampleFound {
return ErrSampleNotFound
}
return nil
}

Expand Down
17 changes: 17 additions & 0 deletions src/pkg/cli/new_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cli

import (
"context"
"errors"
"testing"
)

func TestInitFromSamples(t *testing.T) {
err := InitFromSamples(context.Background(), t.TempDir(), []string{"nonexisting"})
if err == nil {
t.Fatal("Expected test to fail")
}
if !errors.Is(err, ErrSampleNotFound) {
t.Errorf("Expected error to be %v, got %v", ErrSampleNotFound, err)
}
}

0 comments on commit 5cafb4f

Please sign in to comment.