Skip to content

Commit

Permalink
Merge pull request #485 from DefangLabs/edw-sample-close-file
Browse files Browse the repository at this point in the history
Make sure file is closed and allow init multiple samples
  • Loading branch information
lionello committed Jun 20, 2024
2 parents 00dd8e5 + 76d6692 commit 640ee0e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 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 @@ -435,7 +435,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 @@ -552,7 +552,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
48 changes: 30 additions & 18 deletions src/pkg/cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/DefangLabs/defang/src/pkg/http"
Expand Down Expand Up @@ -45,23 +46,22 @@ func FetchSamples(ctx context.Context) ([]Sample, error) {
return samples, err
}

func InitFromSample(ctx context.Context, name string) error {
func InitFromSamples(ctx context.Context, dir string, 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
}
defer resp.Body.Close()
term.Debug(resp.Header)
body, err := gzip.NewReader(resp.Body)
tarball, err := gzip.NewReader(resp.Body)
if err != nil {
return err
}
defer body.Close()
tarReader := tar.NewReader(body)
defer tarball.Close()
tarReader := tar.NewReader(tarball)
term.Info("Writing files to disk...")
for {
h, err := tarReader.Next()
Expand All @@ -72,23 +72,35 @@ 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)
path := filepath.Join(dir, base)
if h.FileInfo().IsDir() {
if err := os.MkdirAll(path, 0755); err != nil {
return err
}
continue
}
if err := createFile(path, h, tarReader); err != nil {
return err
}
continue
}
// Like os.Create, but with the same mode as the original file (so scripts are executable, etc.)
file, err := os.OpenFile(base, os.O_RDWR|os.O_CREATE|os.O_EXCL, h.FileInfo().Mode())
if err != nil {
return err
}
if _, err := io.Copy(file, tarReader); err != nil {
return err
}
}
}
return nil
}

func createFile(base string, h *tar.Header, tarReader *tar.Reader) error {
// Like os.Create, but with the same mode as the original file (so scripts are executable, etc.)
file, err := os.OpenFile(base, os.O_RDWR|os.O_CREATE|os.O_EXCL, h.FileInfo().Mode())
if err != nil {
return err
}
defer file.Close()
if _, err := io.Copy(file, tarReader); err != nil {
return err
}
return nil
}

0 comments on commit 640ee0e

Please sign in to comment.