diff --git a/src/cmd/cli/command/commands.go b/src/cmd/cli/command/commands.go index c8865275..5bfc8165 100644 --- a/src/cmd/cli/command/commands.go +++ b/src/cmd/cli/command/commands.go @@ -434,7 +434,7 @@ var generateCmd = &cobra.Command{ if sample == "" { return errors.New("cannot run in non-interactive mode") } - return cli.InitFromSample(cmd.Context(), sample) + return cli.InitFromSamples(cmd.Context(), []string{sample}) } if sample == "" { @@ -550,7 +550,7 @@ Generate will write files in the current folder. You can edit them and then depl if sample != "" { term.Info("Fetching sample from the Defang repository...") - err := cli.InitFromSample(cmd.Context(), sample) + err := cli.InitFromSamples(cmd.Context(), []string{sample}) if err != nil { return err } diff --git a/src/pkg/cli/init.go b/src/pkg/cli/init.go index 3da94e7b..ca3b5e9a 100644 --- a/src/pkg/cli/init.go +++ b/src/pkg/cli/init.go @@ -45,11 +45,10 @@ func FetchSamples(ctx context.Context) ([]Sample, error) { return samples, err } -func InitFromSample(ctx context.Context, name string) error { +func InitFromSamples(ctx context.Context, names []string) error { const repo = "samples" const branch = "main" - prefix := fmt.Sprintf("%s-%s/samples/%s/", repo, branch, name) resp, err := http.GetWithContext(ctx, "https://github.com/DefangLabs/"+repo+"/archive/refs/heads/"+branch+".tar.gz") if err != nil { return err @@ -72,16 +71,19 @@ func InitFromSample(ctx context.Context, name string) error { return err } - if base, ok := strings.CutPrefix(h.Name, prefix); ok && len(base) > 0 { - fmt.Println(" -", base) - if h.FileInfo().IsDir() { - if err := os.MkdirAll(base, 0755); err != nil { + for _, name := range names { + prefix := fmt.Sprintf("%s-%s/samples/%s/", repo, branch, name) + if base, ok := strings.CutPrefix(h.Name, prefix); ok && len(base) > 0 { + fmt.Println(" -", base) + if h.FileInfo().IsDir() { + if err := os.MkdirAll(base, 0755); err != nil { + return err + } + continue + } + if err := createFile(base, h, tarReader); err != nil { return err } - continue - } - if err := createFile(base, h, tarReader); err != nil { - return err } } }