/
gogetgithub.go
97 lines (88 loc) · 2.24 KB
/
gogetgithub.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/cheggaaa/pb"
"log"
"os"
"os/exec"
"strings"
)
var Repos []GHRepo
func main() {
Username := flag.String("username", "", "The username that you are targetting")
update := flag.Bool("update", false, "rather than get, update packages")
flag.Parse()
if os.Getenv("GOPATH") == "" {
fmt.Println("You don't have a GOPATH set, I don't know where to clone to! Please set one.")
os.Exit(1)
}
if *Username == "" {
fmt.Println("Please give a username to auto clone all their go stars to the gopath to.")
os.Exit(1)
}
fmt.Print("Grabbing all stars off that user")
GHUrl := fmt.Sprintf("https://api.github.com/users/%s/starred", *Username)
s, e := ExpectGithubToBreak(GHUrl)
if e != nil {
fmt.Println("Cannot get the first set, Not going to attempt to get others.")
}
Repos = make([]GHRepo, 0)
CastData := make([]GHRepo, 0)
e = json.Unmarshal([]byte(s), &CastData)
Repos = FilterForGoRepo(CastData, Repos)
if e != nil {
fmt.Println("Cannot decode the first set, Not going to attempt to get others.")
}
var PageCount int = 2
var TripCount int = 0
for {
GHUrl = fmt.Sprintf("https://api.github.com/users/%s/starred?page=%d", *Username, PageCount)
s, e = ExpectGithubToBreak(GHUrl)
if e == nil && TripCount < 2 {
CastData = make([]GHRepo, 0)
e = json.Unmarshal([]byte(s), &CastData)
if len(CastData) == 0 {
// We have found it all!
break
}
if e != nil {
fmt.Println("Cannot decode the first set, Not going to attempt to get others.")
return
}
Repos = FilterForGoRepo(CastData, Repos)
PageCount++
fmt.Print(".")
} else {
TripCount++
if TripCount < 2 {
fmt.Println("API errors stopped the program from running.", e.Error())
}
}
}
bar := pb.StartNew(len(Repos))
for _, repo := range Repos {
GoGet(strings.Replace(repo.HtmlURL, "https://", "", -1), update)
bar.Increment()
}
}
func GoGet(url string, update *bool) {
buf := make([]byte, 1024)
var cmd *exec.Cmd
if *update {
cmd = exec.Command("go", "get", "-u", url)
} else {
cmd = exec.Command("go", "get", url)
}
stdout, _ := cmd.StdoutPipe()
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
for {
_, e := stdout.Read(buf)
if e != nil {
break
}
}
}