From c689c7d1713b4f39ffc2001551394d5ed0871899 Mon Sep 17 00:00:00 2001 From: Romain Bertrand Date: Tue, 16 Dec 2025 12:38:19 +0100 Subject: [PATCH] enhance: clone feature --- cmd/repo.go | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/cmd/repo.go b/cmd/repo.go index 814f3c2..41f6650 100644 --- a/cmd/repo.go +++ b/cmd/repo.go @@ -3,6 +3,8 @@ package cmd import ( "fmt" "os" + "os/exec" + "path/filepath" "text/tabwriter" "code.gitea.io/sdk/gitea" @@ -33,10 +35,10 @@ var repoListCmd = &cobra.Command{ } var repoCloneCmd = &cobra.Command{ - Use: "clone ", + Use: "clone [destination]", Short: "Clone a repository", - Long: "Clone a repository locally.", - Args: cobra.ExactArgs(1), + Long: "Clone a repository locally. If destination is not specified, the repository name is used.", + Args: cobra.RangeArgs(1, 2), RunE: runRepoClone, } @@ -175,9 +177,34 @@ func runRepoClone(cmd *cobra.Command, args []string) error { cloneURL = repository.CloneURL } - fmt.Printf("Cloning %s/%s...\n", owner, name) - fmt.Printf("git clone %s\n", cloneURL) + // Determine destination path + var destination string + if len(args) > 1 { + destination = args[1] + } else { + destination = name + } + fmt.Printf("Cloning %s/%s to %s...\n", owner, name, destination) + + // Create parent directory if it doesn't exist + if dir := filepath.Dir(destination); dir != "." { + if err := os.MkdirAll(dir, 0755); err != nil { + return fmt.Errorf("failed to create destination directory: %w", err) + } + } + + // Execute git clone + gitCmd := exec.Command("git", "clone", cloneURL, destination) + gitCmd.Stdout = os.Stdout + gitCmd.Stderr = os.Stderr + gitCmd.Stdin = os.Stdin + + if err := gitCmd.Run(); err != nil { + return fmt.Errorf("failed to clone repository: %w", err) + } + + fmt.Printf("Repository cloned successfully to %s\n", destination) return nil }