Skip to content

Commit

Permalink
Merge pull request #513 from DefangLabs/edw-load-sample-create-dir
Browse files Browse the repository at this point in the history
Create dir and pass to generate with AI instead of Chdir
  • Loading branch information
lionello authored Jun 28, 2024
2 parents 5cafb4f + 73e5b61 commit f51e89a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
22 changes: 8 additions & 14 deletions src/cmd/cli/command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,30 +541,20 @@ Generate will write files in the current folder. You can edit them and then depl

Track("Generate Started", P{"language", language}, P{"sample", sample}, P{"description", prompt.Description}, P{"folder", prompt.Folder})

// create the folder if needed
cd := ""
if prompt.Folder != "." {
cd = "`cd " + prompt.Folder + "` and "
os.MkdirAll(prompt.Folder, 0755)
if err := os.Chdir(prompt.Folder); err != nil {
return err
}
}

// Check if the current folder is empty
if empty, err := pkg.IsDirEmpty("."); !empty || err != nil {
term.Warn("The folder is not empty. We recommend running this command in an empty folder.")
if empty, err := pkg.IsDirEmpty(prompt.Folder); !os.IsNotExist(err) && !empty {
term.Warnf("The folder %q is not empty. We recommend running this command in an empty folder.", prompt.Folder)
}

if sample != "" {
term.Info("Fetching sample from the Defang repository...")
err := cli.InitFromSamples(cmd.Context(), "", []string{sample})
err := cli.InitFromSamples(cmd.Context(), prompt.Folder, []string{sample})
if err != nil {
return err
}
} else {
term.Info("Working on it. This may take 1 or 2 minutes...")
_, err := cli.GenerateWithAI(cmd.Context(), client, language, prompt.Description)
_, err := cli.GenerateWithAI(cmd.Context(), client, language, prompt.Folder, prompt.Description)
if err != nil {
return err
}
Expand All @@ -579,6 +569,10 @@ Generate will write files in the current folder. You can edit them and then depl
term.Debug("unable to launch VS Code:", err)
}

cd := ""
if prompt.Folder != "." {
cd = "`cd " + prompt.Folder + "` and "
}
printDefangHint("Check the files in your favorite editor.\nTo deploy the service, "+cd+"do:", "compose up")
return nil
},
Expand Down
8 changes: 6 additions & 2 deletions src/pkg/cli/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"context"
"fmt"
"os"
"path/filepath"

"github.com/DefangLabs/defang/src/pkg/cli/client"
"github.com/DefangLabs/defang/src/pkg/term"
defangv1 "github.com/DefangLabs/defang/src/protos/io/defang/v1"
)

func GenerateWithAI(ctx context.Context, client client.Client, language string, description string) ([]string, error) {
func GenerateWithAI(ctx context.Context, client client.Client, language, dir, description string) ([]string, error) {
if DoDryRun {
term.Warn("Dry run, not generating files")
return nil, ErrDryRun
Expand Down Expand Up @@ -38,11 +39,14 @@ func GenerateWithAI(ctx context.Context, client client.Client, language string,

// Write each file to disk
term.Info("Writing files to disk...")
if err := os.MkdirAll(dir, 0755); err != nil {
return nil, err
}
for _, file := range response.Files {
// Print the files that were generated
fmt.Println(" -", file.Name)
// TODO: this will overwrite existing files
if err = os.WriteFile(file.Name, []byte(file.Content), 0644); err != nil {
if err = os.WriteFile(filepath.Join(dir, file.Name), []byte(file.Content), 0644); err != nil {
return nil, err
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/pkg/cli/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Sample struct {
ShortDescription string `json:"shortDescription"`
Tags []string `json:"tags"`
Languages []string `json:"languages"`
Configs []string `json:"configs"`
}

func FetchSamples(ctx context.Context) ([]Sample, error) {
Expand Down Expand Up @@ -79,13 +80,13 @@ func InitFromSamples(ctx context.Context, dir string, names []string) error {
}

for _, name := range names {
// Create a subdirectory for each sample when there is more than one sample requested
// Create the sample directory or subdirectory for each sample when there is more than one sample requested
subdir := ""
if len(names) > 1 {
subdir = name
if err := os.MkdirAll(filepath.Join(dir, subdir), 0755); err != nil {
return err
}
}
if err := os.MkdirAll(filepath.Join(dir, subdir), 0755); err != nil {
return err
}
prefix := fmt.Sprintf("%s-%s/samples/%s/", repo, branch, name)
if base, ok := strings.CutPrefix(h.Name, prefix); ok && len(base) > 0 {
Expand Down

0 comments on commit f51e89a

Please sign in to comment.