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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
"text/tabwriter"
|
"text/tabwriter"
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
"code.gitea.io/sdk/gitea"
|
||||||
|
|
@ -33,10 +35,10 @@ var repoListCmd = &cobra.Command{
|
||||||
}
|
}
|
||||||
|
|
||||||
var repoCloneCmd = &cobra.Command{
|
var repoCloneCmd = &cobra.Command{
|
||||||
Use: "clone <owner/name>",
|
Use: "clone <owner/name> [destination]",
|
||||||
Short: "Clone a repository",
|
Short: "Clone a repository",
|
||||||
Long: "Clone a repository locally.",
|
Long: "Clone a repository locally. If destination is not specified, the repository name is used.",
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.RangeArgs(1, 2),
|
||||||
RunE: runRepoClone,
|
RunE: runRepoClone,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -175,9 +177,34 @@ func runRepoClone(cmd *cobra.Command, args []string) error {
|
||||||
cloneURL = repository.CloneURL
|
cloneURL = repository.CloneURL
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Cloning %s/%s...\n", owner, name)
|
// Determine destination path
|
||||||
fmt.Printf("git clone %s\n", cloneURL)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue