-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
63 lines (56 loc) · 1.42 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"strings"
)
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter Github Username or Organization: ")
username, _ := reader.ReadString('\n')
repos, err := getRepositories(username)
if err != nil {
fmt.Println("Error fetching repositories:", err)
return
}
cloneRepositories(username, repos)
}
func getRepositories(username string) (string, error) {
cmd := exec.Command("bash", "-c", fmt.Sprintf("gh repo list %s --json name | jq .[].name -r", strings.TrimSpace(username)))
fmt.Print(cmd)
out, err := cmd.Output()
fmt.Print(string(out))
if err != nil {
return "", err
}
return string(out), nil
}
func cloneRepositories(username, repos string) error {
urls := strings.Split(repos, "\n")
for _, url := range urls {
url = strings.TrimSpace(url) // Remove leading/trailing whitespace, including newline characters
url = strings.ReplaceAll(url, `"`, "")
if url == "" {
continue
}
fmt.Printf("Cloning %s\n", url)
path := fmt.Sprintf("https://github.com/%s/%s.git", strings.TrimSpace(username), strings.TrimSpace(url))
cmd := exec.Command("git", "clone", path)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return err
}
}
return nil
}
func runCommand(cmd string) (string, error) {
out, err := exec.Command("sh", "-c", cmd).Output()
if err != nil {
return "", err
}
return string(out), nil
}