enhance: clone feature
This commit is contained in:
parent
308ecf332c
commit
c689c7d171
1 changed files with 32 additions and 5 deletions
37
cmd/repo.go
37
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 <owner/name>",
|
||||
Use: "clone <owner/name> [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
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue