Skip to content

Commit

Permalink
use default output dir
Browse files Browse the repository at this point in the history
  • Loading branch information
qiushiyan committed Jul 22, 2024
1 parent d9555c2 commit 6b6d698
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
37 changes: 32 additions & 5 deletions cmd/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"os"
"strings"

degit "github.com/qiushiyan/degit/pkg"
"github.com/spf13/cobra"
Expand All @@ -13,17 +14,41 @@ var cloneCmd = &cobra.Command{
Use: "clone <src> <dst>",
Short: "Clone a repository locally",
Long: `Downloads a repository into a local destination directory.`,
Args: cobra.MatchAll(cobra.ExactArgs(2), cobra.OnlyValidArgs),
Args: cobra.MatchAll(cobra.OnlyValidArgs, cobra.MinimumNArgs(1)),
RunE: func(cmd *cobra.Command, args []string) error {
dst := args[1]
repo, err := degit.ParseRepo(args[0])
if err != nil {
return err
}

// if dst is not specified, use the repo name or subdir as the name for the output directory
var dst string
if len(args) < 2 {
if repo.Subdir != "" {
if strings.HasPrefix(repo.Subdir, "/") {
dst = repo.Subdir[1:]
} else {
dst = repo.Subdir
}
} else {
dst = repo.Name
}
} else {
dst = args[1]
}

if !Force {
stat, err := os.Stat(dst)
if err == nil && stat.IsDir() {
return fmt.Errorf("destination %s already exists, use --force to overwrite", dst)
return fmt.Errorf("destination `%s` already exists, use --force to overwrite", dst)
}
}

err := degit.Clone(args[0], args[1], Force, Verbose)
if Verbose {
fmt.Printf("Cloning `%s` into `%s`\n", repo.URL, dst)
}

err = repo.Clone(dst, Force, Verbose)
if err != nil {
return err
}
Expand All @@ -34,7 +59,9 @@ var cloneCmd = &cobra.Command{
}

if len(entries) == 0 {
fmt.Println("output directory is empty. did you specify the correct directory?")
fmt.Println(
"Output directory is empty, you might have specified an non-existing subfolder in the repository",
)
}

return nil
Expand Down
5 changes: 4 additions & 1 deletion pkg/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ func ParseRepo(src string) (*Repo, error) {
)
match := re.FindStringSubmatch(src)
if match == nil {
return nil, fmt.Errorf("could not parse %s", src)
return nil, fmt.Errorf(
"cant recognize %s as a git repository, example: github.com/user/repo",
src,
)
}

site := firstNonEmpty(match[1], match[2], match[3], "github")
Expand Down
6 changes: 3 additions & 3 deletions pkg/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ func (r *Repo) Clone(dst string, force bool, verbose bool) error {
}

func (r *Repo) download(dst string, hash string, verbose bool) error {
f, err := os.Create(dst)
folder, err := os.Create(dst)
if err != nil {
return err
}
defer f.Close()
defer folder.Close()

var url string
switch r.Site {
Expand All @@ -118,7 +118,7 @@ func (r *Repo) download(dst string, hash string, verbose bool) error {
}
defer resp.Body.Close()

_, err = io.Copy(f, resp.Body)
_, err = io.Copy(folder, resp.Body)

return err
}
Expand Down

0 comments on commit 6b6d698

Please sign in to comment.