Skip to content

Commit

Permalink
Allow InitFromSamples to load multiple samples
Browse files Browse the repository at this point in the history
  • Loading branch information
edw-defang committed Jun 20, 2024
1 parent 0a0b0e3 commit 5749b95
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/cmd/cli/command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down Expand Up @@ -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
}
Expand Down
22 changes: 12 additions & 10 deletions src/pkg/cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}
}
Expand Down

0 comments on commit 5749b95

Please sign in to comment.